私が歌川です

@utgwkk が書いている

GitHub Actionsでテスト落ちた & 別のstepのoutputがtrue のときだけ走るstepを定義する

GitHub Actionsでテスト落ちた & 別のstepのoutputがtrue のときだけ走るstepを定義しようとしたけどできなかった - 私が歌川です の続きです。表題のようなworkflowを書こうとして結局やり方が分からなかったので、フォーラムに投稿したところ、回答をいただけました。

github.community

if: failure() && (steps.foo.outputs.exists == 'true') のように条件を指定すればよい、とのことです。確かにこの方法で実現できました。 前回の記事にあったworkflow YAMLを修正すると以下のようになります。

name: test
on:
  push:

jobs:
  test:
    name: annotate
    runs-on: ubuntu-latest
    steps:
      # 実際のテストの代わりに、ファイルを出力して non-zero status を返している
      - id: do_test
        run: |
          echo hello > hello.txt
          false

      - id: foo
        run: |
          if [ -e 'hello.txt' ]; then
            echo "::set-output name=exists::true"
          fi
        if: failure()

      - name: check steps.foo.outputs.exists
        run: echo ${{ steps.foo.outputs.exists }}
        if: always()

      - uses: actions/upload-artifact@v2
        with:
          name: hello
          path: hello.txt
        if: failure() && (steps.foo.outputs.exists == 'true')

failure() とか always() がない場合、そもそも条件式が評価されないみたいです。ということは、逆に failure() などは評価規則がちょっと変わってくる?

追記

If your if expression does not contain any of the status functions it will automatically result with success(). Context and expression syntax for GitHub Actions - GitHub Docs

ドキュメントに書いてありました。やはり failure()always() が含まれない場合、自動的に if: success() && ... だと解釈されるようです。