1

我们已经在使用来自 Tekton Hubgitlab-set-status任务将 Tekton Pipeline 的状态报告回我们的 GitLab 实例(这是我们的 EKS 设置和 Tekton 安装以及gitlab.com 上的示例项目)。我们pipeline.yml看起来像这样,当前STATE每次 Tekton Pipeline 运行时都会报告成功:

apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: buildpacks-test-pipeline
spec:
  params:
    - name: IMAGE
      type: string
      description: image URL to push
    - name: SOURCE_URL
      type: string
      description: A git repo url where the source code resides.
    - name: REPO_PATH_ONLY
      type: string
      description: GitLab group & repo name only (e.g. jonashackt/microservice-api-spring-boot)
    - name: SOURCE_REVISION
      description: The branch, tag or SHA to checkout.
      default: ""
    - name: GITLAB_HOST
      type: string
      description: Your GitLabs host only (e.g. gitlab.com)
    - name: TEKTON_DASHBOARD_HOST
      type: string
      description: The Tekton dashboard host name only

  workspaces:
    - name: source-workspace # Directory where application source is located. (REQUIRED)
    - name: cache-workspace # Directory where cache is stored (OPTIONAL)
  tasks:
    - name: fetch-repository # This task fetches a repository from github, using the `git-clone` task you installed
      taskRef:
        name: git-clone
      workspaces:
        - name: output
          workspace: source-workspace
      params:
        - name: url
          value: "$(params.SOURCE_URL)"
        - name: revision
          value: "$(params.SOURCE_REVISION)"
        - name: subdirectory
          value: ""
        - name: deleteExisting
          value: "true"
    - name: buildpacks # This task uses the `buildpacks` task to build the application
      taskRef:
        name: buildpacks
      runAfter:
        - fetch-repository
      workspaces:
        - name: source
          workspace: source-workspace
        - name: cache
          workspace: cache-workspace
      params:
        - name: APP_IMAGE
          value: "$(params.IMAGE)"
        - name: BUILDER_IMAGE
          value: paketobuildpacks/builder:base # This is the builder we want the task to use (REQUIRED)
    - name: report-pipeline-end-to-gitlab
      taskRef:
        name: "gitlab-set-status"
      runAfter:
        - buildpacks
      params:
        - name: "STATE"
          value: "success"
        - name: "GITLAB_HOST_URL"
          value: "$(params.GITLAB_HOST)"
        - name: "REPO_FULL_NAME"
          value: "$(params.REPO_PATH_ONLY)"
        - name: "GITLAB_TOKEN_SECRET_NAME"
          value: "gitlab-api-secret"
        - name: "GITLAB_TOKEN_SECRET_KEY"
          value: "token"
        - name: "SHA"
          value: "$(params.SOURCE_REVISION)"
        - name: "TARGET_URL"
          value: "$(params.TEKTON_DASHBOARD_HOST)/#/namespaces/default/pipelineruns/$(context.pipelineRun.name)"
        - name: "CONTEXT"
          value: "tekton-pipeline"
        - name: "DESCRIPTION"
          value: "Finished building your commit in Tekton"

我们如何增强我们的 Tekton Pipeline 以正确地报告状态,而不管 GitLab 的任何失败或成功?

4

1 回答 1

1

在 v0.14 中,Tekton 引入了所谓的finallyTasks,它在每个结束时运行Pipeline- 无论哪个 Task 失败或成功。正如文档所述

finally 任务保证在任务下的所有 PipelineTask 完成后,无论成功或错误,都会并行执行。

一般finally任务如下所示:

spec:
  tasks:
    - name: tests
      taskRef:
        name: integration-test
  finally:
    - name: cleanup-test
      taskRef:
        name: cleanup

但是我们如何STATE在我们的gitlab-set-statusTask中创建对应的呢?通过在我们的任务中使用when表达式, finallygitlab-set-status我们可以根据整体管道状态(或聚合管道状态)运行我们的任务:

finally:
  - name: notify-any-failure # executed only when one or more tasks fail
    when:
      - input: $(tasks.status)
        operator: in
        values: ["Failed"]
    taskRef:
      name: notify-failure

我们通过简单地使用来获取聚合执行状态$(tasks.status)。该变量被声明为具有这 4 种可能的状态:

  • Succeeded(“所有任务都成功了”)
  • Completed(“所有任务都已成功完成,包括一项或多项跳过的任务”)

-> 可以翻译成gitlab-set-status任务STATEsuccess

  • Failed(“一个或多个任务失败”)
  • None(“没有可用的聚合执行状态(即以上都不是),一个或多个任务可能处于挂起/运行/取消/超时”)

-> 这两者都可以翻译成gitlab-set-statusTasksSTATEfailed。因为None这只是有效的,因为我们处于finally task, 因为pending/running否则也可能意味着管道处于良好状态。

我们需要在when表达式中检查 4 个状态,我们是否需要为每个状态实现一个单独的 finally 任务?不,因为幸运的是when表达式“值是字符串值的数组”。. 所以我们能够做到

  when:
    - input: $(tasks.status)
      operator: in
      values: [ "Failed", "None" ]

  when:
    - input: $(tasks.status)
      operator: in
      values: [ "Succeeded", "Completed" ]

最后,这导致我们的 Tekton Pipeline 像这样锁定(并实现 2 个 finally 任务report-pipeline-failed-to-gitlabreport-pipeline-success-to-gitlab):

...
  finally:
    - name: report-pipeline-failed-to-gitlab
      when:
        - input: $(tasks.status)
          operator: in
          values: [ "Failed", "None" ] # see aggregated status https://tekton.dev/docs/pipelines/pipelines/#using-aggregate-execution-status-of-all-tasks
      taskRef:
        name: "gitlab-set-status"
      params:
        - name: "STATE"
          value: "failed"
        - name: "GITLAB_HOST_URL"
          value: "$(params.GITLAB_HOST)"
        - name: "REPO_FULL_NAME"
          value: "$(params.REPO_PATH_ONLY)"
        - name: "GITLAB_TOKEN_SECRET_NAME"
          value: "gitlab-api-secret"
        - name: "GITLAB_TOKEN_SECRET_KEY"
          value: "token"
        - name: "SHA"
          value: "$(params.SOURCE_REVISION)"
        - name: "TARGET_URL"
          value: "$(params.TEKTON_DASHBOARD_HOST)/#/namespaces/default/pipelineruns/$(context.pipelineRun.name)"
        - name: "CONTEXT"
          value: "tekton-pipeline"
        - name: "DESCRIPTION"
          value: "An error occurred building your commit in Tekton"
    - name: report-pipeline-success-to-gitlab
      when:
          - input: $(tasks.status)
            operator: in
            values: [ "Succeeded", "Completed" ] # see aggregated status https://tekton.dev/docs/pipelines/pipelines/#using-aggregate-execution-status-of-all-tasks
      taskRef:
        name: "gitlab-set-status"
      params:
        - name: "STATE"
          value: "success"
        - name: "GITLAB_HOST_URL"
          value: "$(params.GITLAB_HOST)"
        - name: "REPO_FULL_NAME"
          value: "$(params.REPO_PATH_ONLY)"
        - name: "GITLAB_TOKEN_SECRET_NAME"
          value: "gitlab-api-secret"
        - name: "GITLAB_TOKEN_SECRET_KEY"
          value: "token"
        - name: "SHA"
          value: "$(params.SOURCE_REVISION)"
        - name: "TARGET_URL"
          value: "$(params.TEKTON_DASHBOARD_HOST)/#/namespaces/default/pipelineruns/$(context.pipelineRun.name)"
        - name: "CONTEXT"
          value: "tekton-pipeline"
        - name: "DESCRIPTION"
          value: "Finished building your commit in Tekton"

现在应该正确地向我们的 GitLab 报告执行我们的 Tekton Pipeline。失败看起来像这样:

在此处输入图像描述

成功的管道如下所示:

在此处输入图像描述

于 2021-11-29T14:05:35.783 回答