Tekton CICD demo 2

这一步的主要目的是把上一章中的 task 转换成 pipeline,然后添加一个新的task (build image)

先把之前的task转换成 pipeline

apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: pipeline-demo
  namespace: lizhe
spec:
  resources:
    - name: source-repo
      type: git
  tasks:
    - name: git-commitid
      taskRef:
        name: get-commitid
      resources:
        inputs:
          - name: git-source
            resource: source-repo

然后新建一个用于 build image 的task

apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: build-img
  namespace: lizhe
spec:
  steps:
    - name: echo-commitid
      image: ubuntu
      command: ["/bin/bash", "-c", "cat /workspace/git-source/commitid.txt"]

添加这个 task 到 pipeline

apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: pipeline-demo
  namespace: lizhe
spec:
  resources:
    - name: source-repo
      type: git
  tasks:
    - name: git-commitid
      taskRef:
        name: get-commitid
      resources:
        inputs:
          - name: git-source
            resource: source-repo
    - name: build-img
      taskRef:
        name: build-img

新建一个pipeline run

apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: pipeline-demo
  namespace: lizhe
spec:
  resources:
    - name: source-repo
      type: git
  tasks:
    - name: git-commitid
      taskRef:
        name: get-commitid
      resources:
        inputs:
          - name: git-source
            resource: source-repo
    - name: build-img
      taskRef:
        name: build-img

直接运行这个 pipeline run 的话会出现异常

可以看到,在 git pull 命令执行之前,build img (目前只是尝试输出 git task 中生成的 commitid )失败了,并且可以观察到两个task是并行的,实际上我们这里,task2 对 task1 是有依赖的,而且似乎两个task也没有共享 workspace

对之前的 2个 task ,1个 pipeline 和 1个 pipeline run 进行修改,添加一个 pvc ,用于共享文件夹

官方文档中提到 access mode 会影响 task 的并行,所以这里需要把pipeline改成串行的

PersistentVolumeClaim volumes are a good choice for sharing data among Tasks within a Pipeline. Beware that the access mode configured for the PersistentVolumeClaim effects how you can use the volume for parallel Tasks in a Pipeline. See Specifying workspace order in a Pipeline and Affinity Assistants for more information about this. There are two ways of using PersistentVolumeClaims as a VolumeSource.

pipeline

apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: pipeline-demo
  namespace: lizhe
spec:
  resources:
    - name: source-repo
      type: git
  workspaces:
    - name: build-workspace
      optional: false
  tasks:
    - name: git-commitid
      taskRef:
        name: get-commitid
      resources:
        inputs:
          - name: git-source
            resource: source-repo
      workspaces:
        - name: build-workspace
          workspace: build-workspace
    - name: build-img
      runAfter: [git-commitid]
      taskRef:
        name: build-img
      workspaces:
        - name: build-workspace
          workspace: build-workspace

pipeline run

apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
  name: pipeline-demo-run
  namespace: lizhe
spec:
  serviceAccountName: build-bot-sa
  pipelineRef:
    name: pipeline-demo
  resources:
    - name: source-repo
      resourceRef:
        name: tektongolang-git
  workspaces:
    - name: build-workspace # this workspace name must be declared in the Pipeline
      volumeClaimTemplate:
        spec:
          storageClassName: "local-path"
          accessModes:
            - ReadWriteOnce # access mode may affect how you can use this volume in parallel tasks
          resources:
            requests:
              storage: 1Gi

task1

apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: get-commitid
  namespace: lizhe
spec:
  resources:
    inputs:
      - name: git-source
        type: git
  steps:
    - name: get-commitid
      image: libaibai/gitcid
      env:
        - name: "DOCKER_CONFIG"
          value: "/tekton/home/.docker/"
      command: ["/bin/bash", "-c", "/getcommitid.sh /workspace/git-source"]
  workspaces:
    - name: build-workspace
      description: |
        The folder where will be shared among the tasks
      optional: false
      mountPath: /workspace

task2

apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: build-img
  namespace: lizhe
spec:
  steps:
    - name: echo-commitid
      image: ubuntu
      command: ["/bin/bash", "-c", "cat /workspace/git-source/commitid.txt"]
  workspaces:
    - name: build-workspace
      description: |
        The folder where will be shared among the tasks
      optional: false
      mountPath: /workspace

经过上面的修改,我们的代码最终变成了

  1. 通过 pvc template 共享一个pv
  2. 两个task由并行变成了串行
  3. task1 通过 input resource 从git下载了代码
  4. task1 本身通过 libaibai/gitcid 镜像输出 commitid 到 workspace/commitid.txt
  5. task2 读取 workspace/commitid.txt

Send a Message