[Advent Calendar 2021][Day20]CDパイプラインを作る

はじめに
Advent Calendar 2021のDay20。 CDパイプラインを作っていきます。
CDパイプラインを構築する
CloudRunにデプロイしたいのでArtifactRegistryにログインしておきます。
- .github/workflows/ci.yml
- name: Login to GAR uses: docker/login-action@v1 with: registry: asia-northeast1-docker.pkg.dev username: _json_key password: ${{ secrets.GAR_JSON_KEY }}
build-push-actionのステップでArtifactRegistryのTagを足しておきます。
参考
https://github.com/docker/login-action
CDのパイプライン定義を書きます。デプロイは、マニュアルにしています。workload_identity_providerとservice_accountはハードコードしちゃっていますが、ここだけServiceAccountの鍵を発行せずに、WorkloadIdentityという機能を使って、GithubActionsにアクセス権を与えることが出来ます。鍵を取り扱わないので、安全性が高まると思います。CloudRunのデプロイ部分は、環境変数やもろもろ明示したほうがよいですが、指定しない場合はすでにデプロイされている設定が保持されます。
- .github/workflows/cd.yml
name: CDon: workflow_dispatch: inputs: tag: description: 'container image tag' required: true default: latestjobs: deploy: name: Deploy runs-on: ubuntu-latest permissions: contents: 'read' id-token: 'write' steps: - id: 'auth' uses: 'google-github-actions/auth@v0' with: workload_identity_provider: 'projects/836760468639/locations/global/workloadIdentityPools/github-actions/providers/gha-provider' service_account: '836760468639-compute@developer.gserviceaccount.com' project_id: cloudandbuild-advent-calendar - name: 'Deploy Hasura to Cloud Run' uses: 'google-github-actions/deploy-cloudrun@v0' with: image: asia-northeast1-docker.pkg.dev/cloudandbuild-advent-calendar/advent-calendar/hasura:${{ github.event.inputs.tag }} service: 'hasura-graphql' region: 'asia-northeast1' - name: 'Deploy Nuxt to Cloud Run' uses: 'google-github-actions/deploy-cloudrun@v0' with: image: asia-northeast1-docker.pkg.dev/cloudandbuild-advent-calendar/advent-calendar/nuxt3-app:${{ github.event.inputs.tag }} service: 'nuxt3-app' region: 'asia-northeast1'
参考
https://cloud.google.com/iam/docs/workload-identity-federation
https://github.com/google-github-actions/auth
https://github.com/google-github-actions/deploy-cloudrun
その他
CIは、GitHub Container Registryを使っていましたが、CloudRunをデプロイするときは、もともと作っていたArtifactRegistryを使っていたので、ImageのRegistryを2重となっていますが、ArtifatRegistryに寄せてもよかったかなと思います。
まとめ
今回は、CDパイプラインを構築しました。 次回は、カレンダーの更新削除を実装していきます。
