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

news2024/9/20 18:48:10

场景介绍

我们在上一篇文章中构建了一个最简单的ci,接下来我们对我们的github的项目构建一个较标准的ci。

Tekton简介,安装和构建最简单ci/cd-CSDN博客文章浏览阅读239次,点赞2次,收藏2次。本文介绍了tekton是什么,如何安装,以及实践了下task和task runhttps://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

# task-clone.yaml
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: git-clone
  labels:
    app.kubernetes.io/version: "0.8"
spec:
  workspaces:
    - name: output
      description: The git repo will be cloned into the dir
  params:
    - name: url
      description: Repository URL to clone from.
      type: string
    - name: revision
      description: which commit you would like to get, master or others
      type: string
    - name: base_image
      description: base_image for git & unit testing
      type: string
  steps:
    - name: clone
      image: "$(params.base_image)"
      env:
      - name: WORKSPACE_OUTPUT_PATH
        value: $(workspaces.output.path)
      - name: GIT_REPO_URL
        value: $(params.url)
      - name: GIT_REPO_REVISION
        value: $(params.revision)
      script: |
        #!/usr/bin/env sh
        set -eu
        whoami
        pwd
        cd ${WORKSPACE_OUTPUT_PATH}
        pwd
        rm -rf *
        git clone ${GIT_REPO_URL}
        repo_dir=$(echo $GIT_REPO_URL | rev  | cut -d '/' -f 1 | rev | sed 's/.git//g')
        cd ${repo_dir}
        pwd
        git checkout ${GIT_REPO_REVISION}
        ls -al

pipeline & pipelinerun for git-clone

# pipeline.yaml
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: pipeline
spec:
  workspaces:
    - name: git-repo-pvc
  params:
    - name: git_url
      type: string
    - name: revision
      type: string
      default: main
    - name: git_base_image
      type: string
  tasks:
    - name: clone
      taskRef:
        name: git-clone
      workspaces:
        - name: output
          workspace: git-repo-pvc
      params:
        - name: url
          value: $(params.git_url)
        - name: revision
          value: $(params.revision)
        - name: base_image
          value: $(params.git_base_image)
# pipelinerun.yaml
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
  name: pipelinerun
spec:
  pipelineRef:
    name: pipeline
  workspaces:
    - name: git-repo-pvc
      persistentVolumeClaim:
        claimName: git-repo-pvc
  params:
    - name: git_url
      value: https://github.com/testcara/tekton_triggers_learning.git
    - name: git_base_image
      value: docker.io/cara/cara-pipeline-base:V1

现在让我们测试执行

kubectl apply -f task-clone.yaml
kubectl pipeline.yaml
kubectl pipelinerun.yaml

查看结果

kubectl get pods
# 当查看到pipelinerun-clone-pod status 为completed时
# 查看pipelinerun logs
tkn pipelinerun logs pipelinerun

可以看到我的输出表明其是正常完成的

carawang@ci %tkn pipelinerun logs pipelinerun
[clone : clone] root
[clone : clone] /
[clone : clone] /workspace/output
[clone : clone] Cloning into 'tekton_triggers_learning'...
[clone : clone] /workspace/output/tekton_triggers_learning
[clone : clone] Already on 'main'
[clone : clone] total 28
[clone : clone] drwxr-xr-x 4 root root 4096 Sep  6 02:40 .
[clone : clone] drwxrwxrwx 3 root root 4096 Sep  6 02:40 ..
[clone : clone] drwxr-xr-x 8 root root 4096 Sep  6 02:40 .git
[clone : clone] -rw-r--r-- 1 root root   67 Sep  6 02:40 Dockerfile
[clone : clone] -rw-r--r-- 1 root root   77 Sep  6 02:40 README.md
[clone : clone] -rw-r--r-- 1 root root  496 Sep  6 02:40 index.html
[clone : clone] drwxr-xr-x 2 root root 4096 Sep  6 02:40 nginx

task test

我们就简单的fake个test

# task-test.yaml
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: test
spec:
  workspaces:
    - name: output
      description: The git repo will be cloned into the dir
  params:
    - name: base_image
      description: base_image for git & unit testing
      type: string
  steps:
    - name: test
      image: "$(params.base_image)"
      env:
      - name: WORKSPACE_OUTPUT_PATH
        value: $(workspaces.output.path)
      script: |
        #!/usr/bin/env sh
        set -eu
        whoami
        pwd
        cd ${WORKSPACE_OUTPUT_PATH}
        pwd
        ls -al
        if [ -e tekton_triggers_learning/Dockerfile ]
        then
          echo 'fake test passed'
        else
          exit 1
        fi

pipeline & pipelinerun for test

我们仅列出新编写的代码

# pipeline.yaml
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: pipeline
spec:
  params:
    - name: test_base_image
      type: string
  tasks:
    - name: test
      taskRef:
        name: test
      runAfter:
        - clone
      workspaces:
        - name: output
          workspace: git-repo-pvc
      params:
        - name: base_image
          value: $(params.test_base_image)
# pipelinerun.yaml
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
  name: pipelinerun
spec:
  params:
    - name: test_base_image
      value: centos:7

我们安装这些yaml,然后观察pod,查看pipelinerun logs.

通过我的log, 可以看到pipelinerun顺利完成。

carawang@tekton_trigger_learning %tkn pipelinerun logs pipelinerun
[clone : clone] root
[clone : clone] /
[clone : clone] /workspace/output
[clone : clone] Cloning into 'tekton_triggers_learning'...
[clone : clone] /workspace/output/tekton_triggers_learning
[clone : clone] Already on 'main'
[clone : clone] total 28
[clone : clone] drwxr-xr-x 4 root root 4096 Sep  6 04:10 .
[clone : clone] drwxrwxrwx 3 root root 4096 Sep  6 04:10 ..
[clone : clone] drwxr-xr-x 8 root root 4096 Sep  6 04:10 .git
[clone : clone] -rw-r--r-- 1 root root   67 Sep  6 04:10 Dockerfile
[clone : clone] -rw-r--r-- 1 root root   77 Sep  6 04:10 README.md
[clone : clone] -rw-r--r-- 1 root root  496 Sep  6 04:10 index.html
[clone : clone] drwxr-xr-x 2 root root 4096 Sep  6 04:10 nginx

[test : test] root
[test : test] /
[test : test] /workspace/output
[test : test] total 12
[test : test] drwxrwxrwx 3 root root 4096 Sep  6 04:10 .
[test : test] drwxrwxrwx 3 root root 4096 Sep  6 04:10 ..
[test : test] drwxr-xr-x 4 root root 4096 Sep  6 04:10 tekton_triggers_learning
[test : test] fake test passed

task build

我们仅列出新编写的代码

# task-build.yaml
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: build
  labels:
    app.kubernetes.io/version: "0.8"
spec:
  workspaces:
    - name: output
  params:
    - name: base_image
      description: base_image for docker build
      type: string
  steps:
    - name: build
      image: "$(params.base_image)"
      volumeMounts:
        - name: docker-socket
          mountPath: /var/run/docker.sock
      env:
      - name: WORKSPACE_OUTPUT_PATH
        value: $(workspaces.output.path)
      script: |
        #!/usr/bin/env sh
        set -eu
        whoami
        pwd
        cd ${WORKSPACE_OUTPUT_PATH}
        cd tekton_triggers_learning
        pwd
        docker version
        docker build . -t cara/cara-hello-nginx:latest
  volumes:
    - name: docker-socket
      hostPath:
        path: /var/run/docker.sock
        type: Socket

pipeline & pipelinerun for build

我们仅列出新编写的代码

# pipeline.yaml
spec:
  params:
    - name: docker_build_base_image
      type: string
  tasks:
    - name: build
      taskRef:
        name: build
      runAfter:
        - test
      workspaces:
        - name: output
          workspace: git-repo-pvc
      params:
        - name: base_image
          value: $(params.docker_build_base_image)
# pipelinerun.yaml
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
  name: pipelinerun
spec:
  params:
    - name: docker_build_base_image
      value: docker.io/cara/my-dind-docker:latest

我们安装这些yaml,然后观察pod,查看pipelinerun logs.

通过我的pod status, 可以看到pipelinerun顺利完成。logs太长,这里不再列出。

carawang@ci %kubectl get pods
NAME                                        READY   STATUS      RESTARTS      AGE
hello-nginx-b786f45d4-7lndt                 1/1     Running     3 (50m ago)   20h
hello-nginx-b786f45d4-cpj2g                 1/1     Running     3 (50m ago)   20h
hello-nginx-b786f45d4-rv7ch                 1/1     Running     3 (50m ago)   20h
pipelinerun-build-pod                       0/1     Completed   0             74s
pipelinerun-clone-pod                       0/1     Completed   0             90s
pipelinerun-test-pod                        0/1     Completed   0             81s
project-ci-cd-pipeline-run-fake-ci-cd-pod   0/1     Completed   0             45h

task push

为了简便,我们之间登陆,而不用credentials secrect的方式。这种方式仅作为自己测试和学习使用。不能用于生产。

# task-build.yaml
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: push
  labels:
    app.kubernetes.io/version: "0.8"
spec:
  params:
    - name: base_image
      description: base_image for docker build
      type: string
  steps:
    - name: push
      image: "$(params.base_image)"
      volumeMounts:
        - name: docker-socket
          mountPath: /var/run/docker.sock
      env:
      script: |
        #!/usr/bin/env sh
        set -eu
        echo mypassword |  docker login --username myname --password-stdin
        docker push cara/cara-hello-nginx:latest
  volumes:
    - name: docker-socket
      hostPath:
        path: /var/run/docker.sock
        type: Socket

pipeline & pipelinerun for push

我们仅列出新编写的代码

# pipeline.yaml
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: pipeline
spec:
  tasks:
    - name: push
      taskRef:
        name: push
      runAfter:
        - build
      params:
        - name: base_image
          value: $(params.docker_build_base_image)

pipeline run没有任何变化,这里不列出。

我们安装这些yaml,然后观察pod,查看pipelinerun logs.

通过我的pod status, 可以看到pipelinerun顺利完成。logs太长,这里不再列出。

carawang@ci %kubectl get pods
NAME                                        READY   STATUS      RESTARTS       AGE
hello-nginx-b786f45d4-7lndt                 1/1     Running     3 (120m ago)   21h
hello-nginx-b786f45d4-cpj2g                 1/1     Running     3 (120m ago)   21h
hello-nginx-b786f45d4-rv7ch                 1/1     Running     3 (120m ago)   21h
pipelinerun-build-pod                       0/1     Completed   0              7m32s
pipelinerun-clone-pod                       0/1     Completed   0              7m47s
pipelinerun-push-pod                        0/1     Completed   0              7m25s
pipelinerun-test-pod                        0/1     Completed   0              7m38s
project-ci-cd-pipeline-run-fake-ci-cd-pod   0/1     Completed   0              46h

结语

这里我们的tekton ci已经构建完成。我们将在接下文的文章中,介绍用tekton进行部署和用argocd进行部署。这两种部署都是比较流行的cd的形式。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2111897.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

vsftpd配置用户和密码让其他客户端连接

一、第一个主机:vsftpd下载及配置 前置准备: #卸载防火墙 yum -y remove firewalld #为了不让防火墙有影响,iptables配置也清空 iptables -F vim /etc/selinux/conf SELINUXdisabled #主要是把它改为disabled或者permissive SELINUXTYPEtargeted #重启linux让seli…

[Web安全 网络安全]-安全法规 网络基础 信息收集

文章目录: 一:网络安全法规 二:计算机网络 1.计算机网络的组成 2.网络分层模型(OSI七层 TCP/IP四层) 3.通信协议 IP协议 UDP协议/TCP协议 TCP协议 UDP协议 区别 HTTP协议/HTTPS协议 HTTP协议 HTTPS协议…

RedissonClient 分布式队列工具类

注意:轻量级队列可以使用工具类,重量级数据量 请使用 MQ 本文章基于redis使用redisson客户端实现轻量级队列,以及代码、执行结果演示 一、常见队列了解 普通队列:先进先出(FIFO),只能在一端添…

【网络安全 | 甲方建设】SaaS平台、Jira工具及Jenkins服务器

原创文章,不得转载。 文章目录 SaaS平台友好性Jira友好性Jenkins友好性SaaS平台 SaaS,全称为 “Software as a Service”(软件即服务),是一种基于云计算的软件交付模型。在这种模型中,软件不需要用户在本地安装和维护,而是通过互联网访问和使用。软件通常由服务提供商托…

RLC(电阻、电感、电容)

RLC(电阻、电感、电容) 目录一、两个电阻(R1,R2),电容(C1,C2)的串联/并联公式?二、请画出这个1ms, 1V的Vin脉冲信号在Vout端的大致图像1.电路图2.…

中秋猜灯谜_猜字谜小程序源码,无需服务器

这款小程序搭建是免服务器和域名的,serverless,没有后端; 无需设置合法域名的!上传就可以使用; 只需要使用微信开发者工具打开源码然后上传审核就可以了! 这款小程序其实比较简洁,分两种模式青铜模式(普通)和王者模式(困难),猜…

关于蓝屏查看日志分析原因

一、前提 虽然电脑经常蓝屏,或者发生了蓝屏现象,但是仍然可以进入系统,并且可以进行桌面操作。 二、查看蓝屏日志 1.按下win键,搜索计算机管理。 2.依次点击:系统工具->事件查看器->Windows日志->系统 3.在…

CosyVoice:开源强大的 AI 语音合成工具

在当今科技飞速发展的时代,AI 语音合成技术正逐渐改变着我们的生活。今天,就为大家介绍一款卓越的语音合成工具——CosyVoice。 一、安装步骤 克隆和安装: 克隆仓库:git clone --recursive https://github.com/FunAudioLLM/Cos…

linux使用samba共享目录,其他虚拟机和windows都可以访问

一、192.168.137.12主机作为源目录主机,将/samba/shared_dir目录分享出去 #192.168.137.12主机: rpm -q samba #查看是否安装 yum -y install samba #创建共享目录 mkdir /samba/shared_dir -p #给共享目录赋权 chown -R samba.samba /samba #提示用户不…

【流程设计】JAVA系统集成activiti工作流,流程设计器,在线审批,会签,驳回,流程图查看(实际多套系统运用案例分析)

基于Javavue开发的智能审批系统,低代码平台方案 其他资料,软件资料清单列表部分文档清单:工作安排任务书,可行性分析报告,立项申请审批表,产品需求规格说明书,需求调研计划,用户需求…

图论篇--代码随想录算法训练营第五十二天打卡| 101. 孤岛的总面积,102. 沉没孤岛,103. 水流问题,104.建造最大岛屿

101. 孤岛的总面积 题目链接:101. 孤岛的总面积 题目描述: 给定一个由 1(陆地)和 0(水)组成的矩阵,岛屿指的是由水平或垂直方向上相邻的陆地单元格组成的区域,且完全被水域单元格…

攻防世界 unseping

unseping 攻防世界web新手练习 -unseping_攻防世界web新手题unseping-CSDN博客 这道题对我来说还是有点难&#xff0c;什么oct绕过命令执行第一次遇到捏&#xff0c;所以基本是跟着别人的wp写的&#xff0c;一点点记录吧 先对源码进行分析 <?php highlight_file(__FILE…

LLVM IR指令VM混淆分析

未混淆编译 编写一个最简单的测试代码&#xff0c;对 test_add函数用于对两个数相加&#xff1a; int __attribute((__annotate__("vm"))) test_add(int a, int b) {int c a b;return c; }int main(void) {int c test_add(1, 2);return c; } 编译成中间代码&am…

【佳学基因检测】如何知道一个网站是用Nginx还是Apache运行的服务器。

【佳学基因检测】如何知道一个网站是用Nginx还是Apache运行的服务器。 要确定一个的网站是由Nginx还是Apache服务器运行&#xff0c;可以使用以下几种方法&#xff1a; 1. 查看HTTP头信息 您可以通过检查网站返回的HTTP头信息来判断使用的是哪种服务器。具体步骤如下&#x…

Kafka【十三】消费者消费消息的偏移量

偏移量offset是消费者消费数据的一个非常重要的属性。默认情况下&#xff0c;消费者如果不指定消费主题数据的偏移量&#xff0c;那么消费者启动消费时&#xff0c;无论当前主题之前存储了多少历史数据&#xff0c;消费者只能从连接成功后当前主题最新的数据偏移位置读取&#…

FastAPI+Vue3零基础开发ERP系统项目实战课 20240906 上课笔记 fastapi的各种练习

回顾练习 用FastAPI写一个接口&#xff0c;这个接口能够返回九九乘法表的字符串。 获取九九乘法表&#xff1a; for i in range(1, 10):for j in range(1, i 1):print(f"{j} x {i} {j * i}", end"\t")print()# 得到字符串 talbe99 "" for …

多环境jdk安装,CentOS,统信UOS,Ubuntu,KylinOS,windows

文章目录 1.CentOS1.1yum安装1.2压缩包安装 本文档只是为了留档方便以后工作运维&#xff0c;或者给同事分享文档内容比较简陋命令也不是特别全&#xff0c;不适合小白观看&#xff0c;如有不懂可以私信&#xff0c;上班期间都是在得 1.CentOS 1.1yum安装 yum install -y jav…

Oracle VM VirtualBox 下 Ubuntu22 虚拟机配置双网络

初衷&#xff0c;希望在虚拟机里面配置两个网络。一个网络用来给虚拟机上互联网&#xff08;浏览器&#xff0c;邮箱等&#xff09;使用&#xff0c;一个网络用于虚拟机和宿主机通讯&#xff08;静态IP&#xff09; 1 VirtualBox 网络设置 2 宿主机网络配置 3 虚拟机内命令行配…

前端---对MVC MVP MVVM的理解

就需要从前端这些年的从无到有、从有到优的变迁过程讲一下。 1. Web1.0时代 在web1.0时代并没有前端的概念&#xff0c;开发一个web应用多数采用ASP.NET/Java/PHP编写&#xff0c;项目通常用多个aspx/jsp/php文件构成&#xff0c;每个文件中同时包含了HTML、CSS、JavaScript、…

SpringCloud之熔断监控HystrixDashboard和Turbine

SpringCloud之熔断监控HystrixDashboard和Turbine Hystrix-dashboard是一款针对Hystrix进行实时监控的工具&#xff0c;通过Hystrix Dashboard我们可以在直观地看到各 Hystrix Command的请求响应时间, 请求成功率等数据。但是只使用Hystrix Dashboard的话, 你只能看到单个应 …