LoginSignup
1

More than 3 years have passed since last update.

Bitrise CLIでiOSターゲットのUnityプロジェクトをアーカイブ

Last updated at Posted at 2019-12-09

はじめに

こんにちわ、 @kiy0p0n です。
Diverse Advent Calendar 2019の9日目の記事です。

前書き

今回はUnityでモバイルゲーム開発の際のCIについての話題です。
JenkinsでiOS/Androidのデプロイを自動化する記事はたくさんあるのですが、サーバーレスでスクリプトを書かずとも手軽にCI始めたいなと思った際に、bitriseでiOS/Androidのデプロイを自動化できるのでは?
ということをちょっと調べて実際にやってみた内容を今回は記事としてまとめました。

やったこと

bitrise CLIを用いてiOSターゲットのUnityプロジェクトのビルド ~ ipa生成

環境

env version
OS macOS Catalina(10.15.1)
bitrise CLI 1.36.0
unity 2019.1
xcode 11.2

フロー

  1. bitrise CLIのインストール
  2. Unityプロジェクト
    • ビルドスクリプトの追加
    • プロジェクト設定
    • ビルドステップの追加
  3. ipaアーカイブステップの追加

内容

1. bitrise CLIのインストール

homebrewでインストールします。
その後、セットアップコマンドでツール群をインストールします。


$ brew update && brew install bitrise
# install log
$ bitrise -v
1.36.0
$ bitrise setup
# setup log

bitriseの処理は bitrise.yml に記述していきます。
次の内容の bitrise.yml を作成し、実行してみます。
workflows でワークフローを定義します。ワークフローはいくつかの処理を行うステップをまとめたものです。
以下での test はワークフロー名になります。実行時にワークフローを指定します。

format_version: 1.3.1
default_step_lib_source: https://github.com/bitrise-io/bitrise-steplib.git

workflows:
  test:
    steps:
      inputs:
      - content: "echo 'Hello World!'"

testワークフローの実行結果
$ tree .
.
└── bitrise.yml
$ cat bitrise.yml
format_version: 1.3.1
default_step_lib_source: https://github.com/bitrise-io/bitrise-steplib.git

workflows:
  test:
    steps:
    - script@1.1.5:
        inputs:
        - content: "echo 'Hello World!'"
$ bitrise run test

  ██████╗ ██╗████████╗██████╗ ██╗███████╗███████╗
  ██╔══██╗██║╚══██╔══╝██╔══██╗██║██╔════╝██╔════╝
  ██████╔╝██║   ██║   ██████╔╝██║███████╗█████╗
  ██╔══██╗██║   ██║   ██╔══██╗██║╚════██║██╔══╝
  ██████╔╝██║   ██║   ██║  ██║██║███████║███████╗
  ╚═════╝ ╚═╝   ╚═╝   ╚═╝  ╚═╝╚═╝╚══════╝╚══════╝

  version: 1.36.0

INFO[22:43:09] bitrise runs in Secret Filtering mode
INFO[22:43:09] Running workflow: test

Switching to workflow: test

+------------------------------------------------------------------------------+
| (0) script@1.1.5                                                             |
+------------------------------------------------------------------------------+
| id: script                                                                   |
| version: 1.1.5                                                               |
| collection: https://github.com/bitrise-io/bitrise-steplib.git                |
| toolkit: bash                                                                |
| time: 2019-12-08T22:43:11+09:00                                              |
+------------------------------------------------------------------------------+
|                                                                              |
Hello World!
|                                                                              |
+---+---------------------------------------------------------------+----------+
| ✓ | script@1.1.5                                                  | 2.56 sec |
+---+---------------------------------------------------------------+----------+


+------------------------------------------------------------------------------+
|                               bitrise summary                                |
+---+---------------------------------------------------------------+----------+
|   | title                                                         | time (s) |
+---+---------------------------------------------------------------+----------+
| ✓ | script@1.1.5                                                  | 2.56 sec |
+---+---------------------------------------------------------------+----------+
| Total runtime: 2.56 sec                                                      |
+------------------------------------------------------------------------------+


Submitting anonymized usage informations...
For more information visit:
https://github.com/bitrise-io/bitrise-plugins-analytics/blob/master/README.md

2. Unityプロジェクト

ビルドスクリプトの追加

Unityはコマンドラインから実行できようになっています。
Unity マニュアル - コマンドライン引数
マニュアルにライセンスのやりとり方法が書いてありますので、Proを使う場合は参考にしてください。

以下のコマンドで、プロジェクト内のスクリプトを実行できます。
スクリプトにビルド処理を記述することで、Unity Editorを起動せずにビルドを行うことができます。

$ /Applications/Unity/Unity.app/Contents/MacOS/Unity \
    -nographics \
    -quit \
    -batchmode \
    -logFile \
    -projectPath #{プロジェクトパス} \
    -executeMethod #{実行するスクリプトのクラス}.#{実行する関数}
    # 追加のオプションがある場合は続けて記述

ビルドを行うクラスを実装します。
ビルド先のオプションを -output で受け取れるようにしてます。

Assets/Bitrise.cs

#if UNITY_EDITOR
using UnityEditor;
using System.Linq;
using System;

class Bitrise
{
    public static void Build()
    {
        BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions();
        string[] args = Environment.GetCommandLineArgs();
        for (int i = 0; i < args.Length; i++)
        {
            if (args[i].Equals("-output"))
                buildPlayerOptions.locationPathName = args[i + 1];
        }
        var activeScenes = EditorBuildSettings.scenes.Where(s => s.enabled).Select(s => s.path).ToArray();
        buildPlayerOptions.scenes = activeScenes;
        buildPlayerOptions.target = BuildTarget.iOS;
        buildPlayerOptions.options = BuildOptions.None;
        BuildPipeline.BuildPlayer(buildPlayerOptions);
    }
}
#endif

バッチモードでのビルド実行
$ pwd
/Users/kiy0p0n/deploy-test
$ tree "Assets"
Assets
├── Bitrise.cs
├── Bitrise.cs.meta
├── Scenes
│   ├── Test.unity
│   └── Test.unity.meta
└── Scenes.meta

1 directory, 5 files
$ /Applications/Unity/Unity.app/Contents/MacOS/Unity \
    -nographics \
    -quit \
    -batchmode \
    -logFile \
    -projectPath /Users/kiy0p0n/deploy-test \
    -executeMethod Bitrise.Build
    -output /Users/kiy0p0n/deploy-test/Build
# build log

プロジェクト設定

Unity上でiOSの署名周りの設定をする場合は参考にしてください。
Unity上でなくとも、bitrise CLIのapiアーカイブのステップでも設定可能です。
わかりづらい項目だけピックアップして記載してます。
Project Settings > Player > iOS Traget(タブ選択) > Other Settings から署名に必要なパラメータを設定します。

Singing TeamID

Singing TeamID を設定します。TeamIDはApple Developer - Accountページから取得できます。
フォーマット例: AS345R67R1

iOS Provisioning Profile

任意のProvisioning Profileを設定する場合は、以下のどちらかでUUIDを設定できます。

  • Provisioning Profileをダウンロードし、 Browse ボタンでダウンロードした .mobileprovision ファイルを選択
  • Provisioning Profileをダウンロードし、テキストエディタで開き、 UUID の値を取得する

フォーマット例: c5be4123-1234-4f9d-9843-0d9be985a068

ビルドステップの追加

bitrise.yml にUnityプロジェクトのビルドのステップを追加します。

format_version: 1.3.1
default_step_lib_source: https://github.com/bitrise-io/bitrise-steplib.git

workflows:
  test:
    steps:
    - script@1.1.5:
        inputs:
        - content: |
            #!/bin/bash
            /Applications/Unity/Unity.app/Contents/MacOS/Unity
                -nographics
                -quit
                -batchmode
                -logFile
                -projectPath /Users/kiy0p0n/deploy-test
                -executeMethod Bitrise.Build
                -output /Users/kiy0p0n/deploy-test/Build

3. ipaアーカイブステップの追加

bitriseのツールに xcode-achive というアーカイブの記述を簡易にできるモノがあるので利用します。
基本的な記述は以下のようになります。

    - xcode-archive@2.7.0:
        inputs:
        - output_tool: xcodebuild
        - export_method: development
        - output_dir: #{ipaの出力先}
        - project_path: #{xcodeproj or xcworkspaceのパス}
        - scheme: #{アーカイブするスキーマ}
        - team_id: #{署名時のチームID}
        - force_provisioning_profile: #{任意のProvisioning ProfileのUUID}
        # その他のオプション

team_idforce_provisioning_profile オプションは、Unity上で署名関連の設定が済んでいる場合は不要です。Team IDやProvisioning ProfileのUUIDは、プロジェクト設定と同じ形式のモノになります。
細かいその他オプションはソース元のステップを参照してください。

まとめ

最終的な bitrise.yml とワークフローの実行

$ cat bitrise.yml
format_version: 1.3.1
default_step_lib_source: https://github.com/bitrise-io/bitrise-steplib.git

workflows:
  test:
    steps:
    - script@1.1.5:
        inputs:
        - content: |
            #!/bin/bash
            /Applications/Unity/Unity.app/Contents/MacOS/Unity
                -nographics
                -quit
                -batchmode
                -logFile
                -projectPath /Users/kiy0p0n/deploy-test
                -executeMethod Bitrise.Build
                -output /Users/kiy0p0n/deploy-test/Build
    - xcode-archive@2.7.0:
        inputs:
        - output_tool: xcodebuild
        - export_method: development
        - output_dir: /Users/kiy0p0n/deploy-test
        - project_path: /Users/kiy0p0n/deploy-test/Build/Unity-iPhone.xcodeproj

        - scheme: Unity-iPhone
        - compile_bitcode: "no"
$ bitrise run test
# 以下ビルドログ

最後に

手元でコマンド一つでipaまでビルドできました!
BitriseにはdeploygateなどのデプロイツールへのアップロードやSlackといったチャットツールへの投稿を簡易に設定できるツールも用意されているので、色々利用してデプロイ周りを便利にしていきたいです。
また、Bitriseの公式Blogにて、BitriseでUnityビルドができる方法が紹介されてるので、今回の続きとして、ローカル環境ではなくクラウドで実行できるようにしたいです。

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
1