🐙
[GitHub Actions]needs指定があるjobをworkflow_dispatch等で指定して単独で実行したい
導入
たとえば、jobがjob-1,job-2,job-3と存在し、これらを同時に実行する際に、job-1はjob-2とjob-3の完了後に実行する必要があるものとする。
こんな関係のjob構造。
しかし、job-1を単独実行したいケースもある場合、どう書くか?という話
どう書くか?
こんな感じになる
name: example
on:
schedule:
- cron: '0 23 * * SUN-THU'
workflow_dispatch:
inputs:
target:
type: choice
description: 実行するjob
required: true
options:
- job-1
- job-2
- job-3
jobs:
job-1:
needs: [job-2, job-3]
if: |
(
always() &&
contains(needs.*.result, 'skipped') &&
github.event.inputs.target == 'job-1'
) || github.event_name == 'schedule'
runs-on: ...(略)
job-2:
if: github.event_name == 'schedule' || github.event.inputs.target == 'job-2'
runs-on: ...(略)
job-3:
if: github.event_name == 'schedule' || github.event.inputs.target == 'job-3'
runs-on: ...(略)
ポイント
if: |
(
always() &&
contains(needs.*.result, 'skipped') &&
github.event.inputs.target == 'job-1'
) || github.event_name == 'schedule'
は、
if: github.event.inputs.target == 'job-1' || github.event_name == 'schedule'
だとworkflow_dispatchでの実行の場合に動かない。(needsで指定したjob-2とjob-3が成功していないのでjob1はスキップされてしまう)
すっげぇ〜〜〜〜〜〜〜〜冗長だし謎だが、 always()
がいい感じに拾ってくれるキーワードになってくれていそう。そのあと、contains句でfilterするイメージ。
ここでも、すげぇ冗長だけどこれで動くんだよね😅って言われている。
Discussion