人気記事

CI/CD GitHub RaspberryPi

GitHub Actionsのworkflow_dispatchでトリガーと同時に任意のパラメータを設定する方法

投稿日:

以前、GitHub Actionsのself-hosted runnerをラズパイにインストールしてリモートで処理を実行させました。

GitHub self-hosted runnerでリモートのラズパイ上で処理を実行させる

その際は、トリガーを掛けるのみで、パラメータを送ってはいませんでした。

今回はトリガーと同時に任意のパラメータを送ることを試してみます。

スポンサーリンク

公式

GitHub Actionsの公式ページは以下です。

https://docs.github.com/ja/actions/creating-actions/metadata-syntax-for-github-actions#inputs

ソースコード

トリガー側

まずはトリガー側のソースコードです。

パラメータとして

キー名:my_hello_input、バリュー名:hogehoge

を送るサンプルです。

import requests
import json

url = "https://api.github.com/repos/S-S-T-Y/github-actions/actions/workflows/raspberrypi-test.yml/dispatches"

payload = json.dumps({
    "ref":"main",
    "inputs": { "my_hello_input": "hogehoge" }
})
headers = {
  'Authorization': 'Bearer ghp_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
  'Content-Type': 'application/json'
}

response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)

ワークフロー側

次に、GitHub Actionsのワークフローを以下としました。

inputsにパラメータ名を定義することで、Jobsの中で ${{ github.event.inputs.my_hello_input }}  と記述してパラメータを取り出すことができます。

name: Raspberrypi-github-actions

on:
  workflow_dispatch:
    inputs:
         my_hello_input:
           description: 'hello input'
           required: true
           default: 'this is a default hello input'

jobs:
  raspberrypi-test:
    runs-on: [self-hosted, linux, arm]
    steps:
      - name: Run a one-line script
        run: echo Hello, world! > /home/pi/helloworld.txt
      - run: echo "hello, ${{ github.event.inputs.my_hello_input }}"

動作確認

では動作確認してみます。

python githubactions_trigger.py

GitHubのActionsからワークフローの動作状況を見ると、パラメータとして設定した"hogehoge"が出力していることを確認できました。

まとめ

GitHub Actionsのトリガーと同時に任意のパラメータを送れることを確認しました。

パラメータを設定できることで、リモートから任意のタイミングでLEDを点灯させたり、画像を取得したりとできる範囲が広がりそうです。

-CI/CD, GitHub, RaspberryPi

Copyright© ITエンジニアへの転身 , 2025 All Rights Reserved Powered by STINGER.