Tekton CICD demo 3

上一章中我们使用task2正确输出了 task1 生成的 commitid

这一章我要添加一个 step,用来在task2中构建docker image,为了使用 commit.txt 这次我们不直接mount,而是重新编写一个 kaniko的启动脚本

因为添加了一个新的 output 所以pipeline 和 task 都需要修改

  1. 添加docker resource

docker_resource.yaml

apiVersion: tekton.dev/v1alpha1
kind: PipelineResource
metadata:
  name: tektongolang-image
  namespace: lizhe
spec:
  type: image
  params:
    - name: url
      value: libaibai/tektongolang

2. 创建一个新的 kaniko

exec.sh

此脚本接收4个参数分别是

  • $1 dockerfile
  • $2 docker context path
  • $3 docker destination
  • $4 commitid.txt path
set -e

if [ $# -lt 4 ]; then
echo "Usage:  <path to Dockerfile> <context directory> <docker_url> <commitid_path>"
exit 1
fi

dockerfile=$1
context=$2
destination=$3
commitid=`cat $4`
echo "building start..."
echo "commitid:"${commitid}
/kaniko/executor    --dockerfile=$dockerfile \
                --destination=$destination:$commitid \
                --context=$context

Dockerfile

FROM gcr.io/kaniko-project/executor:debug
COPY exec.sh /exec.sh
CMD [ "/busybox/sh" ]

3. 修改 build img task

build_img_task.yaml

apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: build-img
  namespace: lizhe
spec:
  resources:
    outputs:
      - name: builtImage
        type: image
  steps:
    - name: echo-commitid
      image: ubuntu
      command: ["/bin/bash", "-c", "cat /workspace/git-source/commitid.txt"]
    - name: build-image-with-commitid-and-push
      image: libaibai/kanikocid:latest
      env:
        - name: "DOCKER_CONFIG"
          value: "/tekton/home/.docker/"
      command: ["/busybox/sh", "-c", "/exec.sh Dockerfile /workspace/git-source $(resources.outputs.builtImage.url) /workspace/git-source/commitid.txt"]
  workspaces:
    - name: build-workspace
      description: |
        The folder where will be shared among the tasks
      optional: false
      mountPath: /workspace

4. 修改pipeline

pipeline_demo.yaml

apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: pipeline-demo
  namespace: lizhe
spec:
  resources:
    - name: source-repo
      type: git
    - name: tektongolang-image
      type: image
  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
      resources:
        outputs:
          - name: builtImage
            resource: tektongolang-image
      workspaces:
        - name: build-workspace
          workspace: build-workspace

5. 修改pipeline run

pipeline_demo_run.yaml

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
    - name: tektongolang-image
      resourceRef:
        name: tektongolang-image
  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
Send a Message