Tekton Pipelines 基础

Pipelines 概述

Pipeline是Tasks的集合,作为持续集成流的一部分,您可以定义并按照特定的执行顺序排列这些Tasks。Pipeline中的每个Tasks在Kubernetes集群上作为Pod执行。您可以配置各种执行条件来满足您的业务需求。

Pipeline使用When表达式

when 表达式

input: 被评估的内容,支持使用静态值或者变量(Parameters或者Results变量)。 e.g. "$(params.image)" or "$(tasks.task1.results.image)" or "$(tasks.task1.results.array-results[1])"。
operator: 比较操作符,仅支持in或notin两个。
values: 由字符串组成的列表,必须定义,且不能使用空值,但允许使用静态值或者变量。e.g. ["$(params.image)"] or ["$(tasks.task1.results.image)"] or ["$(tasks.task1.results.array-results[1])"]

when 表达式示例

tasks:
  - name: first-create-file
    when:
      - input: "$(params.path)"
        operator: in
        values: ["README.md"]
    taskRef:
      name: first-create-file
---
tasks:
  - name: echo-file-exists
    when:
      - input: "$(tasks.check-file.results.exists)"
        operator: in
        values: ["yes"]
    taskRef:
      name: echo-file-exists
---
tasks:
  - name: run-lint
    when:
      - input: "$(workspaces.lint-config.bound)"
        operator: in
        values: ["true"]
    taskRef:
      name: lint-source
---
tasks:
  - name: deploy-in-blue
    when:
      - input: "blue"
        operator: in
        values: ["$(params.deployments[*])"]
    taskRef:
      name: deployment

Pipeline使用runAfter

runAfter 概述

定义Task运行的顺序。

runAfter 示例

workspaces:
- name: source
tasks:
- name: test-app
  taskRef:
    name: make-test
  workspaces:
  - name: source
    workspace: source
- name: build-app
  taskRef:
    name: kaniko-build
  runAfter:
    - test-app          # 在test-app执行完成后再执行build-app。
  workspaces:
  - name: source
    workspace: source

Pipeline使用Finally

Finally 概述

用于在tasks中的各任务执行结束后运行最后的任务。

Finally 使用场景

1. 发送通知
2. 清理资源
3. 终止任务执行
...

Finally 示例

spec:
  finally:
    - name: notification
      displayName: "Notify"
      taskRef:
        name: notification
    - name: notification-using-context-variable
      displayName: "Notification from $(context.pipeline.name)"
      taskRef:
        name: notification

Pipelines 资源清单

apiVersion: tekton.dev/v1
kind: Pipeline
metadata:
  name: pipeline-demo
spec:
  params:
  workspaces:
  results:
  description:
  displayName:
  tasks:
    - name:
      displayName:
      description:
      onError: continue # 当一个PipelineTask失败时,其余的PipelineTask将被跳过,并且PipelineRun将被声明为失败。如果你想忽略这样的PipelineTask失败并继续执行剩下的PipelineTask,设置onError: continue。
      taskRef:
      taskSpec:
      runAfter:         # 定义各Task执行的顺序
      retries:         # 定义Task失败后重试次数
      timeout:
      params:
      workspaces:
      matrix:          #
      when:             # 使用when表达式来为其添加执行条件        
  finally:             # 定义一个最终任务
    - name:
      displayName:
      description:
      taskRef:
      taskSpec:
      retries:         # 定义Task失败后重试次数
      timeout:
      params:
      workspaces:
      matrix:          #
      when:             # 使用when表达式来为其添加执行条件

Pipelines 示例

apiVersion: tekton.dev/v1
kind: Pipeline
metadata:
  name: pipeline-task-ordering
spec:
  tasks:
    - name: task-a
      taskRef:
        name: logger
      params:
      - name: text
        value: "task-A executed"
    - name: task-b
      taskRef:
        name: logger
      params:
      - name: text
        value: "Executed after task-A"
      runAfter: ["task-a"]
    - name: task-c
      taskRef:
        name: logger
      params:
      - name: text
        value: "Executed after task-A"
      runAfter: ["task-a"]
    - name: task-d
      taskRef:
        name: logger
      params:
      - name: text
        value: "Executed after task-B and task-C"
      runAfter: ["task-b", "task-c"]

参考文档

https://tekton.dev/docs/pipelines/pipelines/

posted @ 2023-12-04 16:00  小吉猫  阅读(155)  评论(0编辑  收藏  举报