wada811 / blog

wada811's blog
https://github.com/wada811/blog/issues
Apache License 2.0
6 stars 0 forks source link

bitrise.yml をリポジトリで管理するための Bitrise ワークフロー #41

Open wada811 opened 4 years ago

wada811 commented 4 years ago

Bitrise とは

Bitrise は GUI で簡単にワークフローを設定できるモバイルプラットフォーム向けの CI サービスです。

bitrise.yml とは

GUI で簡単にワークフローを設定できる Bitrise ですが、 bitrise.yml という設定ファイルにワークフローなどの設定を記述することもできます。

bitrise.yml による設定は一般的ではない

Bitrise 公式やユーザコミュニティによるステップがあり、 そのステップを GUI で選択してワークフローを構築することが一般的です。 ステップの名称や入力項目は一度 GUI で設定してみないとわからないため、 いきなり bitrise.yml を直接記述することはなく、 あっても他のワークフローにステップをコピペするくらいです。

bitrise.yml は Bitrise にある

Bitrise のウェブサイトで設定するため bitrise.yml は Bitrise にあります。 他の CI サービスのように設定ファイルをリポジトリで管理しません。

bitrise.yml をバージョン管理したい

bitrise.yml をバージョン管理することで、 誰でも設定を変更しやすい環境を作り、CI/CD の改善が回るようにしたいです。

そのために以下の2つが必要でした。

  1. 設定を簡単に確実に元に戻せるようにする
  2. 設定変更をレビューできるようにする

設定を簡単に確実に元に戻せるようにする

GUI で簡単に設定できるのですが、変更して何か問題あり元に戻したい事があると思います。 ビルドごとにその時点の bitrise.yml が残っている(※)ので戻せないことはないのですが、 戻したいバージョンのビルドを特定して元に戻すのが少し面倒です。 バージョン管理されていれば元に戻すのは簡単です。

bitrise.ymlファイルへのオンラインアクセス - Bitrise Docs

設定変更をレビューできるようにする

以下の要因によりレビューできず、Bitrise おじさん化しやすい環境になると思います。

bitrise.yml をバージョン管理する方法

Bitrise 公式: リポジトリの bitrise.yml を読み込み実行する方法

リポジトリで管理したい場合、以下の方法で管理することができます。

Use bitrise.yml from repository - Bitrise Docs

しかし、この方法では以下の問題があります。

最後の問題はともかく、GUI で設定できないのは Bitrise のメリットが無くなってしまいます。 使ったことないステップは実質的に利用できないので改善にしくくなってしまいます。

Bitrise 公式の方法には問題がある。ではどうするか?

リポジトリの bitrise.yml を読み込み実行する方法は上記の問題があるので、 設定は Bitrise 上で行い、Bitrise 上の設定で実行して欲しいです。

Bitrise の設定をマスターとし、リポジトリの bitrise.yml は単なるコピーとします。 そうすれば Bitrise のメリットが無くならず、bitrise.yml をバージョン管理することができます。

以下で bitrise.yml をリポジトリで管理するための Bitrise ワークフローについて説明します。

bitrise.yml をリポジトリで管理する Bitrise ワークフロー

以下の3つのワークフローを構築します。

  1. pr-bitrise-yml: 定期的に bitrise.yml の差分をチェックして PR を送る
  2. no-op: 上記の PR で通常のワークフローが動作しないようにする
  3. merge-bitrise-yml: PR がマージされたらリポジトリの bitrise.yml を Bitrise に反映する

1. pr-bitrise-yml: 定期的に bitrise.yml の差分をチェックして PR を送る

下記のワークフローを日次など定期的に実行(※)します。

  1. リポジトリを clone する
  2. Bitrise から bitrise.yml を API でダウンロードする
  3. git で差分をチェックする
  4. 差分があれば PR を作成する

ビルドをスケジューリングする - Bitrise Docs

2. no-op: 上記の PR で通常のワークフローが動作しないようにする

通常、PR にコミットすると何かしらのワークフローが動作するように設定してあると思います。 そのため、何も実行しない no-op ワークフローを作成し、 上記の PR では no-op ワークフローが実行されるようにトリガーを設定します。

3. merge-bitrise-yml: PR がマージされたらリポジトリの bitrise.yml を Bitrise に反映する

上記の PR のターゲットブランチへの Push をトリガーに以下のワークフローを実行します。

  1. リポジトリを clone する
  2. コミットが上記の PR のブランチのマージコミットかチェックする
  3. 条件に一致する場合、リポジトリの bitrise.yml を API でアップロードする

ワークフロー全体イメージ

上記のワークフローの動作イメージは以下のようになります。 sync-bitrise-yml-workflow ja このフローを構築することによって bitrise.yml をリポジトリで管理することが可能となります。

Ref

Articles

wada811 commented 4 years ago

bitrise-yml-in-repository

wada811 commented 4 years ago

Bitrise workflow for managing bitrise.yml in the repository

What is Bitrise

Bitrise is a CI service for mobile platforms that allows you to easily configure workflows with a GUI.

What is bitrise.yml

Bitrise makes it easy to set up a workflow with a GUI. Settings such as workflow can also be described in a setting file called bitrise.yml.

Setting with bitrise.yml is not common

There are steps by Bitrise official and user community, It is common to build a workflow by selecting that step in the GUI. Because the name of the step and the input items are not known unless you set it once in the GUI, I do n’t write bitrise.yml directly, Even if it exists, it just copies the steps to other workflows.

bitrise.yml is in Bitrise

Bitrise.yml is in Bitrise for setting up on the Bitrise website. Does not manage configuration files in the repository like other CI services.

I want to version control bitrise.yml

By managing bitrise.yml version, I want to create an environment where anyone can easily change the settings so that CI / CD can be improved. The following two were necessary for that.

  1. Make sure settings can be easily and reliably restored
  2. Make configuration changes available for review

Make it easy and reliable to restore settings

It can be set easily with the GUI, but I think there is something wrong with it and I want to change it back. The bitrise.yml at that point remains for each build (*), so there is nothing that can not be restored, It’s a bit cumbersome to identify the version of the build you want to revert and revert. It’s easy to get it back if it’s versioned.

* Accessing the bitrise.yml file online — Bitrise Docs

Allow configuration changes to be reviewed

I can’t review due to the following factors, so I think Bitrise will be an easy-to-unform environment.

How to version control bitrise.yml

Bitrise Official: How to read and execute bitrise.yml in the repository

If you want to manage in the repository, you can manage it in the following way. Using the bitrise.yml from repository — Bitrise Docs However, this method has the following problems.

Regardless of the last problem, the benefits of Bitrise will be lost if it cannot be set in the GUI. Steps that you haven’t used aren’t practically usable and can be difficult to improve.

There is a problem with Bitrise official method. Then what should we do?

The method of reading and executing bitrise.yml in the repository has the above problem, I would like the settings to be done on Bitrise and executed with the settings on Bitrise. The Bitrise settings are the master, and the repository bitrise.yml is just a copy. Then you can manage the version of bitrise.yml without losing the benefits of Bitrise. The following describes the Bitrise workflow for managing bitrise.yml in the repository.

Bitrise workflow to manage bitrise.yml in the repository

Build the following three workflows:

  1. pr-bitrise-yml: Check the difference of bitrise.yml regularly and send PR
  2. no-op: Prevent normal workflow from working with the above PR
  3. merge-bitrise-yml: When PR is merged, the repository bitrise.yml is reflected in Bitrise

1. pr-bitrise-yml: Check the difference of bitrise.yml periodically and send PR

The following workflow is executed (*) periodically such as daily.

  1. Clone the repository
  2. Download bitrise.yml from Bitrise with API
  3. Check the difference with git
  4. Create a PR if there are differences

* Scheduling your builds — Bitrise Docs

2. no-op: Prevent normal workflow from working with the above PR

I think that some workflow is usually set to work when you commit to PR. So create a no-op workflow that does nothing, In the above PR, the trigger is set so that the no-op workflow is executed.

3. merge-bitrise-yml: When PR is merged, reflect bitrise.yml of repository to Bitrise

The following workflow is executed by pushing the above PR to the target branch as a trigger.

  1. Clone the repository
  2. Check if the commit is a merge commit of the above PR branch
  3. If the conditions match, upload the repository bitrise.yml with API

Overall workflow

The overall workflow is as follows.

sync-bitrise-yml-workflow en

By building this flow, you can manage bitrise.yml in the repository.

wada811 commented 4 years ago

PlantUML

ja

@startuml
|__Bitrise__|
start
:Bitrise による定期実行;
partition pr-bitrise-yml {
    :リポジトリをクローン;
    :Bitrise から bitrise.yml をダウンロード;
    if (差分があるか?) then (no)
        :差分がない場合は終了;
        end
    else (yes)
        :差分をコミット;
        if (PR が存在するか?) then (no)
            :PR 作成;
        else (yes)
        endif
    endif
}
|#AntiqueWhite|__GitHub__|
:レビュー;
while (マージ可能か?) is (no)
:修正をコミット;
fork
    |__Bitrise__|
partition no-op {
    :何も実行しない;
    stop
}
|__GitHub__|
fork again
:レビュー;
end fork
endwhile (yes)
:PRマージ;
|__Bitrise__|
partition merge-bitrise-yml {
    :リポジトリをクローン;
    if (pr-bitrise-yml で作成したブランチか?) then (yes)
        :Bitrise に bitrise.yml をアップロード;
        end
    else (no)
        :異なるブランチの場合は終了;
        end
    endif
}
@enduml

en

@startuml
|__Bitrise__|
start
:Schedule Build by Bitrise;
partition pr-bitrise-yml {
    :Clone repository;
    :Download bitrise.yml from Bitrise;
    if (Is there a difference?) then (no)
        :End if there is no difference;
        end
    else (yes)
        :Commit the difference;
        if (does PR exist?) then (no)
            :Create PR;
        else (yes)
        endif
    endif
}
|#AntiqueWhite|__GitHub__|
:Review;
while (Can merge?) is (no)
:Commit the fix;
fork
    |__Bitrise__|
    partition no-op {
        :Do nothing;
        end
    }
|__GitHub__|
fork again
:Review;
end fork
endwhile (yes)
:Merge PR;
|__Bitrise__|
partition merge-bitrise-yml {
    :Clone repository;
    if (A branch created with pr-bitrise-yml?) then (yes)
        :Upload bitrise.yml to Bitrise;
        stop
    else (no)
        :End if different branch;
        end
    endif
}
@enduml