更新日:
投稿日:

GitHub Actionsで前のジョブの結果を後続で利用したい時に使えるやつ

サンプルコード

  • 適当な文字列を変数にセットして、各ジョブで出力する例
name: outputs sharing example
on:
  pull_request:
jobs:
  first:
    runs-on: ubuntu-latest
    outputs:
      # <out-job-name>: ${{ steps.<step-id>.outputs.<in-job-name> }}
      baz: ${{ steps.foo.outputs.bar }}
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - id: foo
        # この場合、FOO-BARという値がセットされる
        run: echo "::set-output name=bar::$(echo FOO-BAR)"
      - id: test
        # steps.<step-id>.outputs.<in-job-name>
        run: echo "${{ steps.foo.outputs.bar }}"
  second:
    needs: [first]
    runs-on: ubuntu-latest
    steps:
      - id: test
        # needs.<job-id>.outputs.<out-job-name>
        run: echo "${{ needs.first.outputs.baz }}"

参考

更新日:
投稿日:

pull_requestイベントの公式リファレンスが手薄くイベントの意味を明示してないので動きを実際に確認したものをメモ程度に。基本は意味のままだと思いますが…

opened: PRが開いたとき
reopened: PRが開き直されたとき
synchronize: PRに対してPushが走ったとき

特定ブランチから特定ブランチへのPRを阻止する

  • サンプルで作っただけなので中身は適当
    • pull_requestbranchesを先頭に書かないと、指定ブランチ以外でも走るので注意
name: testing on opend PR to main
on:
  pull_request:
    branches:
      - main
    types:
      - opened
      - reopened
      - synchronize
jobs:
  # 事前ブランチチェック
  before-check:
    runs-on: ubuntu-latest
    steps:
      - name: fail case
        if: startsWith(github.head_ref, 'test/') && github.base_ref == 'main'
        # test/* ブランチから main ブランチ宛である場合
        # exit 1で終了することで Workflow を failure 扱いにする
        # https://docs.github.com/ja/actions/creating-actions/setting-exit-codes-for-actions
        run: exit 1
        # 前の if に入らなければ、そのまま次のジョブにつながる
  after-exec:
    # 指定されたジョブの成功を要求、失敗している場合、このジョブを実行しない
    # 必然的に線形実行になる(並列では走らない)
    needs: [before-check]
    runs-on: ubuntu-latest
    steps:
      - name: TEST!
        run: echo "RUN after-exec"
更新日:
投稿日:

取り敢えず各ジョブの中で使うやつ

今まで使っていた::set-outputは2023-05-31に廃止される予定なので置き換える必要があります。

GitHub Actions: Deprecating save-state and set-output commands

サンプルコード

  • 設定方法:echo "<KEY>=<VALUE>" >> "$GITHUB_OUTPUT"
  • 参照方法:steps.<ID>.outputs.{KEY}
name: variable example
on:
  workflow_dispatch:
jobs:
  ubuntu-testing:
    runs-on: ubuntu-latest
    steps:
      - id: example
        run: echo "value=hoge" >> "$GITHUB_OUTPUT"
      - name: disp
        run: echo ${{ steps.example.outputs.value }}