2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Unity as a LibraryのXCFrameworkを生成するGitHub Actionsの紹介

Posted at

Unity as a Library×Swiftの記事はシリーズになっています。
記事を順番に読み進めると、Unity as a LibraryをSwiftで使えるようになります。

はじめに

本記事は どすこい塾 Advent Calendar 2025 の15日目の記事です。
昨日も @uhooiUnity as a LibraryでXCFrameworkを生成する方法(Swift) でした。

Unity as a LibraryのXCFrameworkを生成するGitHub Actionsのワークフローを紹介します。

前提条件

XCFrameworkの提供側と利用側のリポジトリが分かれていると仮定します。
わかりやすくするため、架空のリポジトリを使って説明します。

  • 提供側リポジトリ: uhooi/loki-unity
    • Unity as a Libraryのコードを管理している
  • 利用側リポジトリ: uhooi/loki-ios
    • iOSアプリのコードを管理している
    • uhooi/loki-unity が提供しているXCFrameworkを利用している

フォルダ構成は以下とします。

$ tree
.
├── loki-ios
│   └── Packages
│       ├── LokiPackage
│       │   └── Package.swift
│       └── unity-framework
│           ├── commit_hash.txt
│           ├── Package.swift
│           └── UnityFramework.xcframework
└── loki-unity
    ├── Makefile
    └── UnityProject
        ├── iosBuild
        │   └── Unity-iPhone.xcodeproj
        └── iosEmptyBuild
            └── UnityFramework.xcodeproj

ワークフローの全体像

最初にワークフローの全体像を紹介します。
ワークフローは提供側と利用側で2つ作ります。

## `uhooi/loki-unity` のワークフロー

- `main` ブランチへのプッシュをトリガーに、`uhooi/loki-ios` のワークフローを実行する

## `uhooi/loki-ios` のワークフロー

- `uhooi/loki-unity` をクローンする
- XCFrameworkを生成する
- PRを作成する

ワークフローの作成

ワークフローを作成します。

XCFrameworkの提供側

main ブランチへのプッシュをトリガーに、 uhooi/loki-ios にあるワークフローを実行するだけです。

以下の記事の通りに作ります。

update_unity_framework.yml (uhooi/loki-unity)
on:
  workflow_dispatch:
  push:
    branches:
      - main

jobs:
  update-unity-framework-ios:
    runs-on: macos-15
    steps:
      - name: Update Unity framework for iOS
        run: gh workflow run update-unity-framework.yml --repo 'uhooi/loki-ios' --ref 'develop'

トリガーは必要に応じて変更してください。

XCFrameworkの利用側

今回は利用側に作業を寄せているので、少し長いです。
1ステップずつ説明します。

ステップ以外

ステップ以外の箇所をまとめて説明します。

トリガーは uhooi/loki-unity 側で指定しているため、ここでは workflow_dispatch: のみ指定しています。

パーミッションはファイルを更新してPRを作成するため、 contentspull-requestswrite を指定します。

runs-on にはmacOSを指定します。

update_unity_framework.yml (uhooi/loki-ios)
on:
  workflow_dispatch:

permissions:
  contents: write
  pull-requests: write

jobs:
  update-unity-framework:
    runs-on: macos-15

    steps:
      # ...

チェックアウト(loki-ios)

loki-ios をチェックアウトします。

update_unity_framework.yml (uhooi/loki-ios)
      - name: Checkout loki-ios
        uses: actions/checkout@v4

チェックアウト(loki-unity)

loki-unity をチェックアウトします。

lfs: true はGit LFSを使っている場合のみ指定します。

update_unity_framework.yml (uhooi/loki-ios)
      - name: Checkout loki-unity
        uses: actions/checkout@v4
        with:
          repository: 'uhooi/loki-unity'
          path: 'loki-unity'
          lfs: true

更新チェック

更新が必要か確認します。
loki-ios/Packages/unity-framework/commit_hash.txt に前回更新時の loki-unity のコミットIDを保持しているので、 HEAD と比較してコミットIDが同じ場合は更新しません。

update_unity_framework.yml (uhooi/loki-ios)
      - name: Check version if already updated
        id: check-version
        run: |
          OLD_COMMIT_HASH=$(cat ./Packages/unity-framework/commit_hash.txt)
          NEW_COMMIT_HASH=$(cd ./loki-unity/ && git rev-parse HEAD)
          if [[ "$OLD_COMMIT_HASH" == "$NEW_COMMIT_HASH" ]]; then
            echo "❌ Error: Already updated."
            exit 1
          fi
          echo "new_commit_hash=$NEW_COMMIT_HASH" >> "$GITHUB_OUTPUT"

Unity frameworkの生成

Unity frameworkを生成します。
前回の記事 で作成した make ios を実行するだけです。

update_unity_framework.yml (uhooi/loki-ios)
      - name: Create Unity framework
        run: |
          cd ./loki-unity/
          make ios
          cd ./../

Unity frameworkの上書き

元のUnity frameworkを削除し、新しいものに差し替えます。

update_unity_framework.yml (uhooi/loki-ios)
      - name: Replace Unity framework
        run: |
          rm -rf ./Packages/unity-framework/UnityFramework.xcframework
          mv ./loki-unity/UnityProject/UnityFramework.xcframework/ ./Packages/unity-framework/

ブランチの作成

PRの作成に必要なブランチを作成します。

ブランチ名は被らないようにコミットIDを含めています。

update_unity_framework.yml (uhooi/loki-ios)
      - name: Setup branch
        id: setup-branch
        run: |
          BASE_BRANCH="${{ github.ref_name }}"
          NEW_COMMIT_HASH="${{ steps.check-version.outputs.new_commit_hash }}"
          if [[ "$BASE_BRANCH" == main ]]; then
            NEW_BRANCH="hotfix/update_unity_framework_$NEW_COMMIT_HASH"
          else
            NEW_BRANCH="feature/update_unity_framework_$NEW_COMMIT_HASH"
          fi
          git fetch origin "$BASE_BRANCH"
          git switch -c "$NEW_BRANCH" "origin/$BASE_BRANCH"
          echo "new_branch=$NEW_BRANCH" >> "$GITHUB_OUTPUT"
          echo "base_branch=$BASE_BRANCH" >> "$GITHUB_OUTPUT"

コミット

commit_hash.txt を更新し、Unity frameworkと一緒にコミットします。

update_unity_framework.yml (uhooi/loki-ios)
      - name: Commit
        run: |
          NEW_COMMIT_HASH="${{ steps.check-version.outputs.new_commit_hash }}"
          NEW_BRANCH="${{ steps.setup-branch.outputs.new_branch }}"
          echo $NEW_COMMIT_HASH > ./Packages/unity-framework/commit_hash.txt
          git add ./Packages/unity-framework/
          git commit -m "Update Unity framework"
          git push origin "$NEW_BRANCH"

PRの作成

PRを作成したら完了です。

update_unity_framework.yml (uhooi/loki-ios)
      - name: Create Pull Request
        run: |
          BASE_BRANCH="${{ steps.setup-branch.outputs.base_branch }}"
          NEW_BRANCH="${{ steps.setup-branch.outputs.new_branch }}"
          NEW_COMMIT_HASH="${{ steps.check-version.outputs.new_commit_hash }}"
          gh pr create \
            --title "[commit: ${NEW_COMMIT_HASH:0:7}] Unity frameworkの更新" \
            --body "" \
            --base "$BASE_BRANCH" \
            --head "$NEW_BRANCH"

ワークフローの全貌

最後にワークフローの全貌を紹介します。

update_unity_framework.yml (uhooi/loki-ios)
on:
  workflow_dispatch:

permissions:
  contents: write
  pull-requests: write

jobs:
  update-unity-framework:
    runs-on: macos-15

    steps:
      - name: Checkout loki-ios
        uses: actions/checkout@v4

      - name: Checkout loki-unity
        uses: actions/checkout@v4
        with:
          repository: 'uhooi/loki-unity'
          path: 'loki-unity'
          lfs: true

      - name: Check version if already updated
        id: check-version
        run: |
          OLD_COMMIT_HASH=$(cat ./Packages/unity-framework/commit_hash.txt)
          NEW_COMMIT_HASH=$(cd ./loki-unity/ && git rev-parse HEAD)
          if [[ "$OLD_COMMIT_HASH" == "$NEW_COMMIT_HASH" ]]; then
            echo "❌ Error: Already updated."
            exit 1
          fi
          echo "new_commit_hash=$NEW_COMMIT_HASH" >> "$GITHUB_OUTPUT"

      - name: Create Unity framework
        run: |
          cd ./loki-unity/
          make ios
          cd ./../

      - name: Replace Unity framework
        run: |
          rm -rf ./Packages/unity-framework/UnityFramework.xcframework
          mv ./loki-unity/UnityProject/UnityFramework.xcframework/ ./Packages/unity-framework/

      - name: Setup branch
        id: setup-branch
        run: |
          BASE_BRANCH="${{ github.ref_name }}"
          NEW_COMMIT_HASH="${{ steps.check-version.outputs.new_commit_hash }}"
          if [[ "$BASE_BRANCH" == main ]]; then
            NEW_BRANCH="hotfix/update_unity_framework_$NEW_COMMIT_HASH"
          else
            NEW_BRANCH="feature/update_unity_framework_$NEW_COMMIT_HASH"
          fi
          git fetch origin "$BASE_BRANCH"
          git switch -c "$NEW_BRANCH" "origin/$BASE_BRANCH"
          echo "new_branch=$NEW_BRANCH" >> "$GITHUB_OUTPUT"
          echo "base_branch=$BASE_BRANCH" >> "$GITHUB_OUTPUT"

      - name: Commit
        run: |
          NEW_COMMIT_HASH="${{ steps.check-version.outputs.new_commit_hash }}"
          NEW_BRANCH="${{ steps.setup-branch.outputs.new_branch }}"
          echo $NEW_COMMIT_HASH > ./Packages/unity-framework/commit_hash.txt
          git add ./Packages/unity-framework/
          git commit -m "Update Unity framework"
          git push origin "$NEW_BRANCH"

      - name: Create Pull Request
        run: |
          BASE_BRANCH="${{ steps.setup-branch.outputs.base_branch }}"
          NEW_BRANCH="${{ steps.setup-branch.outputs.new_branch }}"
          NEW_COMMIT_HASH="${{ steps.check-version.outputs.new_commit_hash }}"
          gh pr create \
            --title "[commit: ${NEW_COMMIT_HASH:0:7}] Unity frameworkの更新" \
            --body "" \
            --base "$BASE_BRANCH" \
            --head "$NEW_BRANCH"

おわりに

Unity as a LibraryのコードをプッシュするたびにXCFrameworkを生成するワークフローを構築できました。
これでライブラリのビルドを手動で行う必要がなくなり、実装に集中できます。

以上 どすこい塾 Advent Calendar 2025 の15日目の記事でした。
明日も @uhooi です。

参考リンク

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?