GitHub Actions で失敗を条件として扱う

2024-09-25

continue-on-error: true を活用する

continue-on-error: true をつけることで、step や job が失敗した際に処理を継続できる。 if: steps.<id>.outcome == "failure" で、“失敗のとき” のように条件として利用できる。“failure” の逆は “success”。

- name: Check JSON diff
	id: diff
    # --exit-code で差分があると終了コード 1 を返す
	run: |
		git diff --name-only --exit-code swagger_*.json
	continue-on-error: true
- name: git commit & push
	run: |
		set -x
		git config user.name github-actions[bot]
		git config user.email 41898282+github-actions[bot]@users.noreply.github.com
		git fetch && git switch ${{ github.head_ref }}
		git add swagger_*.json
		git commit -m 'message'
		git push origin ${{ github.head_ref }}
	if: steps.diff.outcome == "failure"

job 全体と、step に対して設定できる。