Tekton实战案例-打包代码到镜像-发布到K8S集群

在k8s 集群上安装一个 nfs server

https://github.com/kubernetes-csi/csi-driver-nfs/tree/master/deploy/example/nfs-provisioner
kubectl create -f https://raw.githubusercontent.com/kubernetes-csi/csi-driver-nfs/master/deploy/example/nfs-provisioner/nfs-server.yaml
curl -skSL https://raw.githubusercontent.com/kubernetes-csi/csi-driver-nfs/v4.10.0/deploy/install-driver.sh | bash -s v4.10.0 --
root@k8s-master:~/tekton-and-argocd-in-practise/04-tekton-pipeline-in-practise/01-s2i-no-push# kubectl get pods
NAME                                         READY   STATUS      RESTARTS   AGE
nfs-server-7dd76c89c6-wxrph                  1/1     Running     0          48m


root@k8s-master:~/tekton-and-argocd-in-practise/04-tekton-pipeline-in-practise/01-s2i-no-push# kubectl get pods -n kube-system
NAME                                 READY   STATUS    RESTARTS       AGE
csi-nfs-controller-65c49c4f4-6r6m7   5/5     Running   3 (44m ago)    46m
csi-nfs-node-lllqw                   3/3     Running   1 (44m ago)    46m
csi-nfs-node-wmmrm                   3/3     Running   1 (45m ago)    46m
csi-nfs-node-x72jl                   3/3     Running   1 (45m ago)    46m

为 NFS 配置 CSI 存储类

root@k8s-master:~# kube apply -f /tmp/nfs-csi.yaml 
root@k8s-master:~# cat /tmp/nfs-csi.yaml 
---
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: nfs-csi
provisioner: nfs.csi.k8s.io
parameters:
  server: nfs-server.default.svc.cluster.local
  share: /
  # csi.storage.k8s.io/provisioner-secret is only needed for providing mountOptions in DeleteVolume
  # csi.storage.k8s.io/provisioner-secret-name: "mount-options"
  # csi.storage.k8s.io/provisioner-secret-namespace: "default"
reclaimPolicy: Delete
volumeBindingMode: Immediate
allowVolumeExpansion: true
mountOptions:
  - nfsvers=4.1
root@k8s-master:~/spring-boot-helloWorld# kubectl get sc
NAME      PROVISIONER      RECLAIMPOLICY   VOLUMEBINDINGMODE   ALLOWVOLUMEEXPANSION   AGE
nfs-csi   nfs.csi.k8s.io   Delete          Immediate           true                   7s
root@k8s-master:~/spring-boot-helloWorld# cat pvc-nfs-csi-dynamic.yaml
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-nfs-dynamic
  namespace: default
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 10Gi
  storageClassName: nfs-csi
root@k8s-master:~/spring-boot-helloWorld# kubectl apply -f pvc-nfs-csi-dynamic.yaml

 

root@k8s-master:~/spring-boot-helloWorld# cat maven-cache.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: maven-cache
  namespace: default  # 确保与PipelineRun同命名空间
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
  storageClassName: nfs-csi  # 使用您已有的StorageClass
root@k8s-master:~/spring-boot-helloWorld# kubectl apply -f maven-cache.yaml
root@k8s-master:~/tekton-and-argocd-in-practise/04-tekton-pipeline-in-practise/01-s2i-no-push# cat 01-task-git-clone.yaml 
apiVersion: tekton.dev/v1
kind: Task
metadata:
  name: git-clone
spec:
  description: Clone the code repository to the workspace. 
  params:
    - name: url
      type: string
      description: git url to clone
      default: ""
    - name: branch
      type: string
      description: git branch to checkout
      default: "main"
  workspaces:
    - name: source
      description: The git repo will be cloned onto the volume backing this workspace
  steps:
    - name: git-clone
      image: alpine/git:v2.45.2
      script: git clone -b $(params.branch) -v $(params.url) $(workspaces.source.path)/source
root@k8s-master:~/tekton-and-argocd-in-practise/04-tekton-pipeline-in-practise/01-s2i-no-push# cat 02-task-source-build.yaml 
apiVersion: tekton.dev/v1
kind: Task
metadata:
  name: build-to-package
spec:
  description: build application and package the files to image
  workspaces:
    - name: source
      description: The git repo that cloned onto the volume backing this workspace
  steps:
    - name: build
      image: maven:3.8.7-eclipse-temurin-11-alpine
      workingDir: $(workspaces.source.path)/source
      volumeMounts:
        - name: m2
          mountPath: /root/.m2
      script: |
        curl -sL https://gitee.com/mageedu/spring-boot-helloWorld/raw/main/maven/settings.xml -o /usr/share/maven/conf/settings.xml
        mvn clean install
  volumes:
    - name: m2
      persistentVolumeClaim:
        claimName: maven-cache
root@k8s-master:~/tekton-and-argocd-in-practise/04-tekton-pipeline-in-practise/01-s2i-no-push# cat 03-task-build-image.yaml 
apiVersion: tekton.dev/v1
kind: Task
metadata:
  name: image-build
spec:
  description: package the application files to image
  params:
    - name: dockerfile
      description: The path to the dockerfile to build (relative to the context)
      default: Dockerfile
    - name: image-url
      description: Url of image repository
    - name: image-tag
      description: Tag to apply to the built image
      default: latest
  workspaces:
    - name: source
  steps:
    - name: build-and-push-image
      #image: gcr.io/kaniko-project/executor:debug
      image: gcr.io/kaniko-project/executor:v1.23.2
      securityContext:
        runAsUser: 0
      command:
        - /kaniko/executor
      args:
        - --dockerfile=$(params.dockerfile)
        - --context=$(workspaces.source.path)/source
        - --insecure 
        - --insecure-pull
        - --skip-tls-verify
        - --skip-tls-verify-pull
        - --no-push
root@k8s-master:~/tekton-and-argocd-in-practise/04-tekton-pipeline-in-practise/01-s2i-no-push# cat 04-pipeline-source-to-image.yaml 
apiVersion: tekton.dev/v1
kind: Pipeline
metadata:
  name: source-to-image
spec:
  params:
    - name: git-url
    - name: pathToContext
      description: The path to the build context, used by Kaniko - within the workspace
      default: .
    - name: image-url
      description: Url of image repository
    - name: image-tag
      description: Tag to apply to the built image
  workspaces:
    - name: codebase
  tasks:
    - name: git-clone
      taskRef:
        name: git-clone
      params:
        - name: url
          value: "$(params.git-url)"
      workspaces:
        - name: source
          workspace: codebase
    - name: build-to-package
      taskRef:
        name: build-to-package
      workspaces:
        - name: source
          workspace: codebase
      runAfter:
        - git-clone
    - name: image-build
      taskRef:
        name: image-build
      params:
        - name: image-url
          value: "$(params.image-url)"
        - name: image-tag
          value: "$(params.image-tag)"
      workspaces:
        - name: source
          workspace: codebase
      runAfter:
        - build-to-package
root@k8s-master:~/tekton-and-argocd-in-practise/04-tekton-pipeline-in-practise/01-s2i-no-push# cat 05-pipelinerun-source-to-image.yaml 
apiVersion: tekton.dev/v1
kind: PipelineRun
metadata:
  name: s2i-no-push-run-00001
spec:
  pipelineRef:
    name: source-to-image
  params:
    - name: git-url
      #value: https://gitee.com/mageedu/spring-boot-helloWorld.git
      # 从本地仓库克隆代码,其Dockerfile中引用的Image修改为可访问的位置
      value: https://gitee.com/zyyangct/spring-boot-helloWorld.git
    - name: image-url
      value: zyyangct/spring-boot-helloWorld
    - name: image-tag
      value: latest
  workspaces:
    - name: codebase
      volumeClaimTemplate:
        spec:
          accessModes:
            - ReadWriteOnce
          resources:
            requests:
              storage: 1Gi
          storageClassName: nfs-csi

代码地址

https://gitee.com/zyyangct/spring-boot-helloWorld

 

将代码打包到镜像中并上传到阿里云镜像仓库

创建镜像仓库认证

root@k8s-master:~/tekton-and-argocd-in-practise/04-tekton-pipeline-in-practise/02-s2i-push-to-dockerhub# kubectl create secret generic docker-config --from-file=/root/.docker/config.json
root@k8s-master:~/tekton-and-argocd-in-practise/04-tekton-pipeline-in-practise/02-s2i-push-to-dockerhub# kubectl get secret
NAME            TYPE     DATA   AGE
docker-config   Opaque   1      29m
root@k8s-master:~/tekton-and-argocd-in-practise/04-tekton-pipeline-in-practise/02-s2i-push-to-dockerhub# cat 01-task-git-clone.yaml 
apiVersion: tekton.dev/v1
kind: Task
metadata:
  name: git-clone
spec:
  description: Clone the code repository to the workspace. 
  params:
    - name: url
      type: string
      description: git url to clone
      default: ""
    - name: branch
      type: string
      description: git branch to checkout
      default: "main"
  workspaces:
    - name: source
      description: The git repo will be cloned onto the volume backing this workspace
  steps:
    - name: git-clone
      image: alpine/git:v2.45.2
      script: git clone -b $(params.branch) -v $(params.url) $(workspaces.source.path)/source
root@k8s-master:~/tekton-and-argocd-in-practise/04-tekton-pipeline-in-practise/02-s2i-push-to-dockerhub# cat 02-task-source-build.yaml 
apiVersion: tekton.dev/v1
kind: Task
metadata:
  name: build-to-package
spec:
  description: build application and package the files to image
  workspaces:
    - name: source
      description: The git repo that cloned onto the volume backing this workspace
  steps:
    - name: build
      image: maven:3.8.7-eclipse-temurin-11-alpine
      workingDir: $(workspaces.source.path)/source
      volumeMounts:
        - name: m2
          mountPath: /root/.m2
      script: |
        curl -sL https://gitee.com/mageedu/spring-boot-helloWorld/raw/main/maven/settings.xml -o /usr/share/maven/conf/settings.xml
        mvn clean install
  volumes:
    - name: m2
      persistentVolumeClaim:
        claimName: maven-cache
root@k8s-master:~/tekton-and-argocd-in-practise/04-tekton-pipeline-in-practise/02-s2i-push-to-dockerhub# cat 03-task-build-image.yaml 
apiVersion: tekton.dev/v1
kind: Task
metadata:
  name: image-build-and-push
spec:
  description: package the application files to image
  params:
    - name: dockerfile
      description: The path to the dockerfile to build (relative to the context)
      default: Dockerfile
    - name: image-url
      description: Url of image repository
    - name: image-tag
      description: Tag to apply to the built image
      default: latest
  workspaces:
    - name: source
    - name: dockerconfig
      # Secret resource which contains identity to image registry
      mountPath: /kaniko/.docker
  steps:
    - name: image-build-and-push
      image: gcr.io/kaniko-project/executor:debug
      #image: gcr.io/kaniko-project/executor:v1.23.2
      securityContext:
        runAsUser: 0
      env:
        - name: DOCKER_CONFIG
          value: /kaniko/.docker
      command:
        - /kaniko/executor
      args:
        - --dockerfile=$(params.dockerfile)
        - --context=$(workspaces.source.path)/source
        - --insecure
        - --insecure-pull
        - --skip-tls-verify
        - --skip-tls-verify-pull
        - --skip-push-permission-check
        - --destination=$(params.image-url):$(params.image-tag)
root@k8s-master:~/tekton-and-argocd-in-practise/04-tekton-pipeline-in-practise/02-s2i-push-to-dockerhub# cat 04-pipeline-source-to-image.yaml 
apiVersion: tekton.dev/v1
kind: Pipeline
metadata:
  name: source-to-image
spec:
  params:
    - name: git-url
    - name: pathToContext
      description: The path to the build context, used by Kaniko - within the workspace
      default: .
    - name: image-url
      description: Url of image repository
    - name: image-tag
      description: Tag to apply to the built image
  workspaces:
    - name: codebase
    - name: docker-config
  tasks:
    - name: git-clone
      taskRef:
        name: git-clone
      params:
        - name: url
          value: "$(params.git-url)"
      workspaces:
        - name: source
          workspace: codebase
    - name: build-to-package
      taskRef:
        name: build-to-package
      workspaces:
        - name: source
          workspace: codebase
      runAfter:
        - git-clone
    - name: image-build-and-push
      taskRef:
        name: image-build-and-push
      params:
        - name: image-url
          value: "$(params.image-url)"
        - name: image-tag
          value: "$(params.image-tag)"
      workspaces:
        - name: source
          workspace: codebase
        - name: dockerconfig
          workspace: docker-config
      runAfter:
        - build-to-package
root@k8s-master:~/tekton-and-argocd-in-practise/04-tekton-pipeline-in-practise/02-s2i-push-to-dockerhub# cat 05-pipelinerun-source-to-image.yaml 
apiVersion: tekton.dev/v1
kind: PipelineRun
metadata:
  name: s2i-image-push-run-00001
spec:
  pipelineRef:
    name: source-to-image
  params:
    - name: git-url
      #value: https://gitee.com/mageedu/spring-boot-helloWorld.git
      value: https://gitee.com/zyyangct/spring-boot-helloWorld.git
    - name: image-url
      #value: ikubernetes/spring-boot-helloworld
      value: registry.cn-beijing.aliyuncs.com/yzybaseimages/tekton
    - name: image-tag
      value: helloWorld-v0.9.3
  workspaces:
    - name: codebase
      volumeClaimTemplate:
        spec:
          accessModes:
            - ReadWriteOnce
          resources:
            requests:
              storage: 1Gi
          storageClassName: nfs-csi
    - name: docker-config
      secret:
        secretName: docker-config

 

root@k8s-master:~/tekton-and-argocd-in-practise/04-tekton-pipeline-in-practise/02-s2i-push-to-dockerhub# kubectl apply -f ./
task.tekton.dev/git-clone created
task.tekton.dev/build-to-package created
task.tekton.dev/image-build-and-push created
pipeline.tekton.dev/source-to-image created
pipelinerun.tekton.dev/s2i-image-push-run-00001 created

 

阿里云镜像仓库也有了我刚打包的镜像

 

 

打包镜像时候添加 build-id

root@k8s-master:~/tekton-and-argocd-in-practise/04-tekton-pipeline-in-practise/03-s2i-auto-gen-build-id# cat 01-task-git-clone.yaml 
apiVersion: tekton.dev/v1
kind: Task
metadata:
  name: git-clone
spec:
  description: Clone the code repository to the workspace. 
  params:
    - name: url
      type: string
      description: git url to clone
      default: ""
    - name: branch
      type: string
      description: git branch to checkout
      default: "main"
  workspaces:
    - name: source
      description: The git repo will be cloned onto the volume backing this workspace
  steps:
    - name: git-clone
      image: alpine/git:v2.45.2
      script: git clone -b $(params.branch) -v $(params.url) $(workspaces.source.path)/source
root@k8s-master:~/tekton-and-argocd-in-practise/04-tekton-pipeline-in-practise/03-s2i-auto-gen-build-id# cat 02-task-source-build.yaml 
apiVersion: tekton.dev/v1
kind: Task
metadata:
  name: build-to-package
spec:
  description: build application and package the files to image
  workspaces:
    - name: source
      description: The git repo that cloned onto the volume backing this workspace
  steps:
    - name: build
      #image: maven:3.8-openjdk-11-slim
      image: maven:3.8.7-eclipse-temurin-11-alpine
      workingDir: $(workspaces.source.path)/source
      volumeMounts:
        - name: m2
          mountPath: /root/.m2
      script: |
        curl -sL https://gitee.com/mageedu/spring-boot-helloWorld/raw/main/maven/settings.xml -o /usr/share/maven/conf/settings.xml
        mvn clean install
  volumes:
    - name: m2
      persistentVolumeClaim:
        claimName: maven-cache
root@k8s-master:~/tekton-and-argocd-in-practise/04-tekton-pipeline-in-practise/03-s2i-auto-gen-build-id# cat 03-generate-build-id.yaml 
apiVersion: tekton.dev/v1
kind: Task
metadata:
  name: generate-build-id
spec:
  params:
    - name: version
      description: The version of the application
      type: string
  results:
    - name: datetime
      description: The current date and time
    - name: buildId
      description: The build ID
  steps:
    - name: generate-datetime
      image: registry.cn-beijing.aliyuncs.com/yzybaseimages/tekton:admin-box-v1.2
      script: |
        #!/usr/bin/env bash
        datetime=`date +%Y%m%d-%H%M%S`
        echo -n ${datetime} | tee $(results.datetime.path)
    - name: generate-buildid
      image: ikubernetes/admin-box:v1.2
      script: |
        #!/usr/bin/env bash
        buildDatetime=`cat $(results.datetime.path)`
        buildId=$(params.version)-${buildDatetime}
        echo -n ${buildId} | tee $(results.buildId.path)
root@k8s-master:~/tekton-and-argocd-in-practise/04-tekton-pipeline-in-practise/03-s2i-auto-gen-build-id# cat 04-task-build-image.yaml 
apiVersion: tekton.dev/v1
kind: Task
metadata:
  name: image-build-and-push
spec:
  description: package the application files to image
  params:
    - name: dockerfile
      description: The path to the dockerfile to build (relative to the context)
      default: Dockerfile
    - name: image-url
      description: Url of image repository
    - name: image-tag
      description: Tag to apply to the built image
  workspaces:
    - name: source
    - name: dockerconfig
      mountPath: /kaniko/.docker
  steps:
    - name: image-build-and-push
      #image: gcr.io/kaniko-project/executor:debug
      image: gcr.io/kaniko-project/executor:v1.23.2
      securityContext:
        runAsUser: 0
      env:
        - name: DOCKER_CONFIG
          value: /kaniko/.docker
      command:
        - /kaniko/executor
      args:
        - --dockerfile=$(params.dockerfile)
        - --context=$(workspaces.source.path)/source
        - --insecure
        - --insecure-pull
        - --skip-tls-verify
        - --skip-tls-verify-pull
        - --skip-push-permission-check
        - --destination=$(params.image-url):$(params.image-tag)
root@k8s-master:~/tekton-and-argocd-in-practise/04-tekton-pipeline-in-practise/03-s2i-auto-gen-build-id# cat 05-pipeline-source-to-image.yaml 
apiVersion: tekton.dev/v1
kind: Pipeline
metadata:
  name: source-to-image
spec:
  params:
    - name: git-url
    - name: pathToContext
      description: The path to the build context, used by Kaniko - within the workspace
      default: .
    - name: image-url
      description: Url of image repository
    - name: version
      description: The version of the application
      type: string
      default: "v0.9" 
  #results:
  #  - name: datetime
  #    description: The current date and time
  #  - name: buildId
  #    description: The build ID
  workspaces:
    - name: codebase
    - name: docker-config
  tasks:
    - name: git-clone
      taskRef:
        name: git-clone
      params:
        - name: url
          value: "$(params.git-url)"
      workspaces:
        - name: source
          workspace: codebase
    - name: build-to-package
      taskRef:
        name: build-to-package
      workspaces:
        - name: source
          workspace: codebase
      runAfter:
        - git-clone
    - name: generate-build-id
      taskRef:
        name: generate-build-id
      params:
        - name: version
          value: "$(params.version)"
      runAfter:
        - git-clone
    - name: image-build-and-push
      taskRef:
        name: image-build-and-push
      params:
        - name: image-url
          value: "$(params.image-url)"
        - name: image-tag
          value: "$(tasks.generate-build-id.results.buildId)"
      workspaces:
        - name: source
          workspace: codebase
        - name: dockerconfig
          workspace: docker-config
      runAfter:
        - generate-build-id
        - build-to-package
root@k8s-master:~/tekton-and-argocd-in-practise/04-tekton-pipeline-in-practise/03-s2i-auto-gen-build-id# cat 06-pipelinerun-source-to-image.yaml 
apiVersion: tekton.dev/v1
kind: PipelineRun
metadata:
  name: s2i-buildid-run-00001
spec:
  pipelineRef:
    name: source-to-image
  params:
    - name: git-url
      value: https://gitee.com/zyyangct/spring-boot-helloWorld.git
    - name: image-url
      value: registry.cn-beijing.aliyuncs.com/yzybaseimages/tekton
    - name: version
      value: helloworld-v0.9.4
  workspaces:
    - name: codebase
      volumeClaimTemplate:
        spec:
          accessModes:
            - ReadWriteOnce
          resources:
            requests:
              storage: 1Gi
          storageClassName: nfs-csi
    - name: docker-config
      secret:
        secretName: docker-config
root@k8s-master:~/tekton-and-argocd-in-practise/04-tekton-pipeline-in-practise/03-s2i-auto-gen-build-id# kubectl apply -f .
task.tekton.dev/git-clone created
task.tekton.dev/build-to-package created
task.tekton.dev/generate-build-id created
task.tekton.dev/image-build-and-push created
pipeline.tekton.dev/source-to-image created
pipelinerun.tekton.dev/s2i-buildid-run-00001 created

 

构建镜像并发布到K8S集群

root@k8s-master:~/tekton-and-argocd-in-practise/04-tekton-pipeline-in-practise/04-s2i-auto-deploy# cat 01-task-git-clone.yaml 
apiVersion: tekton.dev/v1
kind: Task
metadata:
  name: git-clone
spec:
  description: Clone the code repository to the workspace. 
  params:
    - name: url
      type: string
      description: git url to clone
      default: ""
    - name: branch
      type: string
      description: git branch to checkout
      default: "main"
  workspaces:
    - name: source
      description: The git repo will be cloned onto the volume backing this workspace
  steps:
    - name: git-clone
      image: alpine/git:v2.45.2
      script: git clone -b $(params.branch) -v $(params.url) $(workspaces.source.path)/source
root@k8s-master:~/tekton-and-argocd-in-practise/04-tekton-pipeline-in-practise/04-s2i-auto-deploy# cat 02-task-source-build.yaml 
apiVersion: tekton.dev/v1
kind: Task
metadata:
  name: build-to-package
spec:
  description: build application and package the files to image
  workspaces:
    - name: source
      description: The git repo that cloned onto the volume backing this workspace
  steps:
    - name: build
      #image: maven:3.8-openjdk-11-slim
      image: maven:3.8.7-eclipse-temurin-11-alpine
      workingDir: $(workspaces.source.path)/source
      volumeMounts:
        - name: m2
          mountPath: /root/.m2
      script: |
        curl -sL https://gitee.com/mageedu/spring-boot-helloWorld/raw/main/maven/settings.xml -o /usr/share/maven/conf/settings.xml
        mvn clean install
  volumes:
    - name: m2
      persistentVolumeClaim:
        claimName: maven-cache
root@k8s-master:~/tekton-and-argocd-in-practise/04-tekton-pipeline-in-practise/04-s2i-auto-deploy# cat 03-generate-build-id.yaml 
apiVersion: tekton.dev/v1
kind: Task
metadata:
  name: generate-build-id
spec:
  params:
    - name: version
      description: The version of the application
      type: string
  results:
    - name: datetime
      description: The current date and time
    - name: buildId
      description: The build ID
  steps:
    - name: generate-datetime
      image: ikubernetes/admin-box:v1.2
      script: |
        #!/usr/bin/env bash
        datetime=`date +%Y%m%d-%H%M%S`
        echo -n ${datetime} | tee $(results.datetime.path)
    - name: generate-buildid
      image: ikubernetes/admin-box:v1.2
      script: |
        #!/usr/bin/env bash
        buildDatetime=`cat $(results.datetime.path)`
        buildId=$(params.version)-${buildDatetime}
        echo -n ${buildId} | tee $(results.buildId.path)
root@k8s-master:~/tekton-and-argocd-in-practise/04-tekton-pipeline-in-practise/04-s2i-auto-deploy# cat 04-task-build-image.yaml 
apiVersion: tekton.dev/v1
kind: Task
metadata:
  name: image-build-and-push
spec:
  description: package the application files to image
  params:
    - name: dockerfile
      description: The path to the dockerfile to build (relative to the context)
      default: Dockerfile
    - name: image-url
      description: Url of image repository
    - name: image-tag
      description: Tag to apply to the built image
  workspaces:
    - name: source
    - name: dockerconfig
      mountPath: /kaniko/.docker
  steps:
    - name: image-build-and-push
      #image: gcr.io/kaniko-project/executor:debug
      image: gcr.io/kaniko-project/executor:v1.23.2
      securityContext:
        runAsUser: 0
      env:
        - name: DOCKER_CONFIG
          value: /kaniko/.docker
      command:
        - /kaniko/executor
      args:
        - --dockerfile=$(params.dockerfile)
        - --context=$(workspaces.source.path)/source
        - --insecure
        - --insecure-pull
        - --skip-tls-verify
        - --skip-tls-verify-pull
        - --skip-push-permission-check
        - --destination=$(params.image-url):$(params.image-tag)
root@k8s-master:~/tekton-and-argocd-in-practise/04-tekton-pipeline-in-practise/04-s2i-auto-deploy# cat 05-task-deploy.yaml 
apiVersion: tekton.dev/v1
kind: Task
metadata:
  name: deploy-using-kubectl
spec:
  workspaces:
    - name: source
      description: The git repo
  params:
    - name: deploy-config-file
      description: The path to the yaml file to deploy within the git source
    - name: image-url
      description: Image name including repository
    - name: image-tag
      description: Image tag
  steps:
    - name: update-yaml
      image: alpine:3.20
      command: ["sed"]
      args:
        - "-i"
        - "-e"
        - "s@__IMAGE__@$(params.image-url):$(params.image-tag)@g"
        - "$(workspaces.source.path)/source/deploy/$(params.deploy-config-file)"
    - name: run-kubectl
      #image: lachlanevenson/k8s-kubectl
      image: bitnami/kubectl
      command: ["kubectl"]
      args:
        - "apply"
        - "-f"
        - "$(workspaces.source.path)/source/deploy/$(params.deploy-config-file)"
root@k8s-master:~/tekton-and-argocd-in-practise/04-tekton-pipeline-in-practise/04-s2i-auto-deploy# cat 06-pipeline-source-to-image.yaml 
apiVersion: tekton.dev/v1
kind: Pipeline
metadata:
  name: source-to-image
spec:
  params:
    - name: git-url
    - name: pathToContext
      description: The path to the build context, used by Kaniko - within the workspace
      default: .
    - name: image-url
      description: Url of image repository
    - name: deploy-config-file
      description: The path to the yaml file to deploy within the git source
      default: all-in-one.yaml
    - name: version
      description: The version of the application
      type: string
      default: "v0.9" 
  #results:
  #  - name: datetime
  #    description: The current date and time
  #  - name: buildId
  #    description: The build ID
  workspaces:
    - name: codebase
    - name: docker-config
  tasks:
    - name: git-clone
      taskRef:
        name: git-clone
      params:
        - name: url
          value: "$(params.git-url)"
      workspaces:
        - name: source
          workspace: codebase
    - name: build-to-package
      taskRef:
        name: build-to-package
      workspaces:
        - name: source
          workspace: codebase
      runAfter:
        - git-clone
    - name: generate-build-id
      taskRef:
        name: generate-build-id
      params:
        - name: version
          value: "$(params.version)"
      runAfter:
        - git-clone
    - name: image-build-and-push
      taskRef:
        name: image-build-and-push
      params:
        - name: image-url
          value: "$(params.image-url)"
        - name: image-tag
          value: "$(tasks.generate-build-id.results.buildId)"
      workspaces:
        - name: source
          workspace: codebase
        - name: dockerconfig
          workspace: docker-config
      runAfter:
        - generate-build-id
        - build-to-package
    - name: deploy-to-cluster
      taskRef:
        name: deploy-using-kubectl
      workspaces:
        - name: source
          workspace: codebase
      params:
        - name: deploy-config-file
          value: $(params.deploy-config-file)
        - name: image-url
          value: $(params.image-url)
        - name: image-tag
          value: "$(tasks.generate-build-id.results.buildId)"
      runAfter:
        - image-build-and-push
root@k8s-master:~/tekton-and-argocd-in-practise/04-tekton-pipeline-in-practise/04-s2i-auto-deploy# cat 07-rbac.yaml 
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: helloworld-admin
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: helloworld-admin
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
- kind: ServiceAccount
  name: helloworld-admin
  namespace: default
root@k8s-master:~/tekton-and-argocd-in-practise/04-tekton-pipeline-in-practise/04-s2i-auto-deploy# cat 08-pipelinerun-source-to-image.yaml 
apiVersion: tekton.dev/v1
kind: PipelineRun
metadata:
  name: s2i-buildid-run-00002
spec:
  pipelineRef:
    name: source-to-image
  taskRunSpecs:
    - pipelineTaskName: deploy-to-cluster
      serviceAccountName: helloworld-admin
  params:
    - name: git-url
      value: https://gitee.com/zyyangct/spring-boot-helloWorld.git
    - name: image-url
      value: registry.cn-beijing.aliyuncs.com/yzybaseimages/tekton
    - name: version
      value: v0.9
  workspaces:
    - name: codebase
      volumeClaimTemplate:
        spec:
          accessModes:
            - ReadWriteOnce
          resources:
            requests:
              storage: 1Gi
          storageClassName: nfs-csi
    - name: docker-config
      secret:
        secretName: docker-config
root@k8s-master:~/tekton-and-argocd-in-practise/04-tekton-pipeline-in-practise/04-s2i-auto-deploy# kubectl apply -f .
task.tekton.dev/git-clone configured
task.tekton.dev/build-to-package configured
task.tekton.dev/generate-build-id configured
task.tekton.dev/image-build-and-push configured
task.tekton.dev/deploy-using-kubectl created
pipeline.tekton.dev/source-to-image configured
serviceaccount/helloworld-admin created
clusterrolebinding.rbac.authorization.k8s.io/helloworld-admin created
pipelinerun.tekton.dev/s2i-buildid-run-00002 created

验证

root@k8s-master:~/tekton-and-argocd-in-practise/04-tekton-pipeline-in-practise/04-s2i-auto-deploy# kubectl get pods -n hello
NAME                                      READY   STATUS    RESTARTS   AGE
spring-boot-helloworld-5947fdd9d7-56hz9   1/1     Running   0          48s
root@k8s-master:~/tekton-and-argocd-in-practise/04-tekton-pipeline-in-practise/04-s2i-auto-deploy# kubectl get svc -n hello
NAME                     TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
spring-boot-helloworld   NodePort   10.101.44.176   <none>        80:30162/TCP   58s

posted @ 2025-06-17 14:21  Maniana  阅读(30)  评论(0)    收藏  举报