一篇带你创建 Tekton 流水线

2025-05-27 0 39

一篇带你创建 Tekton 流水线

前面我们创建的两个任务 test 和 build-and-push 都已经完成了,我们还可以创建一个流水线来将这两个任务组织起来,形成一个流水线,这里就是我们要使用的 Pipeline 这个 CRD 对象。

创建流水线

比如我们这里的流水线流程为先运行 test 任务,如果通过了再执行后面的 build-and-push 这个任务,那么我们可以创建一个名为 test-pipeline.yaml 的资源对象,内容如下所示:

  1. #test-pipeline.yaml
  2. apiVersion:tekton.dev/v1beta1
  3. kind:Pipeline
  4. metadata:
  5. name:test-pipeline
  6. spec:
  7. resources:#为Tasks提供输入和输出资源声明
  8. name:demo-git
  9. type:git
  10. name:harbor-image
  11. type:image
  12. tasks:#添加task到流水线中
  13. #运行应用测试
  14. name:test
  15. taskRef:
  16. name:test
  17. resources:
  18. inputs:
  19. name:repo#Task输入名称
  20. resource:demo-git#Pipeline资源名称
  21. #构建并推送Docker镜像
  22. name:build-and-push
  23. taskRef:
  24. name:build-and-push
  25. runAfter:
  26. -test#测试任务执行之后
  27. resources:
  28. inputs:
  29. name:repo#指定输入的git仓库资源
  30. resource:demo-git
  31. outputs:#指定输出的镜像资源
  32. name:builtImage
  33. resource:harbor-image

首先我们需要定义流水线需要哪些资源,可以是输入或者输出的资源,在这里我们只有一个输入,那就是命名为 repo 的应用程序源码的 GitHub 仓库。接下来定义任务,每个任务都通过 taskRef 进行引用,并传递任务需要的输入参数。

同样直接创建这个资源对象即可:

  1. $kubectlapply-ftest-pipeline.yaml
  2. pipeline.tekton.dev/test-pipelinecreated

前面我们提到过和通过创建 TaskRun 去触发 Task 任务类似,我们可以通过创建一个 PipelineRun 对象来运行流水线。这里我们创建一个名为 test-pipelinerun.yaml 的 PipelineRun 对象来运行流水线,文件内容如下所示:

  1. apiVersion:tekton.dev/v1beta1
  2. kind:PipelineRun
  3. metadata:
  4. name:test-pipelinerun
  5. spec:
  6. serviceAccountName:build-sa
  7. pipelineRef:
  8. name:test-pipeline
  9. resources:
  10. name:demo-git#指定输入的git仓库资源
  11. resourceRef:
  12. name:demo-git
  13. name:harbor-image#指定输出的镜像资源
  14. resourceRef:
  15. name:harbor-image

定义方式和 TaskRun 几乎一样,通过 serviceAccountName 属性指定 ServiceAccount 对象,pipelineRef 关联流水线对象。同样直接创建这个资源,创建后就会触发我们的流水线任务了:

  1. $kubectlapply-ftest-pipelinerun.yaml
  2. pipelinerun.tekton.dev/test-pipelineruncreated
  3. $githubkubectlgetpods|greptest-pipelinerun
  4. test-pipelinerun-build-and-push-62g65-pod-6jqqf0/4Init:1/203s
  5. test-pipelinerun-test-c4r9m-pod-j7jjd0/2Completed012s
  6. $tknpipelinerundescribetest-pipelinerun
  7. Name:test-pipelinerun
  8. Namespace:default
  9. PipelineRef:test-pipeline
  10. ServiceAccount:build-sa
  11. Timeout:1h0m0s
  12. Labels:
  13. tekton.dev/pipeline=test-pipeline
  14. Status
  15. STARTEDDURATIONSTATUS
  16. 47secondsago22secondsSucceeded
  17. Resources
  18. NAMERESOURCEREF
  19. ∙demo-gitdemo-git
  20. ∙harbor-imageharbor-image
  21. Params
  22. Noparams
  23. Results
  24. Noresults
  25. Workspaces
  26. Noworkspaces
  27. Taskruns
  28. NAMETASKNAMESTARTEDDURATIONSTATUS
  29. ∙test-pipelinerun-build-and-push-62g65build-and-push38secondsago13secondsSucceeded
  30. ∙test-pipelinerun-test-c4r9mtest46secondsago8secondsSucceeded

到这里证明我们的流水线执行成功了。我们将 Tekton 安装在 Kubernetes 集群上,定义了一个 Task,并通过 YAML 清单和 Tekton CLI 创建 TaskRun 对其进行了测试。我们创建了由两个任务组成的 Tektok 流水线,第一个任务是从 GitHub 克隆代码并运行应用程序测试,第二个任务是构建一个 Docker 镜像并将其推送到 Docker Hub 上。

使用 Results 传递数据

上面我们在构建镜像的时候可以看到镜像的 TAG 我们是写死的,或者需要在每次执行的时候通过参数传递进去,比较麻烦,那么有没有什么办法可以自动生成镜像 TAG 呢?比如根据时间戳来生成一个构建的ID。

这里我们可以通过定义一个 Task 任务,然后通过 script 脚本去获取到数据后传入到 results 中去,我们可以把这些 results 数据传递到流水线中的其他任务中去,比如我们想要获取 git commit 的 SHA 值,或者生成一个随机的 ID 来作为镜像 TAG,比如这里我们创建一个名为 generate-build-id 的 Task 任务,定义了 get-timestamp 和 get-buildid 两个 Steps,一个用于生成时间戳,一个用于生成一个包含基本版本的结果值,将结果添加到 results 中去。

  1. #generate-build-id.yaml
  2. apiVersion:tekton.dev/v1beta1
  3. kind:Task
  4. metadata:
  5. name:generate-build-id
  6. spec:
  7. description:>-
  8. Givenabaseversion,thistaskgeneratesauniquebuildidbyappending
  9. thebase-versiontothecurrenttimestamp.
  10. params:
  11. name:base-version
  12. description:Baseproductversion
  13. type:string
  14. default:"1.0"
  15. results:
  16. name:timestamp
  17. description:Currenttimestamp
  18. name:build-id
  19. description:IDofthecurrentbuild
  20. steps:
  21. name:get-timestamp
  22. image:bash:5.0.18
  23. script:|
  24. #!/usr/bin/envbash
  25. ts=`date"+%Y%m%d-%H%M%S"`
  26. echo"CurrentTimestamp:${ts}"
  27. echo${ts}|tr-d"\\n"|tee$(results.timestamp.path)
  28. name:get-buildid
  29. image:bash:5.0.18
  30. script:|
  31. #!/usr/bin/envbash
  32. ts=`cat$(results.timestamp.path)`
  33. buildId=$(inputs.params.base-version)-${ts}
  34. echo${buildId}|tr-d"\\n"|tee$(results.build-id.path)

直接创建上面的 Task:

  1. kubectlapply-fgenerate-build-id.yaml

创建完成后,现在我们就可以在 Pipeline 中来使用这个 Task 了,用来生成构建 ID,修改 test-pipeline.yaml,增加 generate-build-id 任务:

  1. #test-pipeline.yaml
  2. apiVersion:tekton.dev/v1beta1
  3. kind:Pipeline
  4. metadata:
  5. name:test-pipeline
  6. spec:
  7. resources:#为Tasks提供输入和输出资源声明
  8. name:demo-git
  9. type:git
  10. name:harbor-image
  11. type:image
  12. params:
  13. name:image-tag
  14. type:string
  15. tasks:#添加task到流水线中
  16. #运行应用测试
  17. name:test
  18. taskRef:
  19. name:test
  20. resources:
  21. inputs:
  22. name:repo#Task输入名称
  23. resource:demo-git#Pipeline资源名称
  24. name:get-build-id
  25. taskRef:
  26. name:generate-build-id
  27. params:
  28. name:base-version
  29. value:$(params.image-tag)
  30. #构建并推送Docker镜像
  31. name:build-and-push
  32. taskRef:
  33. name:build-and-push
  34. runAfter:
  35. -test#测试任务执行之后
  36. resources:
  37. inputs:
  38. name:repo#指定输入的git仓库资源
  39. resource:demo-git
  40. outputs:#指定输出的镜像资源
  41. name:builtImage
  42. resource:harbor-image
  43. params:
  44. name:imageTag
  45. value:"$(tasks.get-build-id.results.build-id)"

然后在 build-and-push 任务中通过 "$(tasks.get-build-id.results.build-id)" 获取构建的 ID,将这个 ID 作为参数传入任务中去,所以我们也需要在 build-and-push 任务中增加 build-id 这个参数:

  1. #task-build-push.yaml
  2. apiVersion:tekton.dev/v1beta1
  3. kind:Task
  4. metadata:
  5. name:build-and-push
  6. spec:
  7. resources:
  8. inputs:#定义输入资源
  9. name:repo#输入资源,就是github的那个仓库
  10. type:git
  11. outputs:#定义输出资源
  12. name:builtImage#输出镜像名字
  13. type:image
  14. params:
  15. name:pathToDockerfile#指明dockerfile在仓库中的哪个位置
  16. type:string
  17. default:$(resources.inputs.repo.path)/Dockerfile#repo资源的路径
  18. description:Thepathtothedockerfiletobuild
  19. name:pathToContext#指明dockerfile在仓库中的哪个位置
  20. type:string
  21. default:$(resources.inputs.repo.path)#repo资源的路径
  22. description:thebuildcontextusedbydockerdaemon
  23. name:imageTag
  24. type:string
  25. default:"v0.2.0"
  26. description:thedockerimagetag
  27. steps:
  28. name:build-and-push
  29. image:docker:stable
  30. script:|
  31. #!/usr/bin/envsh
  32. dockerloginharbor.k8s.local
  33. dockerbuild-t$(resources.outputs.builtImage.url):$(params.imageTag)-f$(params.pathToDockerfile)$(params.pathToContext)
  34. dockerpush$(resources.outputs.builtImage.url):$(params.imageTag)#这边的参数都是在input和output中定义的
  35. volumeMounts:
  36. name:dockersock#将docker.sock文件挂载进来,使用宿主机dockerdaemon构建镜像
  37. mountPath:/var/run/docker.sock
  38. volumes:
  39. name:dockersock
  40. hostPath:
  41. path:/var/run/docker.sock

然后需要将 builtImage 这个 output 资源的 url 定义中将镜像 tag 去掉,在 PipelineRun 对象中新增 image-tag 的参数:

  1. #test-pipelinerun.yaml
  2. apiVersion:tekton.dev/v1beta1
  3. kind:PipelineRun
  4. metadata:
  5. name:test-pipelinerun
  6. spec:
  7. serviceAccountName:build-sa
  8. pipelineRef:
  9. name:test-pipeline
  10. resources:
  11. name:demo-git#指定输入的git仓库资源
  12. resourceRef:
  13. name:demo-git
  14. name:harbor-image#指定输出的镜像资源
  15. resourceRef:
  16. name:harbor-image
  17. params:
  18. name:image-tag
  19. value:"v0.3.0"

所有修改完成后,重新执行我们的整个流水线即可。

  1. $tknpipelinerunlogstest-pipelinerun
  2. [test:git-source-repo-g68nd]{"level":"info","ts":1623934515.6170688,"caller":"git/git.go:169","msg":"Successfullyclonedhttps://github.com.cnpmjs.org/cnych/tekton-demo@5e1e3a1d0f167b9b639df5b802a0f0f81064d21e(grafted,HEAD,origin/master)inpath/workspace/repo"}
  3. [test:git-source-repo-g68nd]{"level":"info","ts":1623934515.6349964,"caller":"git/git.go:207","msg":"Successfullyinitializedandupdatedsubmodulesinpath/workspace/repo"}
  4. [test:run-test]PASS
  5. [test:run-test]ok_/workspace/repo0.002s
  6. [get-build-id:get-timestamp]CurrentTimestamp:20210617-125634
  7. [get-build-id:get-timestamp]20210617-125634
  8. [get-build-id:get-buildid]v0.3.0-20210617-125634
  9. [build-and-push:git-source-repo-v2lhk]{"level":"info","ts":1623934601.68953,"caller":"git/git.go:169","msg":"Successfullyclonedhttps://github.com.cnpmjs.org/cnych/tekton-demo@5e1e3a1d0f167b9b639df5b802a0f0f81064d21e(grafted,HEAD,origin/master)inpath/workspace/repo"}
  10. [build-and-push:git-source-repo-v2lhk]{"level":"info","ts":1623934601.7080255,"caller":"git/git.go:207","msg":"Successfullyinitializedandupdatedsubmodulesinpath/workspace/repo"}
  11. [build-and-push:build-and-push]Authenticatingwithexistingcredentials…
  12. [build-and-push:build-and-push]LoginSucceeded
  13. [build-and-push:build-and-push]WARNING!Yourpasswordwillbestoredunencryptedin/root/.docker/config.json.
  14. [build-and-push:build-and-push]Configureacredentialhelpertoremovethiswarning.See
  15. [build-and-push:build-and-push]https://docs.docker.com/engine/reference/commandline/login/#credentials-store
  16. [build-and-push:build-and-push]
  17. [build-and-push:build-and-push]SendingbuildcontexttoDockerdaemon154.1kB
  18. [build-and-push:build-and-push]Step1/6:FROMgolang:1.14-alpine
  19. ……
  20. [build-and-push:build-and-push]Successfullybuilt2358e77bbe0e
  21. [build-and-push:build-and-push]Successfullytaggedharbor.k8s.local/course/tekton-demo:v0.3.0-20210617-125634
  22. [build-and-push:build-and-push]Thepushreferstorepository[harbor.k8s.local/course/tekton-demo]
  23. [build-and-push:build-and-push]f9a271a3fb3c:Preparing
  24. ……
  25. [build-and-push:build-and-push]26ec43d351f2:Pushed
  26. [build-and-push:build-and-push]v0.3.0-20210617-125634:digest:sha256:68be388e3f85dd10a6689a986eb2f7f7f5a5c89bb03f40c3db3178e0ce242752size:2198
  27. [build-and-push:image-digest-exporter-t54fb]{"severity":"INFO","timestamp":"2021-06-17T12:56:46.54052284Z","caller":"logging/config.go:116","message":"Successfullycreatedthelogger."}
  28. [build-and-push:image-digest-exporter-t54fb]{"severity":"INFO","timestamp":"2021-06-17T12:56:46.541010181Z","caller":"logging/config.go:117","message":"Logginglevelsetto:info"}
  29. [build-and-push:image-digest-exporter-t54fb]{"severity":"INFO","timestamp":"2021-06-17T12:56:46.541254959Z","caller":"imagedigestexporter/main.go:59","message":"Noindex.jsonfoundfor:builtImage","commit":"7ca5d61"}

我们可以看到在 get-build-id 任务中为我们生成了 v0.3.0-20210617-125634 这样的镜像 TAG,最后也通过 results 传递到了下面的构建任务中去,镜像的 TAG 也更新了。

一篇带你创建 Tekton 流水线

Tekton Catalog

当然这些任务其实都具有一定的通用性的,为此 Tekton 官方提供了一个 Catalog 的服务,用来专门提供一些通用的任务,比如我们想要获取 Git Commit 的相关信息,可以使用 https://artifacthub.io/packages/tekton-task/tekton-catalog-tasks/git-clone 这个 Catalog,文档中也包含相关的使用说明。

到这里我们就完成了使用 Tekton 创建 CI/CD 流水线的一个简单示例,不过这个示例还比较简单,接下来我们再通过一个稍微复杂点的应用来完成我们前面的 Jenkins 流水线。

原文链接:https://mp.weixin.qq.com/s/M08CrTEeriZwvNCjlaZdkg

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 行业资讯 一篇带你创建 Tekton 流水线 https://www.kuaiidc.com/64770.html

相关文章

发表评论
暂无评论