Open3

GitHub Actions Syntax

KyomeKyome

if always() の挙動について実験

needsでJobを直列にしているとき、
if: always() && (needs.first_job.result == 'success' || needs.first_job.result == 'failure')
だとJobがスキップされる。

name: Check GitHub Actions Syntax

on:
  workflow_dispatch:

jobs:
  first_job:
    name: First Job
    runs-on: ubuntu-latest
    if: false
    timeout-minutes: 10

    steps:
    - uses: actions/checkout@v3

    - name: Run
      run: echo "Hello World"

  second_job:
    name: Second Job
    runs-on: ubuntu-latest
    needs: [first_job]
    if: always() && (needs.first_job.result == 'success' || needs.first_job.result == 'failure')
    timeout-minutes: 10

    steps:
    - uses: actions/checkout@v3

    - name: Run
      run: echo "Hello World"

needsでJobを直列しているとき、
if: always() && !contains(needs.*.result, 'failure') && !contains(needs.*.result, 'cancelled')
だとJobが実行された。

name: Check GitHub Actions Syntax

on:
  workflow_dispatch:

jobs:
  first_job:
    name: First Job
    runs-on: ubuntu-latest
    if: false
    timeout-minutes: 10

    steps:
    - uses: actions/checkout@v3

    - name: Run
      run: echo "Hello World"

  second_job:
    name: Second Job
    runs-on: ubuntu-latest
    needs: [first_job]
    if: always() && !contains(needs.*.result, 'failure') && !contains(needs.*.result, 'cancelled')
    timeout-minutes: 10

    steps:
    - uses: actions/checkout@v3

    - name: Run
      run: echo "Hello World"

結論

  • always()needsで指定しているJobがスキップされてもそれは無視して突破する力を持つ
  • ただし、needsで指定しているJobの結果をちゃんと待ってくれる
KyomeKyome

if failure() の挙動について実験

Jobレベルでif: !failure() をしたときにひとつ前のJobまでが範囲なのかそれまでのJob全てが範囲なのかを検証したい。

name: Check GitHub Actions Syntax

on:
  workflow_dispatch:

jobs:
  first_job:
    name: First Job
    runs-on: ubuntu-latest
    timeout-minutes: 10

    steps:
    - uses: actions/checkout@v3

    - name: Run
      run: exit 1

  second_job:
    name: Second Job
    runs-on: ubuntu-latest
    needs: [first_job]
    if: always()
    timeout-minutes: 10

    steps:
    - uses: actions/checkout@v3

    - name: Run
      run: echo "Hello World"

  third_job:
    name: Third Job
    runs-on: ubuntu-latest
    needs: [second_job]
    if: ${{ !failure() }}

    timeout-minutes: 10

    steps:
    - uses: actions/checkout@v3

    - name: Run
      run: echo "Hello World"

こうすると

  • first_job: ❌ failure
  • second_job: ✅ success
  • third_job: 🚫 skipped

となるので、範囲はそれまでのJob全てである

KyomeKyome

if failure() の挙動について実験2

needsで指定しているJobがSkippedだったとしてもJobが動くかどうか検証。

name: Check GitHub Actions Syntax

on:
  workflow_dispatch:

jobs:
  first_job:
    name: First Job
    runs-on: ubuntu-latest
    if: false
    timeout-minutes: 10

    steps:
    - uses: actions/checkout@v3

    - name: Run
      run: echo "Hello World"

  second_job:
    name: Second Job
    runs-on: ubuntu-latest
    if: false
    timeout-minutes: 10

    steps:
    - uses: actions/checkout@v3

    - name: Run
      run: echo "Hello World"

  third_job:
    name: Third Job
    runs-on: ubuntu-latest
    needs: [first_job, second_job]
    if: ${{ !failure() && !cancelled() }}
    timeout-minutes: 10

    steps:
    - uses: actions/checkout@v3

    - name: Run
      run: echo "Hello World"
  • first_job: 🚫 skipped
  • second_job: 🚫 skipped
  • third_job: ✅ success

となったので、always()を使わなくても十分効果を発揮できそう。