tekton构建标准ci(clone repo, test, build & push img)

打印 上一主题 下一主题

主题 1877|帖子 1877|积分 5631

场景介绍

我们在上一篇文章中构建了一个最简单的ci,接下来我们对我们的github的项目构建一个较标准的ci。
Tekton简介,安装和构建最简单ci/cd-CSDN博客文章欣赏阅读239次,点赞2次,收藏2次。本文介绍了tekton是什么,如何安装,以及实践了下task和task run
https://blog.csdn.net/solinger/article/details/141898338?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22141898338%22%2C%22source%22%3A%22solinger%22%7D
ci是一连集成,我们只必要考虑摆设前的事变:
   pipeline:
  - clone repo
  - run test
  - build image
  - push image
  有了这个思绪,我们就把它们实现。 我们按照最简单ci/cd的步调先写task, taskrun,然后写pipeline, pipelinerun。
构建ci

task git-clone

  1. # task-clone.yaml
  2. apiVersion: tekton.dev/v1beta1
  3. kind: Task
  4. metadata:
  5.   name: git-clone
  6.   labels:
  7.     app.kubernetes.io/version: "0.8"
  8. spec:
  9.   workspaces:
  10.     - name: output
  11.       description: The git repo will be cloned into the dir
  12.   params:
  13.     - name: url
  14.       description: Repository URL to clone from.
  15.       type: string
  16.     - name: revision
  17.       description: which commit you would like to get, master or others
  18.       type: string
  19.     - name: base_image
  20.       description: base_image for git & unit testing
  21.       type: string
  22.   steps:
  23.     - name: clone
  24.       image: "$(params.base_image)"
  25.       env:
  26.       - name: WORKSPACE_OUTPUT_PATH
  27.         value: $(workspaces.output.path)
  28.       - name: GIT_REPO_URL
  29.         value: $(params.url)
  30.       - name: GIT_REPO_REVISION
  31.         value: $(params.revision)
  32.       script: |
  33.         #!/usr/bin/env sh
  34.         set -eu
  35.         whoami
  36.         pwd
  37.         cd ${WORKSPACE_OUTPUT_PATH}
  38.         pwd
  39.         rm -rf *
  40.         git clone ${GIT_REPO_URL}
  41.         repo_dir=$(echo $GIT_REPO_URL | rev  | cut -d '/' -f 1 | rev | sed 's/.git//g')
  42.         cd ${repo_dir}
  43.         pwd
  44.         git checkout ${GIT_REPO_REVISION}
  45.         ls -al
复制代码
pipeline & pipelinerun for git-clone

  1. # pipeline.yaml
  2. apiVersion: tekton.dev/v1beta1
  3. kind: Pipeline
  4. metadata:
  5.   name: pipeline
  6. spec:
  7.   workspaces:
  8.     - name: git-repo-pvc
  9.   params:
  10.     - name: git_url
  11.       type: string
  12.     - name: revision
  13.       type: string
  14.       default: main
  15.     - name: git_base_image
  16.       type: string
  17.   tasks:
  18.     - name: clone
  19.       taskRef:
  20.         name: git-clone
  21.       workspaces:
  22.         - name: output
  23.           workspace: git-repo-pvc
  24.       params:
  25.         - name: url
  26.           value: $(params.git_url)
  27.         - name: revision
  28.           value: $(params.revision)
  29.         - name: base_image
  30.           value: $(params.git_base_image)
复制代码
  1. # pipelinerun.yaml
  2. apiVersion: tekton.dev/v1beta1
  3. kind: PipelineRun
  4. metadata:
  5.   name: pipelinerun
  6. spec:
  7.   pipelineRef:
  8.     name: pipeline
  9.   workspaces:
  10.     - name: git-repo-pvc
  11.       persistentVolumeClaim:
  12.         claimName: git-repo-pvc
  13.   params:
  14.     - name: git_url
  15.       value: https://github.com/testcara/tekton_triggers_learning.git
  16.     - name: git_base_image
  17.       value: docker.io/cara/cara-pipeline-base:V1
复制代码
现在让我们测试执行
  1. kubectl apply -f task-clone.yaml
  2. kubectl pipeline.yaml
  3. kubectl pipelinerun.yaml
复制代码
查看结果
  1. kubectl get pods
  2. # 当查看到pipelinerun-clone-pod status 为completed时
  3. # 查看pipelinerun logs
  4. tkn pipelinerun logs pipelinerun
复制代码
可以看到我的输出表明其是正常完成的
  1. carawang@ci %tkn pipelinerun logs pipelinerun
  2. [clone : clone] root
  3. [clone : clone] /
  4. [clone : clone] /workspace/output
  5. [clone : clone] Cloning into 'tekton_triggers_learning'...
  6. [clone : clone] /workspace/output/tekton_triggers_learning
  7. [clone : clone] Already on 'main'
  8. [clone : clone] total 28
  9. [clone : clone] drwxr-xr-x 4 root root 4096 Sep  6 02:40 .
  10. [clone : clone] drwxrwxrwx 3 root root 4096 Sep  6 02:40 ..
  11. [clone : clone] drwxr-xr-x 8 root root 4096 Sep  6 02:40 .git
  12. [clone : clone] -rw-r--r-- 1 root root   67 Sep  6 02:40 Dockerfile
  13. [clone : clone] -rw-r--r-- 1 root root   77 Sep  6 02:40 README.md
  14. [clone : clone] -rw-r--r-- 1 root root  496 Sep  6 02:40 index.html
  15. [clone : clone] drwxr-xr-x 2 root root 4096 Sep  6 02:40 nginx
复制代码
task test

我们就简单的fake个test
  1. # task-test.yaml
  2. apiVersion: tekton.dev/v1beta1
  3. kind: Task
  4. metadata:
  5.   name: test
  6. spec:
  7.   workspaces:
  8.     - name: output
  9.       description: The git repo will be cloned into the dir
  10.   params:
  11.     - name: base_image
  12.       description: base_image for git & unit testing
  13.       type: string
  14.   steps:
  15.     - name: test
  16.       image: "$(params.base_image)"
  17.       env:
  18.       - name: WORKSPACE_OUTPUT_PATH
  19.         value: $(workspaces.output.path)
  20.       script: |
  21.         #!/usr/bin/env sh
  22.         set -eu
  23.         whoami
  24.         pwd
  25.         cd ${WORKSPACE_OUTPUT_PATH}
  26.         pwd
  27.         ls -al
  28.         if [ -e tekton_triggers_learning/Dockerfile ]
  29.         then
  30.           echo 'fake test passed'
  31.         else
  32.           exit 1
  33.         fi
复制代码
pipeline & pipelinerun for test

我们仅列出新编写的代码
  1. # pipeline.yaml
  2. apiVersion: tekton.dev/v1beta1
  3. kind: Pipeline
  4. metadata:
  5.   name: pipeline
  6. spec:
  7.   params:
  8.     - name: test_base_image
  9.       type: string
  10.   tasks:
  11.     - name: test
  12.       taskRef:
  13.         name: test
  14.       runAfter:
  15.         - clone
  16.       workspaces:
  17.         - name: output
  18.           workspace: git-repo-pvc
  19.       params:
  20.         - name: base_image
  21.           value: $(params.test_base_image)
复制代码
  1. # pipelinerun.yaml
  2. apiVersion: tekton.dev/v1beta1
  3. kind: PipelineRun
  4. metadata:
  5.   name: pipelinerun
  6. spec:
  7.   params:
  8.     - name: test_base_image
  9.       value: centos:7
复制代码
我们安装这些yaml,然后观察pod,查看pipelinerun logs.
通过我的log, 可以看到pipelinerun顺利完成。
  1. carawang@tekton_trigger_learning %tkn pipelinerun logs pipelinerun
  2. [clone : clone] root
  3. [clone : clone] /
  4. [clone : clone] /workspace/output
  5. [clone : clone] Cloning into 'tekton_triggers_learning'...
  6. [clone : clone] /workspace/output/tekton_triggers_learning
  7. [clone : clone] Already on 'main'
  8. [clone : clone] total 28
  9. [clone : clone] drwxr-xr-x 4 root root 4096 Sep  6 04:10 .
  10. [clone : clone] drwxrwxrwx 3 root root 4096 Sep  6 04:10 ..
  11. [clone : clone] drwxr-xr-x 8 root root 4096 Sep  6 04:10 .git
  12. [clone : clone] -rw-r--r-- 1 root root   67 Sep  6 04:10 Dockerfile
  13. [clone : clone] -rw-r--r-- 1 root root   77 Sep  6 04:10 README.md
  14. [clone : clone] -rw-r--r-- 1 root root  496 Sep  6 04:10 index.html
  15. [clone : clone] drwxr-xr-x 2 root root 4096 Sep  6 04:10 nginx
  16. [test : test] root
  17. [test : test] /
  18. [test : test] /workspace/output
  19. [test : test] total 12
  20. [test : test] drwxrwxrwx 3 root root 4096 Sep  6 04:10 .
  21. [test : test] drwxrwxrwx 3 root root 4096 Sep  6 04:10 ..
  22. [test : test] drwxr-xr-x 4 root root 4096 Sep  6 04:10 tekton_triggers_learning
  23. [test : test] fake test passed
复制代码
task build

我们仅列出新编写的代码
  1. # task-build.yaml
  2. apiVersion: tekton.dev/v1beta1
  3. kind: Task
  4. metadata:
  5.   name: build
  6.   labels:
  7.     app.kubernetes.io/version: "0.8"
  8. spec:
  9.   workspaces:
  10.     - name: output
  11.   params:
  12.     - name: base_image
  13.       description: base_image for docker build
  14.       type: string
  15.   steps:
  16.     - name: build
  17.       image: "$(params.base_image)"
  18.       volumeMounts:
  19.         - name: docker-socket
  20.           mountPath: /var/run/docker.sock
  21.       env:
  22.       - name: WORKSPACE_OUTPUT_PATH
  23.         value: $(workspaces.output.path)
  24.       script: |
  25.         #!/usr/bin/env sh
  26.         set -eu
  27.         whoami
  28.         pwd
  29.         cd ${WORKSPACE_OUTPUT_PATH}
  30.         cd tekton_triggers_learning
  31.         pwd
  32.         docker version
  33.         docker build . -t cara/cara-hello-nginx:latest
  34.   volumes:
  35.     - name: docker-socket
  36.       hostPath:
  37.         path: /var/run/docker.sock
  38.         type: Socket
复制代码
pipeline & pipelinerun for build

我们仅列出新编写的代码
  1. # pipeline.yaml
  2. spec:
  3.   params:
  4.     - name: docker_build_base_image
  5.       type: string
  6.   tasks:
  7.     - name: build
  8.       taskRef:
  9.         name: build
  10.       runAfter:
  11.         - test
  12.       workspaces:
  13.         - name: output
  14.           workspace: git-repo-pvc
  15.       params:
  16.         - name: base_image
  17.           value: $(params.docker_build_base_image)
复制代码
  1. # pipelinerun.yaml
  2. apiVersion: tekton.dev/v1beta1
  3. kind: PipelineRun
  4. metadata:
  5.   name: pipelinerun
  6. spec:
  7.   params:
  8.     - name: docker_build_base_image
  9.       value: docker.io/cara/my-dind-docker:latest
复制代码
我们安装这些yaml,然后观察pod,查看pipelinerun logs.
通过我的pod status, 可以看到pipelinerun顺利完成。logs太长,这里不再列出。
  1. carawang@ci %kubectl get pods
  2. NAME                                        READY   STATUS      RESTARTS      AGE
  3. hello-nginx-b786f45d4-7lndt                 1/1     Running     3 (50m ago)   20h
  4. hello-nginx-b786f45d4-cpj2g                 1/1     Running     3 (50m ago)   20h
  5. hello-nginx-b786f45d4-rv7ch                 1/1     Running     3 (50m ago)   20h
  6. pipelinerun-build-pod                       0/1     Completed   0             74s
  7. pipelinerun-clone-pod                       0/1     Completed   0             90s
  8. pipelinerun-test-pod                        0/1     Completed   0             81s
  9. project-ci-cd-pipeline-run-fake-ci-cd-pod   0/1     Completed   0             45h
复制代码
task push

为了简便,我们之间登岸,而不用credentials secrect的方式。这种方式仅作为本身测试和学习利用。不能用于生产。
  1. # task-build.yaml
  2. apiVersion: tekton.dev/v1beta1
  3. kind: Task
  4. metadata:
  5.   name: push
  6.   labels:
  7.     app.kubernetes.io/version: "0.8"
  8. spec:
  9.   params:
  10.     - name: base_image
  11.       description: base_image for docker build
  12.       type: string
  13.   steps:
  14.     - name: push
  15.       image: "$(params.base_image)"
  16.       volumeMounts:
  17.         - name: docker-socket
  18.           mountPath: /var/run/docker.sock
  19.       env:
  20.       script: |
  21.         #!/usr/bin/env sh
  22.         set -eu
  23.         echo mypassword |  docker login --username myname --password-stdin
  24.         docker push cara/cara-hello-nginx:latest
  25.   volumes:
  26.     - name: docker-socket
  27.       hostPath:
  28.         path: /var/run/docker.sock
  29.         type: Socket
复制代码
pipeline & pipelinerun for push

我们仅列出新编写的代码
  1. # pipeline.yaml
  2. apiVersion: tekton.dev/v1beta1
  3. kind: Pipeline
  4. metadata:
  5.   name: pipeline
  6. spec:
  7.   tasks:
  8.     - name: push
  9.       taskRef:
  10.         name: push
  11.       runAfter:
  12.         - build
  13.       params:
  14.         - name: base_image
  15.           value: $(params.docker_build_base_image)
复制代码
pipeline run没有任何厘革,这里不列出。
我们安装这些yaml,然后观察pod,查看pipelinerun logs.
通过我的pod status, 可以看到pipelinerun顺利完成。logs太长,这里不再列出。
  1. carawang@ci %kubectl get pods
  2. NAME                                        READY   STATUS      RESTARTS       AGE
  3. hello-nginx-b786f45d4-7lndt                 1/1     Running     3 (120m ago)   21h
  4. hello-nginx-b786f45d4-cpj2g                 1/1     Running     3 (120m ago)   21h
  5. hello-nginx-b786f45d4-rv7ch                 1/1     Running     3 (120m ago)   21h
  6. pipelinerun-build-pod                       0/1     Completed   0              7m32s
  7. pipelinerun-clone-pod                       0/1     Completed   0              7m47s
  8. pipelinerun-push-pod                        0/1     Completed   0              7m25s
  9. pipelinerun-test-pod                        0/1     Completed   0              7m38s
  10. project-ci-cd-pipeline-run-fake-ci-cd-pod   0/1     Completed   0              46h
复制代码
结语

这里我们的tekton ci已经构建完成。我们将在接下文的文章中,介绍用tekton举行摆设和用argocd举行摆设。这两种摆设都是比较流行的cd的情势。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

立山

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表