10
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?

CircleCIでiOSアプリをビルドするためのconfig.ymlの設定とローカルでfastlaneを実行する方法について

Last updated at Posted at 2025-08-26

はじめに

CircleCIを使用してiOSアプリをビルドし、DeployGateにアップロードするまでのCI/CDパイプラインを作成しました。その際に作成したconfig.ymlとfastlaneを用いてローカルでiOSアプリのビルドを行う手順に関して説明します。

CircleCIの設定を行う

CircleCIとは

SaaS型のCI/CDサービスです。クラウド上のVM/Dockerを使用して、iOS、iPadOS、Androidアプリのビルドが可能です。

CircleCI CLIをインストールする

CircleCI Docs:Install and configure the CircleCI local CLIを参考に以下のコマンドでインストールしました。CircleCI CLIはconfig.ymlの文法チェックに使用します。

$ brew install circleci

.circleci/config.ymlを作成する

以下のように作成しました。

.circleci/config.yml
version: 2.1
jobs:
  build:
    macos:
      xcode: 16.2.0
    steps:
      # デフォルトのcheckoutの代わりにshallow cloneを使用
      - run:
          name: checkout (shallow clone)
          command: |
            git clone --depth 1 --branch $CIRCLE_BRANCH "$CIRCLE_REPOSITORY_URL" .  
      # 依存関係のインストール
      - run:
          name: Bundle Install
          command: |
            bundle install
      # CocoaPodsのインストール
      - run:
          name: Install CocoaPods
          command: |
            pod install
      # fastlaneの実行
      - run:
          name: fastlane build_lane
          command: | 
            bundle exec fastlane build_lane
      # deploygateへのアップロード
      - run:
          name: DeployGate upload
          command: |
            curl --url "https://deploygate.com/api/users/${OWNER_NAME}/apps" \
                    -H "Authorization:Bearer $DEPLOYGATE_API_TOKEN" \
                    -X POST \
                    -F "file=@deploy/test.ipa" \
                    --form-string "message=メッセージ欄"

workflows:
  version: 2
  build:
    jobs:
      - build:
          filters:
            branches:
              only:
                - master
  • ビルドに使用するmacOS VMはxcode: 16.2.0としました
  • gitリポジトリのクローンにはShallow cloneを使用しています
    • checkoutを使用することで、楽にクローンをすることができますが、リポジトリが大きくクローンに時間がかかってしまったため、このように対処しました

config.ymlの文法チェックを行う

以下のコマンドで文法チェックができます

$ circleci config validate

CircleCI CLIでは、Dockerイメージが提供されている場合に限りローカルでCircleCIの動作確認が可能です。しかし、iOSの場合はMacOS VMを使用するため、ローカルでの実行はできません。ローカルでCircleCIを動かしたい場合は以下のサイトがわかりやすくて良いと思います。

fastlaneの設定を行う

fastlaneとは

iOSアプリやAndroidアプリのビルド、テスト、デプロイなどのタスクを自動化するためのツールです。Rubyで記述を行います。
ビルドをする、証明書を取得するなどの処理をアクションと呼びます。今回使用しているアクションは以下の2つです。

  • gym: iOSアプリのビルドやIPAファイルの作成を行う
  • match: 証明書の管理を行う

fastlaneをインストールする

fastlane docs:Getting started with fastlane for iOSにしたがって以下の手順でインストールを行いました。

  • Xcode command line toolsのインストールを行う
$ xcode-select --install
  • Rubyのバージョンが2.5以上であることを確認する
$ ruby --version
  • Bundlerをインストールする
$ gem install bundler
  • Gemfileの雛形を作成する
$ bundle init
  • Gemfileを以下のように編集する
source "https://rubygems.org"

gem "fastlane"

fastlaneのセットアップをする

以下のコマンドで、fastlaneのセットアップができます。

$ bundle exec fastlane init

[14:33:26]: What would you like to use fastlane for?
1. 📸  Automate screenshots
2. 👩‍✈️  Automate beta distribution to TestFlight
3. 🚀  Automate App Store distribution
4. 🛠  Manual setup - manually setup your project to automate your tasks

使用用途が質問されるので、使用用途にあったものを選択しましょう。今回はビルドをしたいだけなので4を選択します。
選択するとセットアップが開始します。Continue by pressing Enterと表示されたらEnterを押すと、fastlaneフォルダの中にAppfileとFastfileが作成されます。各ファイルは以下のように作成しました。

Appfile

team_id("XXXXXXXXXX") # Apple DeveloperのteamIDを記述する

Fastfile

# frozen_string_literal: true

default_platform(:ios)
xcode_select('/Applications/Xcode-16.2.app')

# Build Configurationを定義
module Configuration
  STAGING = 'staging'
end

platform :ios do
  desc 'Description of what the lane does'
  lane :build_lane do
    build(Configuration::STAGING)
  end
end

# build configurationを指定してビルドする関数
def build(configuration)
  setup_ci(provider: 'circleci')
  match(type: 'development', verbose: true, readonly: true)
  gym(
    workspace: 'hogehoge.xcworkspace',
    configuration: configuration,
    scheme: 'hogehoge',
    output_directory: 'deploy',
    output_name: 'test.ipa',
    export_method: 'development'
  )
end

matchのセットアップをする

続いてビルド時に使用する証明書を管理するためにmatchのセットアップを行います。以下のコマンドを実行します。

$ bundle exec fastlane match init

fastlane match supports multiple storage modes, please select the one you want to use:
1. git
2. google_cloud
3. s3
4. gitlab_secure_files
?  

質問がされるので証明書を管理するものを選択します。
gitで管理をするため、1を選択しました。1を選ぶと以下のようにリポジトリのURLが聞かれますが後で入力できるためEnterで大丈夫です。

Please create a new, private git repository to store the certificates and profiles there
[14:39:45]: URL of the Git Repo: 

回答を終えるとfastlaneフォルダの中にMatchfileが作成されます。以下のように作成しました。

Matchfile

git_url("") # matchの証明書の保存に使用するリポジトリのURLを記載
storage_mode("git")
type("development")
app_identifier("tools.fastlane.app")
username("user@fastlane.tools") # Apple Developer Portal のusernameを記載

Matchfileに必要事項を記入したら以下を実行して設定したgitリポジトリに証明書を登録します。証明書のタイプは適宜変更してください。

$ bundle exec fastlane match import --type development

初めて証明書を登録する際にはパスフレーズの設定が必要になります。パスフレーズを忘れると登録や読み取りができなくなるため、忘れないようにしましょう。ここで設定したパスフレーズはリポジトリ固有のものとなり、他の人が作業する際にも必要になります。また、CircleCIでビルドする場合には、パスフレーズをMATCH_PASSWORDという名前で環境変数に設定する必要があります。
パスフレーズを入力後、以下のファイルを登録します。あらかじめ準備をしておいてください。

  • 証明書( Certificate )
  • 秘密鍵( Private key )
  • プロビジョニングプロファイル( Provisioning profile)

登録ができているか以下のコマンドで確認しましょう。

$ bundle exec fastlane match development --readonly

matchの設定には以下のサイトを参考にしました。

fastlaneをローカルで実行する

以下のコマンドでfastlaneを実行してみましょう。fastlaneのビルドが成功すると、deploy/test.ipaというIPAファイルが作成されます。

$ bundle exec fastlane build_lane

DeployGateへのアップロードを試す

DeployGateへのアップロードは公式サイトを参考にしました。以下のコマンドで実行できます。

$ curl --url "https://deploygate.com/api/users/${OWNER_NAME}/apps" \
        -H "Authorization:Bearer $DEPLOYGATE_API_TOKEN" \
        -X POST \
        -F "file=@deploy/test.ipa" \
        --form-string "message=DeployGateに表示したい文章を入力する。"

終わりに

CircleCIでiOSアプリをビルドするためのconfig.ymlやfastlaneについて説明を行いました。fastlaneを作成するだけでも、コマンド一つでアプリのビルドを行うことができて便利だと思います。今後は、Androidアプリのビルドについてもまとめていきたいと思います。

10
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
10
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?