■ はじめに
https://dk521123.hatenablog.com/entry/2024/06/27/220219
https://dk521123.hatenablog.com/entry/2024/06/28/030619
の続き。 今回は、Github Custom Action の 複合アクション(composite action) について扱う。
目次
【1】複合アクション(composite action) 【2】使用上の注意 【3】サンプル 0)前提条件 1)リポジトリの作成 2)action.ymlの作成 3)Bashの作成 4)Github リポジトリの設定 5)GitHub Actionsでのテスト
【1】複合アクション(composite action)
* 複数 step からなる処理を行える Action
【2】使用上の注意
* 調べてみると結構ハマっている方が多いらしい、、、
https://zenn.dev/century/articles/29ffe69be90678
https://thaim-til.hatenablog.jp/entry/2024/04/09/084654
https://qiita.com/M_Kagawa/items/98f154477b8b5bd3d315
* inputsのrequired:trueに強制力はない * Bashを実行する場合「shell: bash」は必須 * boolean inputsがstringとして扱われる などなど
【3】サンプル
0)前提条件
* 以下がインストールされていること + git
1)リポジトリの作成
$ mkdir hello-world-composite-action $ cd hello-world-composite-action # git 初期化 $ git init
最終的な主なフォルダ構成
hello-world-composite-action ├─.github │ └─workflows │ └─ demo.yml ├─action.yml └─goodbye.sh
2)action.ymlの作成
* 詳細な構文は、以下の公式ドキュメントを参照のこと
action.yml
name:'hello_world_composite_demo'description:'Greet someone'inputs:who-to-greet: # id of inputdescription:'Who to greet'required:truedefault:'World'outputs:random-number:description:"Random number"value: ${{ steps.random-number-generator.outputs.random-number }} runs: # ★注目★using:"composite"steps:- name: Set Greeting run: echo "Hello $INPUT_WHO_TO_GREET."shell: bash env:INPUT_WHO_TO_GREET: ${{ inputs.who-to-greet }} - name: Random Number Generator id: random-number-generator run: echo "random-number=$(echo $RANDOM)">> $GITHUB_OUTPUT shell: bash - name: Set GitHub Path run: echo "$GITHUB_ACTION_PATH">> $GITHUB_PATH shell: bash env:GITHUB_ACTION_PATH: ${{ github.action_path }} - name: Run goodbye.sh run: goodbye.sh shell: bash
3)Bashの作成
[1] Bashの作成
# Step1: Bash作成 $ echo"echo Goodbye"> goodbye.sh # Step2: 実行権限付与 $ chmod+x goodbye.sh
4)Githubリポジトリの設定
# Step1: githubで新たにrepositoryでリポジトリ作成# => <Your-ID>@hello-world-composite-action# Step2: git push $ git add . $ git commit -m"Add goodbye script" $ git branch -M develop $ git remote add origin https://github.com/dk521123/hello-world-composite-action.git $ git push -u origin develop # Step3: ハッシュタグ取得 $ git show --format='%H'--no-patch 3b3f59d...
5)GitHub Actionsでのテスト
[1] .github/workflows/demo.yml 作成
name: DemoCustomCompositeAction on:workflow_dispatch:jobs:hello_world_job:runs-on: ubuntu-latest name: A job to say hello steps:- uses: actions/checkout@v4 - id: foo # ★ここを修正する(@以降は、ハッシュタグを記載)uses: your-github-user-name/hello-world-composite-action@3b3f59d... with:who-to-greet:'Mona the Octocat'- run: echo random-number "$RANDOM_NUMBER"shell: bash env:RANDOM_NUMBER: ${{ steps.foo.outputs.random-number }}
[2] git push
$ git add . $ git commit -m"Add demo github Workflow" $ git push
[3] Workflow 実行
* 実行したら、以下のような出力がされるはず、、、 ~~~~ Hello Mona the Octocat. Goodbye Run echo random-number "$RANDOM_NUMBER" random-number 8836 ~~~~
参考文献
公式ドキュメント
https://docs.github.com/ja/actions/creating-actions/creating-a-composite-action
一般サイト
https://dev.classmethod.jp/articles/github_actions_composite_action_first_touch/
https://zenn.dev/moaikids/articles/32363c5386978e
https://www.ritolab.com/posts/212
関連記事
Github~ 入門編 ~
https://dk521123.hatenablog.com/entry/2019/07/18/234652
Github Actions ~ 基礎知識編 ~
https://dk521123.hatenablog.com/entry/2021/11/04/142835
Github Actions ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2022/06/16/151443
Github Actions ~ 基本編 ~
https://dk521123.hatenablog.com/entry/2023/12/22/195715
Github Custom Action ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2024/06/27/220219
Github Custom Action ~ Docker コンテナー ~
https://dk521123.hatenablog.com/entry/2024/06/28/030619