4.4. 组装 Pipeline

一个 Pipeline 代表一个 CI/CD 流,由要执行的任务定义。它被设计为在多个应用程序和环境中通用且可重复使用。

Pipeline 通过使用 fromrunAfter 参数来指定在不同任务间如何进行交互以及它们执行的顺序。它使用 workspaces 字段指定 Pipeline 中每个任务在执行过程中所需的一个或多个卷。

在本小节中,您将创建一个 Pipeline,从 GitHub 获取应用程序的源代码,然后在 OpenShift Container Platform 上构建和部署应用程序。

Pipeline 为后端应用程序 vote-api 和前端应用程序 vote-ui 执行以下任务:

  • 通过引用 git-urlgit-revision 参数,从 Git 存储库中克隆应用程序的源代码。
  • 使用 buildah ClusterTask 构建容器镜像。
  • 通过引用 image 参数将镜像推送到内部 镜像 registry。
  • 使用 apply-manifestsupdate-deployment 任务在 OpenShift Container Platform 上部署新镜像。

流程

  1. 复制以下 Pipeline YAML 文件示例内容并保存:

    apiVersion: tekton.dev/v1beta1
    kind: Pipeline
    metadata:
      name: build-and-deploy
    spec:
      workspaces:
      - name: shared-workspace
      params:
      - name: deployment-name
        type: string
        description: name of the deployment to be patched
      - name: git-url
        type: string
        description: url of the git repo for the code of deployment
      - name: git-revision
        type: string
        description: revision to be used from repo of the code for deployment
        default: "release-tech-preview-3"
      - name: IMAGE
        type: string
        description: image to be built from the code
      tasks:
      - name: fetch-repository
        taskRef:
          name: git-clone
          kind: ClusterTask
        workspaces:
        - name: output
          workspace: shared-workspace
        params:
        - name: url
          value: $(params.git-url)
        - name: subdirectory
          value: ""
        - name: deleteExisting
          value: "true"
        - name: revision
          value: $(params.git-revision)
      - name: build-image
        taskRef:
          name: buildah
          kind: ClusterTask
        params:
        - name: TLSVERIFY
          value: "false"
        - name: IMAGE
          value: $(params.IMAGE)
        workspaces:
        - name: source
          workspace: shared-workspace
        runAfter:
        - fetch-repository
      - name: apply-manifests
        taskRef:
          name: apply-manifests
        workspaces:
        - name: source
          workspace: shared-workspace
        runAfter:
        - build-image
      - name: update-deployment
        taskRef:
          name: update-deployment
        workspaces:
        - name: source
          workspace: shared-workspace
        params:
        - name: deployment
          value: $(params.deployment-name)
        - name: IMAGE
          value: $(params.IMAGE)
        runAfter:
        - apply-manifests

    Pipeline 定义提取 Git 源存储库和镜像 registry 的特定内容。当一个 Pipeline 被触发并执行时,这些详细信息会作为 params 添加。

  2. 创建 Pipeline:

    $ oc create -f <pipeline-yaml-file-name.yaml>

    或者,还可以从 Git 存储库直接执行 YAML 文件:

    $ oc create -f https://raw.githubusercontent.com/openshift/pipelines-tutorial/release-tech-preview-3/01_pipeline/04_pipeline.yaml
  3. 使用 tkn pipeline list 命令来验证是否在应用程序中添加了 Pipeline:

    $ tkn pipeline list

    检查输出来验证创建了 build-and-deploy Pipeline:

    NAME               AGE            LAST RUN   STARTED   DURATION   STATUS
    build-and-deploy   1 minute ago   ---        ---       ---        ---