【Tekton】PipelineでTaskを実行する条件をつける

2022.04.13
2024.03.24
CI/CD
TektonTekton Pipeline

はじめに

TektonのPipelineの中で条件をつけてTaskを実行する方法を紹介します。

TektonのPipelineやTaskについては、下記を参照してください。

【KubernetesでCI/CD】Tekton動かしてみる

【KubernetesでCI/CD】Tekton動かしてみる

はじめに 簡単な例を使って、Tekton Pipelineを動かしてみたいと思います。 Tek

whenを使ったtask実行の条件指定

Pipelineの中でTaskを実行する条件を記述するときはwhenを使います。

conditionsというのもありますが、ドキュメントには非推奨と書かれています。

Tekton

Pipelines Pipelines Overview Configuring a Pipeline Specifying Workspaces Specifying Parameters Adding Tasks to the Pipeline Specifying Display Name Specifying Remote Tasks Specifying Pipelines in PipelineTasks Specifying Parameters in PipelineTasks Specifying Matrix in PipelineTasks Specifying Workspaces in PipelineTasks Tekton Bundles Using the runAfter field Using the retries field Using the onError field Produce results with OnError Guard Task execution using when expressions Guarding a Task and its dependent Tasks Cascade when expressions to the specific dependent Tasks Compose using Pipelines in Pipelines Guarding a Task only Configuring the failure timeout Using variable substitution Using the retries and retry-count variable substitutions Using Results Passing one Task’s Results into the Parameters or when expressions of another Emitting Results from a Pipeline Configuring the Task execution order Adding a description Adding Finally to the Pipeline Specifying Display Name Specifying Workspaces in finally tasks Specifying Parameters in finally tasks Specifying matrix in finally tasks Consuming Task execution results in finally Consuming Pipeline result with finally PipelineRun Status with finally Using Execution Status of pipelineTask Using Aggregate Execution Status of All Tasks Guard finally Task execution using when expressions when expressions using Parameters in finally Tasks when expressions using Results in finally ‘Tasks` when expressions using Execution Status of PipelineTask in finally tasks when expressions using Aggregate Execution Status of Tasks in finally tasks Known Limitations Cannot configure the finally task execution order Using Custom Tasks Specifying the target Custom Task Specifying a Custom Task Spec in-line (or embedded) Specifying parameters Specifying matrix Specifying workspaces Using Results Specifying Timeout Specifying Retries Known Custom Tasks Code examples Overview A Pipeline is a collection of Tasks that you define and arrange in a specific order of execution as part of your continuous integration flow.

下記のようにwheninputoperatorvaluesから記述されます。

1tasks:
2...
3- name: task-name
4  runAfter:
5    - previous-task
6  when:
7    - input: $(tasks.previous-task.results.value)
8      operator: in
9      values: ["test"]
10  taskRef:
11    name: task-name
12...

operatorinnotinが利用できます。

Tekton

Pipelines Pipelines Overview Configuring a Pipeline Specifying Workspaces Specifying Parameters Adding Tasks to the Pipeline Specifying Display Name Specifying Remote Tasks Specifying Pipelines in PipelineTasks Specifying Parameters in PipelineTasks Specifying Matrix in PipelineTasks Specifying Workspaces in PipelineTasks Tekton Bundles Using the runAfter field Using the retries field Using the onError field Produce results with OnError Guard Task execution using when expressions Guarding a Task and its dependent Tasks Cascade when expressions to the specific dependent Tasks Compose using Pipelines in Pipelines Guarding a Task only Configuring the failure timeout Using variable substitution Using the retries and retry-count variable substitutions Using Results Passing one Task’s Results into the Parameters or when expressions of another Emitting Results from a Pipeline Configuring the Task execution order Adding a description Adding Finally to the Pipeline Specifying Display Name Specifying Workspaces in finally tasks Specifying Parameters in finally tasks Specifying matrix in finally tasks Consuming Task execution results in finally Consuming Pipeline result with finally PipelineRun Status with finally Using Execution Status of pipelineTask Using Aggregate Execution Status of All Tasks Guard finally Task execution using when expressions when expressions using Parameters in finally Tasks when expressions using Results in finally ‘Tasks` when expressions using Execution Status of PipelineTask in finally tasks when expressions using Aggregate Execution Status of Tasks in finally tasks Known Limitations Cannot configure the finally task execution order Using Custom Tasks Specifying the target Custom Task Specifying a Custom Task Spec in-line (or embedded) Specifying parameters Specifying matrix Specifying workspaces Using Results Specifying Timeout Specifying Retries Known Custom Tasks Code examples Overview A Pipeline is a collection of Tasks that you define and arrange in a specific order of execution as part of your continuous integration flow.

実際に試してみる

実際にwhenを使ってみます。

作成するファイルは下記の通りです。

1.
2├── pipeline-hello-goodbye.yml
3├── pipelineRun-hello-goodbye.yml
4├── task-goodbye.yml
5├── task-hello-again.yml
6└── task-hello.yml

3つのTaskをまとめたPipelineを作成して、PipelineRunで実行します。

Taskを作成

作成する3つのTaskのマニフェスト(task-hell.ymltask-goodbye.ymltask-hello-again.yml)は下記の通りです。

1apiVersion: tekton.dev/v1beta1
2kind: Task
3metadata:
4  name: hello
5spec:
6  results:
7    - name: skip-goodbye
8  steps:
9    - name: hello
10      image: ubuntu
11      script: | # 次のTaskを実行する条件となるResultを格納する
12        #!/bin/bash
13        echo "hello"
14        echo -n "false" > $(results.skip-goodbye.path)
1apiVersion: tekton.dev/v1beta1
2kind: Task
3metadata:
4  name: goodbye
5spec:
6  steps:
7    - name: goodbye
8      image: ubuntu
9      script: |
10        #!/bin/bash
11        echo "Goodbye World!"
1apiVersion: tekton.dev/v1beta1
2kind: Task
3metadata:
4  name: hello-again
5spec:
6  steps:
7    - name: hello
8      image: ubuntu
9      script: |
10        #!/bin/bash
11        echo "hello again"

Pipelineを作成

3つのTaskを実行するPipeline(pipeline-hello-goodbye.yml)を作成します。

1apiVersion: tekton.dev/v1beta1
2kind: Pipeline
3metadata:
4  name: hello-goodbye
5spec:
6  tasks:
7    - name: hello
8      taskRef:
9        name: hello
10    - name: goodbye
11      runAfter:
12        - hello
13      when: # Resultが"true"のときだけ実行する
14        - input: $(tasks.hello.results.skip-goodbye)
15          operator: in
16          values: ["true"]
17      taskRef:
18        name: goodbye
19    - name: hello-again # runAfterとしない
20      taskRef:
21        name: hello-again

PipelineRunを作成

Pipelineを実行するためのPipelineRun(piplineRun-hello-goodbye.yml)を作成します。

1apiVersion: tekton.dev/v1beta1
2kind: PipelineRun
3metadata:
4  generateName: hello-goodbye-run-
5  namespace: default
6spec:
7  pipelineRef:
8    name: hello-goodbye

Pipelineを実行

まずは、TaskとPipelineを作成します。

1kubectl apply -f task-hello.yml
2kubectl apply -f task-goodbye.yml
3kubectl apply -f task-hello-again.yml
4kubectl apply -f pipeline-hello-goodbye.yml

PipelineRunでPipelineを実行します。

1❯ k create -f pipelineRun-hello-goodbye.yml
2pipelinerun.tekton.dev/hello-goodbye-run-txvjk created

結果の確認

作成されたPipelineRunのログを確認してみると、Taskがスキップされていることがわかります。

1❯ tkn pr logs hello-goodbye-run-txvjk
2[hello : hello] hello
3
4[hello-again : hello] hello again

ちなみに、hello-againTaskを下記のようにrunAfterで定義すると、good-byeTaskがスキップされた場合に、実行されなくなります。

1...
2    - name: hello-again
3      runAfter:
4        - good-bye
5      taskRef:
6        name: hello-again
7...

参考

Support

\ この記事が役に立ったと思ったら、サポートお願いします! /

buy me a coffee
Share

Profile

author

Masa

都内のIT企業で働くエンジニア
自分が学んだことをブログでわかりやすく発信していきながらスキルアップを目指していきます!

buy me a coffee