LoginSignup
4
1

More than 3 years have passed since last update.

Github Actions で手動で job を実行してデプロイをする方法

Posted at

概要

例えば master ブランチへのマージをトリガーにデプロイジョブを動かすことは簡単にできますが、特定のブランチに対して手動でデプロイジョブを実行したいときには一工夫必要なのでやり方を共有です。

コード

手動でジョブを実行するときに、何をトリガーにするかですが、 curl で repository_dispatch イベントを実行させることでジョブを実行させます。

任意のブランチにチェックアウトするために client_payload に target_brunch キーを作成しています。

curl -vv \
-H "Authorization: token パーソナルトークン" \
-H "Accept: application/vnd.github.everest-preview+json" \
"https://api.github.com/repos/オーナー名/リポジトリ名/dispatches" \
-d '{"event_type": "build", "client_payload": {"target_brunch": "feature_branch"}}'

以下、action の yml です。
on で repository_dispatch を指定します。
また、手動実行用のジョブを用意し、それ以外のジョブでは repository_dispatch イベントで発火させないようにします。

ブランチのチェックアウトのために、github.event.client_payload.target_brunch で curl で送ったブランチ名を参照しています。

on:
  push: # master にマージされたときにトリガーされる用
    branches:
      - master
  repository_dispatch:
    types: [build]

jobs:
  manual_deploy:
    runs-on: ubuntu-latest
    if: github.event_name == 'repository_dispatch'
    steps:
      - name: Execute git checkout
        uses: actions/checkout@v2
        with:
          ref: ${{ github.event.client_payload.target_brunch }}
    # 任意の処理を続けてください

normal_deploy:
    runs-on: ubuntu-latest
    if: github.event_name == 'push'
    steps:
      - name: Execute git checkout
        uses: actions/checkout@v2
    # 任意の処理を続けてください

下記記事を大変参考にさせてもらいました。

Github Actions を API から実行する
https://qiita.com/okitan/items/88994a36c996f2397a07

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1