【Github】Github Actions ~ if ~

■ はじめに

https://dk521123.hatenablog.com/entry/2024/01/28/004128

の分冊。

Github Actions の if について、まとめる

目次

【1】if
【2】if の指定の種類
 1)jobs.<job_id>.if
 2)jobs.<job_id>.steps[*].if
【3】if に指定する主なイベント
 1)成功 - success()
 2)失敗 - failure()
 3)取り消し - cancelled()
 4)常時 - always()
 5)step時の結果イベント(steps.<step_id>.conclusion / steps.<step_id>.outcome)
【4】使用上の注意
【5】サンプル
 例1:ファイルサイズを確認し、2000byte以下であれば中断する

【1】if

* if [条件] で、[条件] が真になった場合に実行する

https://docs.github.com/ja/actions/learn-github-actions/expressions#status-check-functions

【2】if の指定の種類

* if には、それぞれ「Job」と「Step」に対するものの2種類ある

1)jobs.<job_id>.if

https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idif

* Job に対して指定する

https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#example-only-run-job-for-specific-repository

name: example-workflow
on: [push]
jobs:
  production-deploy:
    if: github.repository == 'octo-org/octo-repo-prod'
    runs-on: ubuntu-latest

2)jobs.<job_id>.steps[*].if

https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsif

* Step に対して指定する

https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#example-using-status-check-functions

steps:
  - name: My first step
    uses: octo-org/action-name@main
  - name: My backup step
    if: ${{ failure() }} # 失敗した時に実行
    uses: actions/heroku@1.0.0

【3】if に指定する主なイベント

1)成功 - success()

* これまでの手順がすべて成功した場合は true を返す

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

if: ${{ success() }}

2)失敗 - failure()

* 失敗時

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

steps:
  ...
  - name: Failing step
    id: demo
    run: exit 1
  - name: The demo step has failed
    if: ${{ failure() && steps.demo.conclusion == 'failure' }}

3)取り消し - cancelled()

* ユーザ・キャンセル時

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

4)常時 - always()

* ステップが常に実行され、キャンセルされた場合でも true を返す

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

if: ${{ always() }}

5)step時の結果イベント(steps.<step_id>.conclusion / steps.<step_id>.outcome)

* 以下の関連記事を参照のこと

https://dk521123.hatenablog.com/entry/2024/01/01/232057

例:Error handling

    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】使用上の注意

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

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

【5】サンプル

例1:ファイルサイズを確認し、2000byte以下であれば中断する

Github Actions ~ Python関連 ~
https://dk521123.hatenablog.com/entry/2022/06/21/143624

      # Wheelファイル作成およびファイルサイズをWHL_FILE_SIZEに格納
      - name: Run Python
        run: |
          python ./DemoLib/setup.py bdist_wheel
          cp ./dist/*.whl ./DemoLib/dist/*.whl
          echo "WHL_FILE_SIZE=$(wc -c ./dist/*.whl | awk '{print $1}')" >> $GITHUB_ENV
      # Wheelファイルサイズを確認(2000byte以下であれば中断)
      - name: File size check
        if: ${{ env.WHL_FILE_SIZE <= 2000 }}
        run: exit 1

関連記事

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 ~ ワークフロー制御 ~
https://dk521123.hatenablog.com/entry/2024/01/28/004128
Github Actions ~ エラー処理 / continue-on-error ~
https://dk521123.hatenablog.com/entry/2024/01/01/232057
Github Actions ~ workflow_dispatch / 手動実行 ~
https://dk521123.hatenablog.com/entry/2024/03/12/012953
Github Actions ~ workflow_run / workflow_call ~
https://dk521123.hatenablog.com/entry/2024/02/14/232546
Github Actions ~ あれこれ編 ~
https://dk521123.hatenablog.com/entry/2023/12/21/155224
Github Actions ~ 外部シェルスクリプト実行 ~
https://dk521123.hatenablog.com/entry/2024/01/19/003044
Github Actions ~ 設定値を切り替えることを考える ~
https://dk521123.hatenablog.com/entry/2024/02/18/232926
Github Actions ~ Python関連 ~
https://dk521123.hatenablog.com/entry/2022/06/21/143624
GitHub CLI ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2024/02/17/233836