【Github】Github Actions ~ エラー処理 / continue-on-error ~

■ はじめに

Github Actions の エラー処理について徐々に書いていく

目次

【1】前提知識:基本動作
【2】主なエラー処理
【3】補足:if: always() について
【4】continue-on-error: true
 1)Hello world
 2)steps.<step_id>.conclusion / steps.<step_id>.outcome
【5】if: ${{ !cancelled() }}
 1)サンプル
【6】その他のエラーハンドリング
 1)if でハンドリング
 2)「shell: /usr/bin/bash {0}」を指定

【1】前提知識:基本動作

* エラーが発生すると、該当処理で処理が停止し、Workflowが失敗する

【2】主なエラー処理

種類 ケース 実装方法
Step ステップ内でエラーが発生しても無視したい場合 continue-on-error: true
Step 指定したステップは必ず実行させたい場合 if: ${{ !cancelled() }}
Job ジョブ内でエラーが発生しても無視したい場合 continue-on-error: true
Job 指定したジョブは必ず実行させたい場合 if: ${{ !cancelled() }}

【3】補足:if: always() について

https://dk521123.hatenablog.com/entry/2024/03/11/000000

でも紹介したが、以下の公式ドキュメントにある通り
ここでは、『推奨される代替手段 if: ${{ !cancelled() }}』を紹介する

https://docs.github.com/ja/actions/learn-github-actions/expressions#always

より抜粋
~~~~~~~~~
警告:
成功または失敗に関係なくジョブまたはステップを実行する場合は、
推奨される代替手段 if: ${{ !cancelled() }} を使用してください
~~~~~~~~~

【3】continue-on-error: true

* エラーでも処理を継続したい場合、
 「continue-on-error: true」を指定する
 => 終了ステータスが0以外でも成功扱いにすることができる
 => Linterなどのチェックで使えそう

1)Hello world

    steps:
      - name: Demo for continue-on-error
        continue-on-error: true
        run: |
          mkdir test_dir
          # !ERROR due to duplicated directory
          mkdir test_dir

2)steps.<step_id>.conclusion / steps.<step_id>.outcome

* continue-on-error の適用前・後のステップ実行結果をハンドリングできる

https://docs.github.com/ja/actions/learn-github-actions/contexts#steps-context

[1] steps.<step_id>.conclusion

* continue-on-error の適用後の完了したステップの結果
* 指定できる値は、success/failure/cancelled/skipped
* continue-on-error ステップが失敗した場合、
 outcome は failure になるが、最終的な conclusion は success になる

[2] steps.<step_id>.outcome

* continue-on-error の適用前の完了したステップの結果
* 指定できる値は、success/failure/cancelled/skipped
* continue-on-error ステップが失敗した場合、
 outcome は failure になるが、最終的な conclusion は success になる

例:Error handling
https://til.toshimaru.net/2023-10-11

    steps:
      - id: demo-for-continue-on-error
        name: Demo for continue-on-error
        continue-on-error: true
        run: |
          mkdir test_dir
          # !ERROR due to duplicated directory
          mkdir test_dir
      # ★ここで改めてエラーハンドリング可能
      - id: demo-for-error-handling
        name: Demo for Error handling
        if: ${{ steps.demo-for-continue-on-error.outcome == 'failure' }}
        run: |
          echo "Error handling"
          exit 1

【4】if: ${{ !cancelled() }}

* 通知など成功・エラーに関わらず実行してほしい場合などに使用

1)サンプル

    steps:
      - name: Sample
        if: ${{ !cancelled() }}
        run: echo "Hello"

【6】その他のエラーハンドリング

* 以下に載っている

https://qiita.com/mkiken/items/3d2999217832704fcf01

1)if でハンドリング

      run: |
        if test_command; then
          # error 時の処理
        fi

2)「shell: /usr/bin/bash {0}」を指定

      shell: /usr/bin/bash {0}
      run: |
        test_command
        if [ $? -ne 0 ] ; then
          # error 時の処理
        fi

関連記事

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 Actions ~ if ~
https://dk521123.hatenablog.com/entry/2024/03/11/000000