tekton基础

task和taskrun示例

[root@master 02-tekton-basics]# cat 01-task-hello.yaml 
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: hello
spec:
  steps:
    - name: say-hello
      image: alpine:3.15
      command: ['/bin/sh']
      args: ['-c', 'echo Hello World']
[root@master 02-tekton-basics]# cat 01-taskrun-hello.yaml 
apiVersion: tekton.dev/v1beta1
kind: TaskRun
metadata:
  name: hello-run-00001
spec:
  taskRef:
    kind: Task
    name: hello
[root@master 02-tekton-basics]# kubectl apply -f 01-task-hello.yaml -f 01-taskrun-hello.yaml 
task.tekton.dev/hello configured
taskrun.tekton.dev/hello-run-00001 created

 task和taskrun示例

[root@master 02-tekton-basics]# kubectl apply -f 02-task-with-params.yaml 
task.tekton.dev/hello-params created
[root@master 02-tekton-basics]# cat 02-task-with-params.yaml 
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: hello-params
spec:
  params:
    - name: target
      type: string
      description: Name of somebody or something to greet
      default: Yang
  steps:
    - name: say-hello
      image: alpine:3.15
      command:
        - /bin/sh
      args: ['-c', 'echo Hello $(params.target)']
[root@master 02-tekton-basics]# tkn task start hello-params --showlog
? Value for param `target` of type `string`? (Default is `Yang`) Yang
TaskRun started: hello-params-run-lqnz5
Waiting for logs to be available...
[say-hello] Hello Yang
[root@master 02-tekton-basics]# tkn task start hello-params --showlog -p target=Tekton
TaskRun started: hello-params-run-l782c
Waiting for logs to be available...
[say-hello] Hello Tekton

task定义多个step方法

[root@master 02-tekton-basics]# kubectl apply -f 03-task-multi-steps.yaml 
task.tekton.dev/multiple created

[root@master 02-tekton-basics]# cat 03-task-multi-steps.yaml 
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: multiple
spec:
  steps:
    - name: first
      image: alpine:3.15
      command:
        - /bin/sh
      args: ['-c', 'echo First Step']
    - name: second
      image: alpine:3.15
      command:
        - /bin/sh
      args: ['-c', 'echo Second Step']

[root@master 02-tekton-basics]# tkn task start multiple --showlog
TaskRun started: multiple-run-lghg2
Waiting for logs to be available...
[first] First Step

[second] Second Step

 task step定义多行

[root@master 02-tekton-basics]# cat 04-task-step-with-script.yaml 
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: script
spec:
  steps:
    - name: step-with-script
      image: alpine:3.15
      script: |
        #!/bin/sh
        echo "Step with Script..."
        echo "Installing necessary tooling"
        apk add curl
        curl -s www.baidu.com && echo "Success" || echo "Fail" 
        echo "All done!"
[root@master 02-tekton-basics]# kubectl apply -f 04-task-step-with-script.yaml 
task.tekton.dev/script created
[root@master 02-tekton-basics]# tkn task start script --showlog
TaskRun started: script-run-fflhs
Waiting for logs to be available...
[step-with-script] Step with Script...
[step-with-script] Installing necessary tooling
[step-with-script] fetch https://dl-cdn.alpinelinux.org/alpine/v3.15/main/x86_64/APKINDEX.tar.gz
[step-with-script] fetch https://dl-cdn.alpinelinux.org/alpine/v3.15/community/x86_64/APKINDEX.tar.gz
[step-with-script] (1/5) Installing ca-certificates (20220614-r0)
[step-with-script] (2/5) Installing brotli-libs (1.0.9-r5)
[step-with-script] (3/5) Installing nghttp2-libs (1.46.0-r0)
[step-with-script] (4/5) Installing libcurl (7.80.0-r3)
[step-with-script] (5/5) Installing curl (7.80.0-r3)
[step-with-script] Executing busybox-1.34.1-r7.trigger
[step-with-script] Executing ca-certificates-20220614-r0.trigger
[step-with-script] OK: 8 MiB in 19 packages
[step-with-script] <!DOCTYPE html>
[step-with-script] <!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必读</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
[step-with-script] Success
[step-with-script] All done!

task脚本和参数结合使用

[root@master 02-tekton-basics]# tkn task start logger --showlog -p text="TaskRun finished"
TaskRun started: logger-run-mpjnr
Waiting for logs to be available...
[log] [2022-10-26 16:59:48] - TaskRun finished

[root@master 02-tekton-basics]# cat 05-task-script-and-parameters.yaml 
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: logger
spec:
  params:
    - name: text
      type: string
      description: something to log
      default: "-"
  steps:
    - name: log
      image: alpine:3.15
      script: |
        apk add -q tzdata
        cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
        DATETIME=$(date "+%F %T")
        echo [$DATETIME] - $(params.text)

定义pipeline并引用task hello和multiple,runAfter会先运行,如果注释掉runAfter两个task会同时运行

[root@master 02-tekton-basics]# cat 06-pipeline-demo.yaml 
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: pipeline-demo
spec:
  tasks:
    - name: first-task
      taskRef:
        name: hello
    - name: second-task
      taskRef:
        name: multiple
      runAfter:
        - first-task
[root@master 02-tekton-basics]# kubectl apply -f 06-pipeline-demo.yaml 
pipeline.tekton.dev/pipeline-demo created

[root@master 02-tekton-basics]# tkn pipeline start pipeline-demo --showlog
PipelineRun started: pipeline-demo-run-6l4v8
Waiting for logs to be available...
[first-task : say-hello] Hello World

[second-task : first] First Step

[second-task : second] Second Step

定义pipeline和pipelinerun,在dashboard中查看log

[root@master 02-tekton-basics]# kubectl apply -f 08-pipeline-with-params.yaml 
pipeline.tekton.dev/pipeline-with-params created
[root@master 02-tekton-basics]# kubectl apply -f 09-pipelinerun-with-params.yaml 
pipelinerun.tekton.dev/pipeline-with-params-run-xxxxy created
[root@master 02-tekton-basics]# cat 08-pipeline-with-params.yaml 
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: pipeline-with-params
spec:
  params:
    - name: text
      type: string
    - name: username
      type: string
  tasks:
    - name: task-one
      taskRef:
        name: hello-params
      params:
        - name: target
          value: $(params.username)
    - name: task-two
      taskRef:
        name: logger
      params:
        - name: text
          value: $(params.text)
[root@master 02-tekton-basics]# cat 09-pipelinerun-with-params.yaml 
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
  name: pipeline-with-params-run-xxxxy
  namespace: default
spec:
  params:
  - name: username
    value: "Yang CloudNative Course"
  - name: text
    value: "Something has happened ~~."
  pipelineRef:
    name: pipeline-with-params
  serviceAccountName: default
  timeout: 10m0s
[root@master 02-tekton-basics]# tkn pipelinerun list
NAME                             STARTED          DURATION   STATUS
pipeline-with-params-run-xxxxy   1 minute ago     3s         Succeeded
pipeline-with-params-run-xxxxx   11 minutes ago   3s         Succeeded
pipeline-demo-run-6l4v8          27 minutes ago   6s         Succeeded
hello-goodbye-run                4 hours ago      18s        Succeeded

 

定义pipeline上各task的次序

[root@master 02-tekton-basics]# kubectl apply -f 10-pipeline-with-ordered-task.yaml 
pipeline.tekton.dev/pipeline-task-ordering created
[root@master 02-tekton-basics]# cat 10-pipeline-with-ordered-task.yaml 
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: pipeline-task-ordering
spec:
  tasks:
    - name: task-a
      taskRef:
        name: logger
      params:
      - name: text
        value: "task-A executed"
    - name: task-b
      taskRef:
        name: logger
      params:
      - name: text
        value: "Executed after task-A"
      runAfter: ["task-a"]
    - name: task-c
      taskRef:
        name: logger
      params:
      - name: text
        value: "Executed after task-A"
      runAfter: ["task-a"]
    - name: task-d
      taskRef:
        name: logger
      params:
      - name: text
        value: "Executed after task-B and task-C"
      runAfter: ["task-b", "task-c"]
[root@master 02-tekton-basics]# tkn pipeline list
NAME                     AGE              LAST RUN                         STARTED          DURATION   STATUS
hello-goodbye            4 hours ago      hello-goodbye-run                4 hours ago      18s        Succeeded
pipeline-demo            38 minutes ago   pipeline-demo-run-6l4v8          35 minutes ago   6s         Succeeded
pipeline-task-ordering   8 seconds ago    ---                              ---              ---        ---
pipeline-with-params     19 minutes ago   pipeline-with-params-run-xxxxy   8 minutes ago    3s         Succeeded
[root@master 02-tekton-basics]# tkn pipeline start pipeline-task-ordering --showlog
PipelineRun started: pipeline-task-ordering-run-rz8hk
Waiting for logs to be available...
[task-a : log] [2022-10-26 17:41:21] - task-A executed

[task-b : log] [2022-10-26 17:41:30] - Executed after task-A
[task-c : log] [2022-10-26 17:41:30] - Executed after task-A


[task-d : log] [2022-10-26 17:41:39] - Executed after task-B and task-C

workspace示例

[root@master 03-tekton-advanced]# cat 01-task-workspace-demo.yaml 
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: workspace-demo
spec:
  params:
  - name: target
    type: string
    default: MageEdu
  steps:
    - name: write-message
      image: alpine:3.16
      script: |
        #!/bin/sh
        set -xe
        if [ "$(workspaces.messages.bound)" == "true" ] ; then
          echo "Hello $(params.target)" > $(workspaces.messages.path)/message
          #cat $(workspaces.messages.path)/message
        fi
        echo "Mount Path: $(workspaces.messages.path)"
        echo "Volume Name: $(workspaces.messages.volume)"
    - name: print-message
      image: alpine:3.16
      script: |
        #!/bin/sh
        set -xe
        if [ "$(workspaces.messages.bound)" == "true" ] ; then
          cat $(workspaces.messages.path)/message
        fi
  workspaces:
    - name: messages
      description: |
        The folder where we write the message to. If no workspace
        is provided then the message will not be written.
      optional: true
      mountPath: /data
[root@master 03-tekton-advanced]# tkn task start workspace-demo --showlog -p target="Yang Cloud Native Course" -w name=messages,emptyDir=""
TaskRun started: workspace-demo-run-8drn5
Waiting for logs to be available...
[write-message] + '[' true '==' true ]
[write-message] + echo 'Hello Yang Cloud Native Course'
[write-message] + echo 'Mount Path: /data'
[write-message] + echo 'Volume Name: ws-zvkph'
[write-message] Mount Path: /data
[write-message] Volume Name: ws-zvkph

[print-message] + '[' true '==' true ]
[print-message] + cat /data/message
[print-message] Hello Yang Cloud Native Course

 

pipeline的workspace

[root@master 03-tekton-advanced]# kubectl apply -f 03-pipeline-workspace.yaml 
pipeline.tekton.dev/pipeline-source-lister created
[root@master 03-tekton-advanced]# cat 03-pipeline-workspace.yaml 
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: pipeline-source-lister
spec:
  workspaces:
  - name: codebase
  params:
  - name: git-url
    type: string
    description: Git repository url to be cloned
  tasks:
  - name: git-clone
    taskRef:
      name: source-lister
    workspaces:
    - name: source
      workspace: codebase
    params:
    - name: git-repo
      value: $(params.git-url)
[root@master 03-tekton-advanced]# tkn pipeline list
NAME                     AGE              LAST RUN                           STARTED       DURATION   STATUS
hello-goodbye            7 hours ago      hello-goodbye-run                  7 hours ago   18s        Succeeded
pipeline-demo            3 hours ago      pipeline-demo-run-6l4v8            3 hours ago   6s         Succeeded
pipeline-source-lister   29 seconds ago   ---                                ---           ---        ---
pipeline-task-ordering   2 hours ago      pipeline-task-ordering-run-rz8hk   2 hours ago   26s        Succeeded
pipeline-with-params     3 hours ago      pipeline-with-params-run-xxxxy     2 hours ago   3s         Succeeded

[root@master 03-tekton-advanced]# tkn pipeline start pipeline-source-lister --showlog -p git-url=https://gitee.com/mageedu/spring-boot-helloWorld.git -w name=source,emptyDir="" -w name=codebase,emptyDir=""
PipelineRun started: pipeline-source-lister-run-t877b
Waiting for logs to be available...
[git-clone : git-clone] Cloning into '/workspace/source/source'...
[git-clone : git-clone] POST git-upload-pack (175 bytes)
[git-clone : git-clone] POST git-upload-pack (367 bytes)

[git-clone : list-files] Dockerfile
[git-clone : list-files] Jenkinsfile
[git-clone : list-files] LICENSE
[git-clone : list-files] README.md
[git-clone : list-files] deploy
[git-clone : list-files] pom.xml
[git-clone : list-files] src

 在k8s上部署一个nfs服务器

[root@master tekton-and-argocd-in-practise]# kubectl create ns nfs
[root@master tekton-and-argocd-in-practise]# kubectl create -f https://raw.githubusercontent.com/kubernetes-csi/csi-driver-nfs/master/deploy/example/nfs-provisioner/nfs-server.yaml -n nfs
[root@master tekton-and-argocd-in-practise]# kubectl get pods -n nfs
NAME                          READY   STATUS    RESTARTS   AGE
nfs-server-5847b99d99-snfrp   1/1     Running   0          19m
[root@master tekton-and-argocd-in-practise]# curl -skSL https://raw.githubusercontent.com/kubernetes-csi/csi-driver-nfs/v3.1.0/deploy/install-driver.sh | bash -s v3.1.0 --
[root@master tekton-and-argocd-in-practise]# kubectl get pods -n kube-system
NAME                                       READY   STATUS             RESTARTS          AGE
csi-nfs-controller-65cf7d587-mdr9f         3/3     Running            0                 5m11s
csi-nfs-controller-65cf7d587-n7hmv         3/3     Running            0                 5m11s
csi-nfs-node-6tcgt                         3/3     Running            0                 3m34s
csi-nfs-node-925sn                         3/3     Running            0                 3m34s
csi-nfs-node-xt4qg                         3/3     Running            0                 3m34s
[root@master tmp]# cat nfs-csi.yaml 
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: nfs-csi
provisioner: nfs.csi.k8s.io
parameters:
  server: nfs-server.nfs.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
mountOptions:
  - nfsvers=4.1
[root@master tmp]# kubectl get sc
NAME      PROVISIONER      RECLAIMPOLICY   VOLUMEBINDINGMODE   ALLOWVOLUMEEXPANSION   AGE
nfs-csi   nfs.csi.k8s.io   Delete          Immediate           false                  3s
[root@master tmp]# kubectl get sc
NAME      PROVISIONER      RECLAIMPOLICY   VOLUMEBINDINGMODE   ALLOWVOLUMEEXPANSION   AGE
nfs-csi   nfs.csi.k8s.io   Delete          Immediate           false                  3s
[root@master tmp]# kubectl apply -f https://raw.githubusercontent.com/kubernetes-csi/csi-driver-nfs/master/deploy/example/pvc-nfs-csi-dynamic.yaml
persistentvolumeclaim/pvc-nfs-dynamic created
[root@master tmp]# kubectl get pvc
NAME              STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
pvc-nfs-dynamic   Bound    pvc-e9d5ac2a-f4d5-47eb-87b6-81220d435e5e   10Gi       RWX            nfs-csi        5s
[root@master tmp]# kubectl get sc
NAME      PROVISIONER      RECLAIMPOLICY   VOLUMEBINDINGMODE   ALLOWVOLUMEEXPANSION   AGE
nfs-csi   nfs.csi.k8s.io   Delete          Immediate           false                  2m49s

 pipelinerun上的workspace和parameters

[root@master 03-tekton-advanced]# kubectl apply -f 04-pipeline-worlspace-02.yaml 
pipeline.tekton.dev/volume-share configured
pipelinerun.tekton.dev/volume-share-run-xxxx created
[root@master 03-tekton-advanced]# cat 04-pipeline-worlspace-02.yaml 
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: volume-share
spec:
  params:
    - name: git-url
      type: string
  workspaces:
    - name: codebase
  tasks:
    - name: fetch-from-source
      params:
        - name: url
          value: $(params.git-url)
      taskSpec:
        workspaces:
          - name: source
        params:
          - name: url
        steps:
          - name: git-clone
            image: alpine/git:v2.36.1
            script: git clone -v $(params.url) $(workspaces.source.path)/source
      workspaces:
        - name: source
          workspace: codebase
    - name: source-lister
      runAfter:
        - fetch-from-source
      taskSpec:
        steps:
          - name: list-files
            image: alpine:3.16
            script: ls $(workspaces.source.path)/source
        workspaces:
          - name: source
      workspaces:
        - name: source
          workspace: codebase
---
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
  name: volume-share-run-xxxx
spec:
  pipelineRef:
    name: volume-share
  params:
    - name: git-url
      value: https://gitee.com/mageedu/spring-boot-helloWorld.git
  workspaces:
    - name: codebase
      volumeClaimTemplate:
        spec:
          accessModes:
            - ReadWriteOnce
          resources:
            requests:
              storage: 1Gi
          storageClassName: nfs-csi
[root@master 03-tekton-advanced]# kubectl get pvc
NAME             STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
pvc-139c72dc23   Bound    pvc-8d2ab544-eca6-4c06-a126-ad6c8eb2c4b4   1Gi        RWO            nfs-csi        2m12s

 

 

生产示例

[root@master 03-tekton-advanced]# kubectl apply -f 05-pipeline-source-to-package.yaml 
pipeline.tekton.dev/source-2-package created
pipelinerun.tekton.dev/source-2-package-run-001 created
[root@master 03-tekton-advanced]# cat 05-pipeline-source-to-package.yaml 
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: source-2-package
spec:
  params:
    - name: git-url
      type: string
  workspaces:
    - name: codebase
  tasks:
    - name: fetch-from-source
      params:
        - name: url
          value: $(params.git-url)
      taskSpec:
        workspaces:
          - name: source
        params:
          - name: url
        steps:
          - name: git-clone
            image: alpine/git:v2.36.1
            script: git clone -v $(params.url) $(workspaces.source.path)/source
      workspaces:
        - name: source
          workspace: codebase
    - name: build-package
      runAfter:
        - fetch-from-source
      taskSpec:
        steps:
          - name: build
            image: maven:3.8-openjdk-11-slim
            workingDir: $(workspaces.source.path)/source
            script: |
              mvn clean install
        workspaces:
          - name: source
      workspaces:
        - name: source
          workspace: codebase
---
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
  name: source-2-package-run-001
spec:
  pipelineRef:
    name: source-2-package
  params:
    - name: git-url
      value: https://gitee.com/mageedu/spring-boot-helloWorld.git
  workspaces:
    - name: codebase
      volumeClaimTemplate:
        spec:
          accessModes:
            - ReadWriteOnce
          resources:
            requests:
              storage: 1Gi
          storageClassName: nfs-csi

在task和step上使用volume,运行两次

[root@master 03-tekton-advanced]# kubectl apply -f 06-pipeline-source-to-package.yaml 
persistentvolumeclaim/maven-cache created
pipeline.tekton.dev/source-2-package configured
pipelinerun.tekton.dev/source-2-package-run-003 created
[root@master 03-tekton-advanced]# cat 06-pipeline-source-to-package.yaml 
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: maven-cache
spec:
  storageClassName: nfs-csi
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 5Gi
---
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: source-2-package
spec:
  params:
    - name: git-url
      type: string
  workspaces:
    - name: codebase
  tasks:
    - name: fetch-from-source
      params:
        - name: url
          value: $(params.git-url)
      taskSpec:
        workspaces:
          - name: source
        params:
          - name: url
        steps:
          - name: git-clone
            image: alpine/git:v2.36.1
            script: git clone -v $(params.url) $(workspaces.source.path)/source
      workspaces:
        - name: source
          workspace: codebase
    - name: build-package
      runAfter:
        - fetch-from-source
      taskSpec:
        steps:
          - name: build
            image: maven:3.8-openjdk-11-slim
            workingDir: $(workspaces.source.path)/source
            volumeMounts:
              - name: m2
                mountPath: /root/.m2
            script: mvn clean test package
        workspaces:
          - name: source
        volumes:
          - name: m2
            persistentVolumeClaim:
              claimName: maven-cache
      workspaces:
        - name: source
          workspace: codebase
---
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
  name: source-2-package-run-003
spec:
  pipelineRef:
    name: source-2-package
  params:
    - name: git-url
      value: https://gitee.com/mageedu/spring-boot-helloWorld.git
      #value: http://code.gitlab.svc.cluster.local/root/spring-boot-helloWorld.git
  workspaces:
    - name: codebase
      volumeClaimTemplate:
        spec:
          accessModes:
            - ReadWriteOnce
          resources:
            requests:
              storage: 1Gi
          storageClassName: nfs-csi
[root@master 03-tekton-advanced]# tkn pipelinerun logs -f source-2-package-run-003 

修改一下pipelinerun的name为source-2-package-run-004在运行一次

这次很快10秒完成,说明第二次用到了缓存的数据

[root@master 03-tekton-advanced]# kubectl apply -f 06-pipeline-source-to-package.yaml 
persistentvolumeclaim/maven-cache unchanged
pipeline.tekton.dev/source-2-package configured
pipelinerun.tekton.dev/source-2-package-run-004 created
[root@master 03-tekton-advanced]# tkn pipelinerun logs -f source-2-package-run-004

使用Results进行数据传递

[root@master 03-tekton-advanced]# kubectl apply -f 07-results-demo.yaml 
task.tekton.dev/generate-buildid created
[root@master 03-tekton-advanced]# cat 07-results-demo.yaml 
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: generate-buildid
spec:
  params:
    - 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
  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@master 03-tekton-advanced]# tkn task list
NAME               DESCRIPTION   AGE
generate-buildid                 7 seconds ago
goodbye                          1 hour ago
hello                            1 week ago
hello-params                     1 week ago
logger                           1 week ago
multiple                         1 week ago
script                           1 week ago
source-lister                    1 hour ago
workspace-demo                   1 hour ago

[root@master 03-tekton-advanced]# tkn task start generate-buildid  --showlog
? Value for param `version` of type `string`? (Default is `v0.9`) v0.9.1
TaskRun started: generate-buildid-run-6v5pk
Waiting for logs to be available...
[generate-datetime] 20221102-095210

[generate-buildid] v0.9.1-20221102-095210

[root@master 03-tekton-advanced]# tkn task start generate-buildid  --showlog -p version='0.9.2'
TaskRun started: generate-buildid-run-6ds97
Waiting for logs to be available...
[generate-datetime] 20221102-095237

[generate-buildid] 0.9.2-20221102-095237

 

posted @ 2022-10-26 17:25  Maniana  阅读(190)  评论(0编辑  收藏  举报