3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

デフォルトブランチに存在しないGitHub Actionsのワークフローをworkflow_dispatchで実行する方法

Last updated at Posted at 2024-02-15

workflow_dispatchを使用したワークフロー作成した際、defaultブランチにマージする前に動作確認したいケースがあると思います。
しかし、ワークフローは一度実行しないと以下に一覧としてワークフローが表示されません。
image.png

また、一度実行したとしても、defaultブランチにそのワークフローが存在しない場合、
以下の画面からworkflow_dispatchによるワークフロー実行ができません。

image.png

解決策

以下の手順でその問題を解決できます。

  • 適当に処理が終了するワークフローをon pushで作成
  • ワークフローを修正し、実行したい処理を記載する
    (on pushは不要であれば削除してok)
  • APIでワークフローのidを取得
  • 取得したIDを指定し、APIを実行しワークフロー実行

詳細

  • 適当に処理が終了するワークフローをon pushで作成
    Workflow IDを取得するために行います。
    一度ワークフローを実行しないとIDを取得できないので実行します。
    (Branch名: workflow-test)
name: Test workflow_dispatch

on:
  push:
  workflow_dispatch:

jobs:
  Job1:
    runs-on: ubuntu-latest
    steps:
    - name: Step1
      run: |
        echo Hello
  • ワークフローを修正し、実行したい処理を記載する
    今回は適当に修正します。
name: Test workflow_dispatch

on:
  workflow_dispatch:
    inputs:
      input1:
        type: string
        required: true
        default: 'Value1'
      input2:
        type: string
        required: true
        default: 'Value2'

jobs:
  Job1:
    runs-on: ubuntu-latest
    steps:
    - name: Step1
      run: |
        echo ${{ inputs.input1 }}
        echo ${{ inputs.input2 }}
  • APIでワークフローのidを取得
$ curl -H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token ${YOUR_PERSONAL_ACCESS_TOKEN}" \
https://api.github.com/repos/${OWNER}/${REPO_NAME}/actions/workflows
{
  "total_count": 1,
  "workflows": [
    {
      "id": 99999999,
      :
      :
      (省略)
      :
      :
    }
  ]
}

  • 取得したIDを指定し、APIを実行しワークフロー実行
    workflow-testブランチのworkflowを実行
curl -X POST \
  -H "Accept: application/vnd.github.v3+json" \
  -H "Authorization: token {YOUR_PERSONAL_ACCESS_TOKEN}" \
  https://api.github.com/repos/{OWNER}/{REPO}/actions/workflows/{WORKFLOW_ID}/dispatches \
  -d '{
    "ref": "workflow-test"
}'

無事実行されました。
image.png

inputを渡したいとき

curl -X POST \
  -H "Accept: application/vnd.github.v3+json" \
  -H "Authorization: token {YOUR_PERSONAL_ACCESS_TOKEN}" \
  https://api.github.com/repos/{OWNER}/{REPO}/actions/workflows/{WORKFLOW_ID}/dispatches \
  -d '{
    "ref": "workflow-test",
    "inputs": {"input1": "aaa", "input2": "bbb"}
}'

問題なく値渡せてますね。
image.png

3
0
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
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?