Carthage を使用しているプロジェクトでの Circle CI

Carthage を使用しているプロジェクトでの Circle CI

タイトルの通り、Carthage を使用しているプロジェクトで Circle CI を使うための設定方法をまとめておく。
CocoaPods ならデフォルトでいい感じにサポートしている Circle だが、Carthage だと一手間ありそうな印象。

Caching Carthage con CircleCI

Caching Carthage con CircleCI

これが公式っぽいのでこれに沿って行こうと思ったが、こいつは CircleCI 1.0 向けの記載になっていて、2018年8月には1.0はサポート終了、2.0に移行したまえとのことなのでおっかなびっくり進めてみる。

Note that we’re assuming that you have added your Carthage/ directory to your .gitignore for the purposes of this post. If you are checking in Carthage/Checkouts or Carthage/Build, this post might not be super useful for you.

んー?今使っている .gitignoreこれ

# Carthage
#
# Add this line if you want to avoid checking in source code from Carthage dependencies.
# Carthage/Checkouts

残念。

コメントアウトを外して .gitignoreCarthage/Checkouts を追加し、以下の手順でこの変更を反映させる。

$ git rm -r --cached .
$ git add -A
$ git commit

Improving Carthage

carthage bootstrap コマンド+αをスクリプトにまとめておいて後で使うらしい。

bin/bootstrap ファイルを作成し以下を記載しておく。

#!/bin/sh

carthage bootstrap
cp Cartfile.resolved Carthage

実行権を付与しておく。

$ chmod +x bin/bootstrap

依存パッケージに更新があるかどうかの判定用に bin/bootstrap-if-needed ファイルも作成する。

#!/bin/sh

if ! cmp -s Cartfile.resolved Carthage/Cartfile.resolved; then
  bin/bootstrap
fi

こちらも実行権を付与しておく。

$ chmod +x bin/bootstrap-if-needed

Caching with Circle

一度依存パッケージを取得したら、これをキャッシュして差分が無い限り使い回す。
これで毎回のCIビルドを高速化できる。

ここまでの内容も踏まえて設定ファイルは最終的に以下のようになった。

# iOS CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/ios-migrating-from-1-2/ for more details
#
version: 2
jobs:
  build:

    # Specify the Xcode version to use
    macos:
      xcode: "9.3.0"

    steps:
      - checkout

      # build dependencies if the cached dependencies are out of date
      - restore_cache:
          key: 1-charthage-{{ checksum "Cartfile.resolved" }}
      - run:
          name: build dependencies if the cached dependencies are out of date
          command: bin/bootstrap-if-needed
      - save_cache:
          key: 1-charthage-{{ checksum "Cartfile.resolved" }}
          paths:
            - Carthage

      # Build the app and run tests
      - run:
          name: Build and run tests
          command: fastlane scan
          environment:
            SCAN_DEVICE: iPhone X
            SCAN_SCHEME: RxSwiftRailsTutorial

      # Collect XML test results data to show in the UI,
      # and save the same XML files under test-results folder
      # in the Artifacts tab
      - store_test_results:
          path: test_output
      - store_artifacts:
          path: /tmp/test-results
          destination: scan-test-results
      - store_artifacts:
          path: ~/Library/Logs/scan
          destination: scan-logs

ひとまず動作はOK。