Madogiwa Blog

主に技術系の学習メモに使っていきます。

HerokuにGitHub ActionでImage Buildしてデプロイする

Herokuにはheroku.ymlを使って簡単にpush時に自動デプロイする機能がありますが、

devcenter.heroku.com

簡単にできる反面、以下のようにRuntimeで設定される環境変数は利用できず、動的な値や秘匿値をセキュアにビルド時に設定するようなことはできません。

Variables set in this section don’t create runtime config vars. Also runtime config vars, such as those config vars set with heroku config:set, aren’t available at build time. Building Docker Images with heroku.yml | Heroku Dev Center

そのようなことをやりたい時にはContainer Registry & Runtime (Docker Deploys)を使うことで自身でDockerfileをビルドしてデプロイすることができます。

devcenter.heroku.com

以下がGitHub Actionでデプロイするようにしてみたworkflowです。

name: Deploy

on:
  workflow_dispatch:
  push:
    branches: [master]

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

env:
  SOURCE_VERSION: ${{ github.sha }}

jobs:
  deploy:
    timeout-minutes: 20
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Build, Push and Release a Docker container to Heroku. 
        uses: gonuit/heroku-docker-deploy@v1.3.3
        with:
          email: your.email@example.com
          heroku_api_key: ${{ secrets.HEROKU_API_KEY }}
          heroku_app_name: your_app_name
          docker_options: "--build-arg SOURCE_VERSION=${{ env.SOURCE_VERSION }}"
          process_type: web

gonuit/heroku-docker-deployを使うと簡単に実現できますね、ありがたや🙏✨

github.com

参考

qiita.com