bitrise のビルドをちょっとだけ短縮

iOS アプリの bitrise ビルドが10分を超える事が多いので、まずは全体を見て手早く改善できるところを探してみた

最も時間がかかるのは xcode-test step だけど、これはアプリ側で色々見直さないといけなさそうで一旦スキップ...

次に目についたのが、 bundle install という script step 👀
bundler をインストールして、 fastlane や danger といった gem をインストールしている

...
+---+---------------------------------------------------------------+----------+
| ✓ | bundle install                                                | 2.1 min  |
+---+---------------------------------------------------------------+----------+
...

bitrise のキャッシュ

毎回インストールする必要はないので、 bitrise にキャッシュさせたい

devcenter.bitrise.io

実行する workflow の最初と最後に、 Cache:Pull と Cache:Push の step を挟み込むと、指定のディレクトリやファイルを s3 にキャッシュしてくれる

The cache is stored as a single archive file: if the content of the cached paths changes in any way, the entire file gets updated. Every branch of your repository on which you run a build will have its own cache archive.

キャッシュはビルドを実行するブランチごとに保存されて、7日使わないと削除される

ブランチごとにキャッシュを持っているなら、新しくブランチを作る度にキャッシュが無効になるの...?と思ったけど、もちろんそんなことはなさそう

If you only want to delete the cache which is related to a single branch, you should also delete the default branch’s cache too! This is because if a build runs on a branch which doesn’t have a cache, the Bitrise.io Cache:Pull Step will get the cache of the default branch.

新しいブランチなどキャッシュが存在しない場合、 Cache:Pull step はデフォルトブランチのキャッシュを利用してくれる

また、指定したファイルをトリガーにキャッシュを更新することもできる
何をトリガーに、何をキャッシュするのかを workflow editor の Cache Paths で指定できる

今回は、 gem のインストール先の vendor/bundle をキャッシュして、 Gemfile.lock に更新が合った時にキャッシュも更新させたいので、以下を追記した
vendor/bundle -> Gemfile.lock

f:id:takorras:20200711161743p:plain

テストしてみる

上記の変更を加えた適当なworkflowでテストしてみる 💭

初回のビルド

bundle install の実行時間は変わらなかったが、 Cache:Push でキャッシュされた(removed file があるのは、おそらく既に Cache:Pull と Cache:Push を導入していた影響...)

...

Checking for file changes
1148 files needs to be removed
0 files has changed
8086 files added
File changes found in 4.069593ms

Generating cache archive
Done in 13.97858345s

Uploading cache archive
Archive file size: 148219904 bytes / 141.353516 MB
Done in 3.691688272s

2度目のビルド

bundle install の実行時間が 6.62sec に改善された

...
+---+---------------------------------------------------------------+----------+
| ✓ | bundle install                                                | 6.62 sec |
+---+---------------------------------------------------------------+----------+
...

bundle install では、キャッシュされたgemを使っている

+ bundle install --path vendor/bundle
Using rake 13.0.1
....

また、Gemfile.lock には変更がないので、 Cache:Push ではキャッシュも更新していない

...
Checking for file changes
0 files needs to be removed
0 files has changed
0 files added
No files found in 4.308899ms

Total time: 317.307518ms

期待通り動作してくれていそう😎
bundle install の実行時間は、 2.1min → 6.6sec に短縮された

3度目のビルド

試しに適当なgemのバージョンを変更してみると、 bundle install では当然該当の gem だけインストールされた

...
Using terminal-table 1.8.0
Fetching danger 8.0.1
Installing danger 8.0.1
Using thor 0.20.3
...

Gemfile.lock の変更を検知して、キャッシュも更新されている

Checking for file changes
0 files needs to be removed
8086 files has changed
121 files added
File changes found in 5.523209ms

Generating cache archive
Done in 2.66160931s

Uploading cache archive
Archive file size: 148735488 bytes / 141.845215 MB
Done in 3.391270071s

Total time: 6.574466233s

まとめ

bitrise のキャッシュは簡単に利用できてとっても便利

今回のさくっとした変更で短縮できたビルド時間は2分程度だけど、頻繁に使うのでかなりの嬉しい改善になった
今後も手が空いた時にビルド時間の改善にしてみる👀