LoginSignup
15
7

More than 3 years have passed since last update.

GitHub Actions で、あるステップが失敗した時だけ実行するステップを定義する

Posted at

GitHub Actions で あるステップが失敗した時だけ実行するステップ を定義したいんだけど、ってことがありました。
具体的に言うと、フロントエンドのテストに失敗した時だけ、スクショを artifact として残しておいて、後からスクショを確認したいとか(例えばです)。

そんな時にどうしたらいいかが、あんまり調べてもなかなか出てこなかったので書いてみます。

結論

結論から言うとこうする

    - name: fail step
      id: fail_step
      run: exit 1

    - name: run if fail_step failed
      if: failure() && steps.fail_step.outcome == 'failure'
      run: echo "${{ steps.fail_step.outcome }}"

ちょっと解説

やることは主に下記の

  1. あるステップが失敗した時だけ〜 に当たるあるステップに id を振る
  2. 失敗した時だけ実行するステップ を定義して、 if: failure() で以前の step が失敗した時だけ実行するようにする
  3. steps.<job_id>.outcome で step の実行結果が取得できるので、 if の条件に追加する

サンプル

参考

https://help.github.com/ja/actions/reference/workflow-syntax-for-github-actions
https://help.github.com/ja/actions/reference/context-and-expression-syntax-for-github-actions#job-status-check-functions

15
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
7