kakakakakku blog

Weekly Tech Blog: Keep Learning!

Branch Deploy Action で Draft なプルリクエストでもデプロイを許可する draft_permitted_targets オプション

はじめに

Branch Deploy Action (branch-deploy) で Draft なプルリクエストで .noop や .deploy をコメントするとデフォルトではエラーになってデプロイが進まないようになっている.以下のキャプチャは Terraform の plan / apply を .noop と .deploy に割り当てて Branch Deploy Action (branch-deploy) を使っているサンプルになっている👌

⚠️ Cannot proceed with deployment
Your pull request is in a draft state

とは言え Draft なプルリクエストで terraform plan を実行したいという場面もある.Branch Deploy Action (branch-deploy) には draft_permitted_targets というオプションがあって,Draft なプルリクエストでもデプロイを許可することができる.

Optional environments which can allow "draft" pull requests to be deployed. By default, this input option is empty and no environments allow deployments sourced from a pull request in a "draft" state. Examples: "development,staging"

github.com

環境

  • Branch Deploy Action (branch-deploy) v12.0.0

環境 (environment) 指定なしの場合

Branch Deploy Action (branch-deploy) を使った GitHub Actions ワークフローのサンプルを抜粋して載せる.まず,Branch Deploy Action (branch-deploy) で環境 (environment) を指定せずに使う場合はデフォルトで production になるため draft_permitted_targets: "production" と実装すれば OK👌

jobs:
  deploy:
    name: deploy
    runs-on: ubuntu-latest
    env:
      GITHUB_TOKEN: ${{ github.token }}
      GH_TOKEN: ${{ github.token }}
    if: ${{ github.event.issue.pull_request }}

    steps:
      - name: branch-deploy
        id: branch-deploy
        uses: github/branch-deploy@f0984057ffe1aa340aa017b6eafb894310aed88e # v12.0.0
        with:
          noop_trigger: ".plan"
          trigger: ".apply"
          admins: kakakakakku
          draft_permitted_targets: "production"

      #
      # 省略
      #

すると Draft なプルリクエストでも .plan と .apply を実行できる🎉

environment 指定ありの場合

environment_targets オプションを使って stg 環境・prd 環境のように環境 (environment) を指定している場合はカンマ区切りで draft_permitted_targets: "prd,stg" と実装すれば OK👌

jobs:
  deploy:
    name: deploy
    runs-on: ubuntu-latest
    env:
      GITHUB_TOKEN: ${{ github.token }}
      GH_TOKEN: ${{ github.token }}
    if: ${{ github.event.issue.pull_request }}

    steps:
      - name: branch-deploy
        id: branch-deploy
        uses: github/branch-deploy@f0984057ffe1aa340aa017b6eafb894310aed88e # v12.0.0
        with:
          noop_trigger: ".plan"
          trigger: ".apply"
          environment_targets: stg,prd
          disable_naked_commands: true
          admins: kakakakakku
          draft_permitted_targets: "prd,stg"

      #
      # 省略
      #

同じく Draft なプルリクエストでも .plan と .apply を実行できる🎉

まとめ

Branch Deploy Action (branch-deploy) で Draft なプルリクエストでもデプロイを許可する場合は draft_permitted_targets オプションを設定しよう❗️という小ネタでした \( 'ω')/

関連記事

kakakakakku.hatenablog.com kakakakakku.hatenablog.com kakakakakku.hatenablog.com