Menu Close
4.2.2.2. when 式
when 式で、パイプライン内のタスクの実行の条件を設定して、タスク実行を保護します。これには、特定の条件が満たされる場合にのみタスクを実行できるようにします。when 式は、パイプライン YAML ファイルの finally
フィールドを使用して指定される最終タスクセットでもサポートされます。
when 式の主要なコンポーネントは、以下のとおりです。
-
input
: パラメーター、タスクの結果、実行ステータスなどの静的入力または変数を指定します。有効な入力を入力する必要があります。有効な入力を入力しない場合は、デフォルトで空の文字列に設定されます。 -
operator
:values
セットへの入力の関係を指定します。operator の値としてin
またはnotin
を入力します。 -
values
: 文字列値の配列を指定します。ワークスペースに、パラメーター、結果、バインドされたステータスなどの静的値や変数の空でない配列を入力します。
宣言された when 式が、タスクの実行前に評価されます。when 式の値が True
の場合は、タスクが実行します。when 式の値が False
の場合、タスクはスキップします。
さまざまなユースケースで when 式を使用できます。たとえば、次のいずれかです。
- 以前のタスクの結果は期待どおりに実行される。
- Git リポジトリーのファイルが以前のコミットで変更になる。
- イメージがレジストリーに存在する。
- 任意のワークスペースが利用可能である。
以下の例は、パイプライン実行の when 式を示しています。パイプライン実行は、次の基準が満たされた場合にのみ create-file
タスクを実行します。path
パラメーターが README.md
です。また、check-file
タスクから生じる exists
が yes
の場合に限り、echo-file-exists
タスクが実行します。
apiVersion: tekton.dev/v1beta1 kind: PipelineRun 1 metadata: generateName: guarded-pr- spec: serviceAccountName: 'pipeline' pipelineSpec: params: - name: path type: string description: The path of the file to be created workspaces: - name: source description: | This workspace is shared among all the pipeline tasks to read/write common resources tasks: - name: create-file 2 when: - input: "$(params.path)" operator: in values: ["README.md"] workspaces: - name: source workspace: source taskSpec: workspaces: - name: source description: The workspace to create the readme file in steps: - name: write-new-stuff image: ubuntu script: 'touch $(workspaces.source.path)/README.md' - name: check-file params: - name: path value: "$(params.path)" workspaces: - name: source workspace: source runAfter: - create-file taskSpec: params: - name: path workspaces: - name: source description: The workspace to check for the file results: - name: exists description: indicates whether the file exists or is missing steps: - name: check-file image: alpine script: | if test -f $(workspaces.source.path)/$(params.path); then printf yes | tee /tekton/results/exists else printf no | tee /tekton/results/exists fi - name: echo-file-exists when: 3 - input: "$(tasks.check-file.results.exists)" operator: in values: ["yes"] taskSpec: steps: - name: echo image: ubuntu script: 'echo file exists' ... - name: task-should-be-skipped-1 when: 4 - input: "$(params.path)" operator: notin values: ["README.md"] taskSpec: steps: - name: echo image: ubuntu script: exit 1 ... finally: - name: finally-task-should-be-executed when: 5 - input: "$(tasks.echo-file-exists.status)" operator: in values: ["Succeeded"] - input: "$(tasks.status)" operator: in values: ["Succeeded"] - input: "$(tasks.check-file.results.exists)" operator: in values: ["yes"] - input: "$(params.path)" operator: in values: ["README.md"] taskSpec: steps: - name: echo image: ubuntu script: 'echo finally done' params: - name: path value: README.md workspaces: - name: source volumeClaimTemplate: spec: accessModes: - ReadWriteOnce resources: requests: storage: 16Mi
- 1
- Kubernetes オブジェクトのタイプを指定します。この例では、
PipelineRun
です。 - 2
create-file
タスクが Pipeline で使用されます。- 3
check-file
タスクから生じたexists
がyes
になった場合に限り、echo-file-exists
タスクを実行するのに指定するwhen
式。- 4
path
パラメーターがREADME.md
の場合に限り、task-should-be-skipped-1
タスクをスキップすることを指定するwhen
式。- 5
echo-file-exists
タスクの実行ステータス、およびタスクステータスがSucceeded
で、check-file
タスクから生じるexists
がyes
になり、path
パラメーターがREADME.md
となる場合に限り、finally-task-should-be-executed
タスクを実行するのに指定するwhen
式。
OpenShift Container Platform Web コンソールの Pipeline Run details ページには、以下のようにタスクと When 式が表示されます。
- すべての基準が満たされています。タスクと、ひし形で表される when 式の記号は緑色です。
- いずれかの基準が満たされていません。タスクはスキップされます。スキップされたタスクと when 式記号は灰色になります。
- 満たされていない基準はありません。タスクはスキップされます。スキップされたタスクと when 式記号は灰色になります。
- タスクの実行が失敗する: 失敗したタスクと when 式の記号が赤で表示されます。