My Note

自己理解のためのブログ

GitHub-Actionsのworkflowについてまとめる

今回はgithub-actionsのworkflowについてです。最近、workflowを書く機会が多く一度、整理するために書いていきます。

workflow実行時に選択式で値を渡す

name: test
on:
  workflow_dispatch:
    inputs:
      env:
        type: choice
        description: "envを選択"
        required: true
        options:
          - dev
          - stg
          - prd

workflow実行時に入力式で値を渡す

name: test
on:
  workflow_dispatch:
    inputs:
      env:
        description: "envを入力"
        required: true

permissions:
  id-token: write
  contents: write

jobs:
  test:
    name: "sample"
    runs-on: ubuntu-22.04
    timeout-minutes: 3

    steps:
      - name: Show ENV
        run: |
          echo "ENV=${{ github.event.inputs.env }}"

三項演算子ライクに値を設定する

  • inputsのenvが prd なら secrets.PRD を設定し
  • inputsのenvが stg なら secrets.STG を設定し
  • prdでもstgでもなければ、secrets.DEV を設定する
name: test
on:
  workflow_dispatch:
    inputs:
      env:
        type: choice
        description: "envを選択"
        required: true
        options:
          - dev
          - stg
          - prd

env:
  ENV_VALUE: |
    ${{
      github.event.inputs.env == 'prd' &&
        secrets.PRD ||
      github.event.inputs.env == 'stg' &&
        secrets.STG ||
      secrets.DEV
    }}

set-envを使わずに環境変数に値をセットする

  • echo "{name}={value}" >> $GITHUB_ENV と記述する
  • workflow実行時に設定したenvの情報を環境変数に設定する
name: test
on:
  workflow_dispatch:
    inputs:
      env:
        type: choice
        description: "envを選択"
        required: true
        options:
          - dev
          - stg
          - prd

permissions:
  id-token: write
  contents: write

jobs:
  test:
    name: "sample"
    runs-on: ubuntu-22.04
    timeout-minutes: 3
      - name: Set ENV
        run: |
          echo "ENV"=`echo ${{ github.event.inputs.env }}` >> $GITHUB_ENV

      - name: Show ENV
        run: |
          echo "ENV=${{ env.ENV }}"

■ 実行結果

repository_dispatch を利用して別のworkflowから実行する

注: このイベントは、ワークフローファイルがデフォルト ブランチにある場合にのみワークフローの実行をトリガーします。

  • 呼び出し元のworkflow
    • event_type は任意の文字列で設定する
name: call
on:
  push:
    branches:
      - test
jobs:
  call:
    runs-on: ubuntu-22.04
    timeout-minutes: 3

    steps:
      - name: Set request body
        run: |
          echo "REQUEST_BODY"='{"event_type": "call_workflow", "client_payload": {"sample_key": "sample_value" }}' >> $GITHUB_ENV

      - name: Trigger repository dispatch
        run: |
          curl \
          -X POST \
          -H "Accept: application/vnd.github.v3+json" \
          -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
          https://api.github.com/repos/yhidetoshi/github-actions-workflow-dev/dispatches \
          --data "$REQUEST_BODY"

■ 実行結果

  • 呼び出し先のworkflow
    • types: [call_workflow] 呼び出し元の event_type に合わせる
name: test_repository_dispatch
on:
  repository_dispatch:
    types: [call_workflow]

permissions:
  id-token: write
  contents: write

jobs:
  test:
    name: "sample"
    runs-on: ubuntu-22.04
    timeout-minutes: 3

    steps:
      - name: Hello world
        run: |
          echo "Hello world (test repository dispatch)"

■ 実行結果

Composite Actionを使う

workflowの処理をモジュール化するときに使える

name: use_composte
on:
  push:
    branches:
      - test

env:
  RELEASE_TAG: 0.0.2

jobs:
  call:
    runs-on: ubuntu-22.04
    timeout-minutes: 3

    steps:
      - name: Checkout composite-action
        uses: actions/checkout@v3
        with:
          repository: yhidetoshi/workflow-composite-action-dev
          path: ./.github/actions/composite-action
          token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
          ref: ${{ env.RELEASE_TAG }}

      - name: Call print composite
        uses: ./.github/actions/composite-action/print
  • composite-actionのworkflow
    • yhidetoshi/workflow-composite-action-dev
❯ tree .
.
├── README.md
└── print
    └── action.yml
name: print

runs:
  using: "composite"
  steps:
    - run: |
        echo "Hello world at composite"
      shell: bash