食べログ アプリDevOpsチームの @maguhiro です。
Advent Calendar 17日目の記事では、Microsoft Teamsへのメッセージ送信を行えるBitrise Stepを作った際のノウハウを共有したいと思います。
はじめに
食べログのスマホアプリ開発ではCI/CDツールとしてJenkinsを利用していました。
しかしながら、以下のような理由から他のCI/CDツール・サービスへの移行を決断しました。
- 定期的なビル停電に伴い利用不可となるタイミングが発生
- Xcodeのバージョンアップに伴うメンテナンス対応コスト
- Jenkins運用の属人化
- 他ビルドへの副作用を及ばさないようなジョブ設計
- CI/CDの利用拡大に伴うジョブキュー滞留
これらの問題を解決するためBitriseを選択しました。
CircleCIも候補に挙がりましたが、Xcodeのバージョンアップ対応の迅速さからBitriseを選定しました。
Bitriseのワークフロー(Jenkinsで言うところのジョブ)はステップという処理単位の連なりで構成されます。
ワークフローのステップはGUIで簡単に追加・削除・順序変更が行えます。
ステップの一例としてはGit Clone, fastlaneのlane実行, TestFlightへのデプロイ, Slackへのメッセージ送信などがあります。
これらのステップはOSSとして公開され、個人でも開発することができます。
食べログではチャットツールとしてMicrosoft Teams を利用しているのですが、Microsoft Teamsへの柔軟なメッセージ送信が可能なBitrise Stepが存在していなかったので今回自分で作成してみることにしました。
※Bitrise StepはGo(golang)またはbashで実装することができるため、今回は初めてGoを使って開発してみました。
作成するにあたって
以下のページやスライドを参考にさせて頂きました。
- Microsoft Teams Incoming Webhook
- Bitrise Step作成
- Bitriseオフィシャル - Creating and sharing your own Step
- Bitriseオフィシャル - Steps in YAML
- Bitrise Stepの作り方 by @wakwak3125さん
-
steps-slack-message
- Slackへのメッセージ送信用Bitrise Step
- 今回はこちらの実装を写経しながらGoの書き方を把握し、Teams用にカスタマイズ実装させて頂きました
作成手順
1. Bitrise CLIのインストール・セットアップ
Homebrew経由でのインストール手順となります。
$ brew update
$ brew install bitrise
$ bitrise setup
$ bitrise plugin update step
2. Stepの新規作成
以下のコマンドを叩くと対話式に入力を行い、ステップの作成をすることができます。
入力項目
- 作者
- ステップ名
- サマリー
- ステップの詳細内容
- カテゴリ
- 実装する言語
- Github上でリポジトリを作成するユーザー or 組織名
$ brew :step create
Who are you / who's the author? [maguhiro] : maguhiro
What's the title / name of the Step? : Send Microsoft Teams message
Generated Step ID (from provided Title): send-microsoft-teams-message
Please provide a summary : Send Microsoft Teams message to a channel
Please provide a description : Send Microsoft Teams message to a channel
What's the primary category of this Step?
Please select from the list:
[1] : access-control
[2] : artifact-info
[3] : installer
[4] : deploy
[5] : utility
[6] : dependency
[7] : code-sign
[8] : build
[9] : test
[10] : notification
(type in the option's number, then hit Enter) : 10
Toolkit: the entry/base language of the Step.
Our recommendation is to use Bash for very simple Steps
and for more complex ones use another language, one which we have toolkit support for.
If you're just getting started with Step development our suggestion is to select Bash,
as that's the easiest option. It's possible to convert the step later, if needed.
Note: Of course even if you select e.g. Bash as the entry language, you can run other scripts from there,
so it's possible to write the majority of the step's code in e.g. Ruby,
and have an entry Bash script which does nothing else except running the Ruby script.
Which toolkit (language) would you like to use?
Please select from the list:
[1] : bash
[2] : go
(type in the option's number, then hit Enter) : 2
Website & source code URL:
Will you host the source code on GitHub? [YES/no]: YES
What's your GitHub username (user/org where you'll register the step's repository)? : maguhiro
We'll use https://github.com/maguhiro/bitrise-step-send-microsoft-teams-message as the website/repo URL for this step.
Please when you create the repository on GitHub for the step
create it under the user/org: maguhiro
and the name of the repository should be: bitrise-step-send-microsoft-teams-message
Creating Step directory at: /Users/maguhiro/go/src/github.com/maguhiro/bitrise-step-send-microsoft-teams-message
* [OK] created: /Users/maguhiro/go/src/github.com/maguhiro/bitrise-step-send-microsoft-teams-message/README.md
* [OK] created: /Users/maguhiro/go/src/github.com/maguhiro/bitrise-step-send-microsoft-teams-message/LICENSE
* [OK] created: /Users/maguhiro/go/src/github.com/maguhiro/bitrise-step-send-microsoft-teams-message/.gitignore
* [OK] created: /Users/maguhiro/go/src/github.com/maguhiro/bitrise-step-send-microsoft-teams-message/step.yml
* [OK] created: /Users/maguhiro/go/src/github.com/maguhiro/bitrise-step-send-microsoft-teams-message/bitrise.yml
* [OK] created: /Users/maguhiro/go/src/github.com/maguhiro/bitrise-step-send-microsoft-teams-message/.bitrise.secrets.yml
* [OK] created: /Users/maguhiro/go/src/github.com/maguhiro/bitrise-step-send-microsoft-teams-message/main.go
Initializing git repository in step directory ...
$ git "init"
$ git "remote" "add" "origin" "https://github.com/maguhiro/bitrise-step-send-microsoft-teams-message"
Step is ready!
You can find it at: /Users/maguhiro/go/src/github.com/maguhiro/bitrise-step-send-microsoft-teams-message
TIP: cd into /Users/maguhiro/go/src/github.com/maguhiro/bitrise-step-send-microsoft-teams-message and run bitrise run test for a quick test drive!
3. Goの依存関係管理ツールdepのインストール
BitriseではGoで簡単にStepが開発できるようにライブラリを公開しています。
そのライブラリを利用できるように依存関係管理ツールdepを今回はインストールします。
$ brew install dep
$ cd /Users/maguhiro/go/src/github.com/maguhiro/bitrise-step-send-microsoft-teams-message
$ dep init # Gopkg.lockファイル, Gopkg,tomlファイル, vendorディレクトリが作成されます
$ vim Gopkg.toml # 依存関係を記述し、保存してください
$ dep ensure # Gopkg.lock, vendor配下が更新される
※本来vendor配下はGit管理不要かもしれませんが、Bitrise Stepではvendor配下もGit管理してください。
4. Input&Outputの定義
先程自動生成されたファイル step.yml
を編集することになります。
step.yml
の書き方は こちら を参考にしてください。
このymlファイルで設定したInputがステップの入力フォームとなり、Outputが後続ステップで利用可能となる環境変数を表します。
5. 実装
先程自動生成されたファイル main.go
を編集することになります。
Input情報を元に処理したい内容をこのファイルに記載してください。
※もちろん必要に応じてファイルを分割しても構いません。
6. テスト
以下のコマンドを実行します。
$ bitrise run test
このコマンドは先程自動生成されたファイルbitrise.yml
のworkflows の testワークフローの記述内容を元に処理を実行します。
Inputの定義変更に合わせてこちらのファイルのtestワークフローの処理内容も変更してからコマンドを実行してください。
ちなみに私が今回作成したコマンドは先行ステップが全て成功した場合、いずれかのステップが失敗した場合に合わせてテストワークフローを2つに分けております。
なので、今回私が作成したステップの場合には、テスト実行コマンドは以下のようになります。
$ bitrise run success-test
$ bitrise run fail-test
7. 共有
今回作成したステップをBitrise Workflow上で利用できるようにするためにはBitriseの審査が必要となります。
審査に出すまでの手順は以下の通りです。
- https://github.com/bitrise-io/bitrise-steplib をforkする
- 新規作成したStepリポジトリでバージョンに対応するタグを切る
※Stepバージョンが【0.0.1】であれば、タグ【0.0.1】を作成してください - 以下のコマンドを実行
$ bitrise run share-this-step
-
https://github.com/bitrise-io/bitrise-steplib に対して forkしたリポジトリ(ブランチ)からPRを出す
※PRがマージされるとBitrise Workflow上で利用できるようになります
使い方
現在、PRを投げております。
https://github.com/bitrise-io/bitrise-steplib/pull/1826
こちらがマージされるまでBitrise Workflow設定画面からは利用できません。
マージされましたら、こちらは更新致します。
最後に
今回開発したBitrise StepをOSSとして公開しました。
MS Teamsを利用されている方は利用して頂けると幸いです。
明日の18日目は@zettaittenaniさんより「Rails の session を完全に理解した」です。
お楽しみに!