Flutter アプリを Bitrise で DeployGate に自動アップロード

プライベートで Flutter を触り始めたので、DeployGate へのアプリデプロイ自動化を試してみた備忘録です。まだアプリの規模が小さいのでとりあえず Bitrise 無料枠の10分以内にはおさまっています。:relaxed:

# iOS

各種設定

- Stack
  - `Xcode 10.0.x, on macOS 10.13 (High Sierra)`
- Code Signing
  - `PROVISIONING PROFILE`
  - `CODE SIGNING IDENTITY`
- Secret Environment Variables
  - `DEPLOYGATE_API_KEY` :  DeployGate の API key
  - `DEPLOYGATE_USER` : DeployGate のユーザ名


## Workflows

<img width="380" alt="workflow_ios.png" src="https://qiita-image-store.s3.amazonaws.com/0/121719/4ed00b5c-66a5-084c-2598-6a1c04317341.png">


最初の 3 Step については特にコメントなし。

- Flutter
  - Flutter version : 自分の開発環境に合わせて `0.8.2-beta` にした
  - Flutter commands to be executed : `build ios --release`
- Convert app to ipa (Script)
  - `flutter build ios --release` で生成されるのは .app なので、zip コマンドで .ipa に変換する

```bash
#!/usr/bin/env bash
# fail if any commands fails
set -e
# debug log
set -x

cd build/ios/iphoneos
mkdir -p Payload
mv Runner.app Payload/
zip -ry Runner.ipa Payload

envman add --key FLUTTER_IPA_PATH --value "${BITRISE_SOURCE_DIR}/build/ios/iphoneos/Runner.ipa"
```

- DeployGate Upload
  - API Key : Secret Environment Variables で設定済みの `$DEPLOYGATE_API_KEY` 
  - Owner name : Secret Environment Variables で設定済みの `$DEPLOYGATE_USER`
  -  App file path : 前の Step で設定した `$FLUTTER_IPA_PATH`
  -  Short Message : とりあえずコミットメッセージをいれてみた `$GIT_CLONE_COMMIT_MESSAGE_SUBJECT`

## bitrise.yml

```yml:bitrise.yml
---
format_version: '6'
default_step_lib_source: https://github.com/bitrise-io/bitrise-steplib.git
project_type: other
trigger_map:
- push_branch: master
  workflow: deploy
workflows:
  deploy:
    steps:
    - certificate-and-profile-installer@1.10.1: {}
    - activate-ssh-key@4.0.3:
        run_if: '{{getenv "SSH_RSA_PRIVATE_KEY" | ne ""}}'
    - git-clone@4.0.12: {}
    - flutter@0.0.5:
        inputs:
        - commands: build ios --release
        - version: 0.8.2-beta
    - script@1.1.5:
        inputs:
        - content: |-
            #!/usr/bin/env bash
            # fail if any commands fails
            set -e
            # debug log
            set -x

            cd build/ios/iphoneos
            mkdir -p Payload
            mv Runner.app Payload/
            zip -ry Runner.ipa Payload

            envman add --key FLUTTER_IPA_PATH --value "${BITRISE_SOURCE_DIR}/build/ios/iphoneos/Runner.ipa"
        title: Convert app to ipa
    - deploygate--upload-app-bitrise-step@1.0.1:
        inputs:
        - owner_name: "$DEPLOYGATE_USER"
        - message: "$GIT_CLONE_COMMIT_MESSAGE_SUBJECT"
        - app_path: "$FLUTTER_IPA_PATH"
        - api_key: "$DEPLOYGATE_API_KEY"
```

## 実行時間

```
+------------------------------------------------------------------------------+
|                               bitrise summary                                |
+---+---------------------------------------------------------------+----------+
|   | title                                                         | time (s) |
+---+---------------------------------------------------------------+----------+
| ✓ | certificate-and-profile-installer@1.10.1                      | 10 sec   |
+---+---------------------------------------------------------------+----------+
| ✓ | activate-ssh-key@4.0.3                                        | 4.57 sec |
+---+---------------------------------------------------------------+----------+
| ✓ | git-clone@4.0.12                                              | 41 sec   |
+---+---------------------------------------------------------------+----------+
| ✓ | flutter@0.0.5                                                 | 202 sec  |
+---+---------------------------------------------------------------+----------+
| ✓ | Convert app to ipa                                            | 7.95 sec |
+---+---------------------------------------------------------------+----------+
| ✓ | deploygate--upload-app-bitrise-step@1.0.1                     | 13 sec   |
+---+---------------------------------------------------------------+----------+
| Total runtime: 279 sec                                                       |
+------------------------------------------------------------------------------+
```
4m 39s :thumbsup: 

# Android

各種設定

- Stack
  - `Android & Docker, on Ubuntu 18.04`
- Code Signing
  - `ANDROID KEYSTORE FILE` を必要に応じてよしなに登録。登録して署名しないとアプリ更新のたびにアンインストールする必要がある。
- Secret Environment Variables
  - `DEPLOYGATE_API_KEY` :  DeployGate の API key
  - `DEPLOYGATE_USER` : DeployGate のユーザ名

## Workflows

<img width="380" alt="workflow_android.png" src="https://qiita-image-store.s3.amazonaws.com/0/121719/913eb90d-6c4c-02a6-a183-abb23d864c4f.png">

iOS との差分だけ。

- Flutter
  - Flutter commands to be executed : `build apk --release`
- Sign APK
  - apk path : `build/app/outputs/apk/release/app-release.apk`
- DeployGate Upload
  - App file path : `$BITRISE_SIGNED_APK_PATH`

## bitrise.yml

```yml:bitrise.yml
---
format_version: '6'
default_step_lib_source: https://github.com/bitrise-io/bitrise-steplib.git
project_type: fastlane
trigger_map:
- push_branch: master
  workflow: deploy
workflows:
  deploy:
    steps:
    - activate-ssh-key@4.0.3:
        run_if: '{{getenv "SSH_RSA_PRIVATE_KEY" | ne ""}}'
    - git-clone@4.0.12: {}
    - flutter@0.0.5:
        inputs:
        - commands: build apk --release
        - version: 0.8.2-beta
    - sign-apk@1.2.3:
        inputs:
        - apk_path: build/app/outputs/apk/release/app-release.apk
    - deploygate--upload-app-bitrise-step@1.0.1:
        inputs:
        - owner_name: "$DEPLOYGATE_USER"
        - app_path: "$BITRISE_SIGNED_APK_PATH"
        - message: "$GIT_CLONE_COMMIT_MESSAGE_SUBJECT"
        - api_key: "$DEPLOYGATE_API_KEY"
```

## 実行時間

```
+------------------------------------------------------------------------------+
|                               bitrise summary                                |
+---+---------------------------------------------------------------+----------+
|   | title                                                         | time (s) |
+---+---------------------------------------------------------------+----------+
| ✓ | activate-ssh-key@4.0.3                                        | 18 sec   |
+---+---------------------------------------------------------------+----------+
| ✓ | git-clone@4.0.12                                              | 40 sec   |
+---+---------------------------------------------------------------+----------+
| ✓ | flutter@0.0.5                                                 | 301 sec  |
+---+---------------------------------------------------------------+----------+
| ✓ | sign-apk@1.2.3                                                | 7.25 sec |
+---+---------------------------------------------------------------+----------+
| ✓ | deploygate--upload-app-bitrise-step@1.0.1                     | 10 sec   |
+---+---------------------------------------------------------------+----------+
| Total runtime: 376 sec                                                       |
+------------------------------------------------------------------------------+
```

6m 16s :thumbsup: 

# まとめ

最初は試行錯誤して Flutter を git clone したり、fastlane や curl コマンドで deploy してみたりしましたが、はじめから用意されている step を利用するだけで簡単に実現できました。