Trigger +Pipeline 完整实战案例

news2024/10/5 16:27:54

2.4.1 案例环境说明

  • 示例项目:http://code.icloud2native.com/root/spring-boot-helloWorld.git

  • 触发机制:

    1. 用户推送代码至项目仓库
    2. 由Push Hook 自东触发pipeline的流水线的执行

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6jsyOFls-1684738152154)(images\image-20230113164026230.png)]

2.4.2 项目实现

1、在k8s上部署一个gitlab,前面上节已经完成。

2、运行的任何一个eventlistener的webhook不允许匿名推事件,所以得生成gitlab webhook token的secret: 01-gitlab-token.yaml:

apiVersion: v1
kind: Secret
metadata:
  name: gitlab-webhook-token
type: Opaque
stringData:
  # Generated by command "openssl rand -base64 12"
  webhookToken: "8/MDKoGoabPzFeZr"

3、eventlistener运行为pod的时候,要读取trgger等资源,所以需要授予RBAC : 02-gitlab-eventlistener-rbac.yaml

apiVersion: v1 
kind: ServiceAccount
metadata:
  name: tekton-triggers-gitlab-sa
secrets:
- name: gitlab-webhook-token
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: tekton-triggers-gitlab-minimal
rules:
  # Permissions for every EventListener deployment to function
  - apiGroups: ["triggers.tekton.dev"]
    resources: ["eventlisteners", "triggerbindings", "triggertemplates", "interceptors"]
    # resources: ["*"]
    verbs: ["get", "list"]
  - apiGroups: [""]
    # secrets are only needed for Github/Gitlab interceptors, serviceaccounts only for per trigger authorization
    resources: ["configmaps", "secrets", "serviceaccounts"]
    verbs: ["get", "list", "watch"]
  # Permissions to create resources in associated TriggerTemplates
  - apiGroups: ["tekton.dev"]
    resources: ["pipelineruns", "pipelineresources", "taskruns"]
    verbs: ["create"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: tekton-triggers-gitlab-binding
subjects:
  - kind: ServiceAccount
    name: tekton-triggers-gitlab-sa
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: tekton-triggers-gitlab-minimal
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: tekton-triggers-gitlab-minimal
rules:
  - apiGroups: ["triggers.tekton.dev"]
    resources: ["clusterinterceptors"]
    verbs: ["get", "list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: tekton-triggers-gitlab-binding
subjects:
  - kind: ServiceAccount
    name: tekton-triggers-gitlab-sa
    namespace: default
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: tekton-triggers-gitlab-minimal

4、最后一个task: deploy-task,需要部署到k8s集群,所以该pod需要一定的权限,定义RBAC: 03-task-deploy-to-cluster-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

5、基于maven构建的cache定义的pvc:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: maven-cache
spec:
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: 1Gi
  storageClassName: nfs-csi
  volumeMode: Filesystem

6、将该项目所有的task定义在一个文件: 05-task-source-2-image.yaml:

---
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: git-clone
spec:
  description: Clone the code repository to the workspace. 
  params:
    - name: git-repo-url
      type: string
      description: git repository url to clone
    - name: git-revision
      type: string
      description: git revision to checkout (branch, tag, sha, ref)
  workspaces:
    - name: source
      description: The git repo will be cloned onto the volume backing this workspace
  steps:
    - name: git-clone
      image: alpine/git:v2.36.1
      script: | 
        git clone -v $(params.git-repo-url) $(workspaces.source.path)/source
        cd $(workspaces.source.path)/source && git reset --hard $(params.git-revision)
---
apiVersion: tekton.dev/v1beta1
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
      workingDir: $(workspaces.source.path)/source
      volumeMounts:
        - name: m2
          mountPath: /root/.m2
      script: mvn clean install
  volumes:
    - name: m2
      persistentVolumeClaim:
        claimName: maven-cache
---
apiVersion: tekton.dev/v1beta1
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)
---
apiVersion: tekton.dev/v1beta1
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
      securityContext:
        runAsUser: 0
      env:
        - name: DOCKER_CONFIG
          value: /kaniko/.docker
      command:
        - /kaniko/executor
      args:
        - --dockerfile=$(params.dockerfile)
        - --context=$(workspaces.source.path)/source
        - --destination=$(params.image-url):$(params.image-tag)
---
apiVersion: tekton.dev/v1beta1
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.16
      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
      command: ["kubectl"]
      args:
        - "apply"
        - "-f"
        - "$(workspaces.source.path)/source/deploy/$(params.deploy-config-file)

7、将前面的task定义为pipeline资源:06-pipeine-s2i.yaml:

apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: source-to-image
spec:
  params:
    - name: git-repo-url
      type: string
      description: git repository url to clone
    - name: git-revision
      type: string
      description: git revision to checkout (branch, tag, sha, ref)
      default: main
    - name: image-build-context
      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" 
    - name: deploy-config-file
      description: The path to the yaml file to deploy within the git source
      default: all-in-one.yaml
  workspaces:
    - name: codebase
    - name: docker-config
  tasks:
    - name: git-clone
      taskRef:
        name: git-clone
      params:
        - name: git-repo-url
          value: "$(params.git-repo-url)"
        - name: git-revision
          value: "$(params.git-revision)"
      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

8、最后将trigger, triggerbind, triggertemplate定义:07-eventlisten.yaml

apiVersion: triggers.tekton.dev/v1beta1
kind: TriggerBinding
metadata:
  name: s2i-binding
spec:
  params:
  - name: git-revision
    value: $(body.checkout_sha)
  - name: git-repo-url
    value: $(body.repository.git_http_url)
  - name: image-url
    value: icloud2native/spring-boot-helloworld
  - name: version
    value: v0.10
---
apiVersion: triggers.tekton.dev/v1beta1
kind: TriggerTemplate
metadata:
  name: s2i-tt
spec:
  params:  # 定义参数
  - name: git-revision
  - name: git-repo-url
  - name: image-url
  - name: version
  resourcetemplates:
  - apiVersion: tekton.dev/v1beta1
    kind: PipelineRun
    metadata:
      generateName: s2i-trigger-run-  # TaskRun 名称前缀
    spec:
      serviceAccountName: default
      pipelineRef:
        name: source-to-image
      taskRunSpecs:
        - pipelineTaskName: deploy-to-cluster
          taskServiceAccountName: helloworld-admin
      params:
        - name: git-repo-url
          value: $(tt.params.git-repo-url)
        - name: git-revision
          value: $(tt.params.git-revision)
        - name: image-url
          value: $(tt.params.image-url)
        - name: version
          value: $(tt.params.version)
      workspaces:
        - name: codebase
          volumeClaimTemplate:
            spec:
              accessModes:
                - ReadWriteOnce
              resources:
                requests:
                  storage: 1Gi
              storageClassName: nfs-csi
        - name: docker-config
          secret:
            secretName: docker-config
---
apiVersion: triggers.tekton.dev/v1beta1
kind: EventListener
metadata:
  name: s2i-listener
spec:
  serviceAccountName: tekton-triggers-gitlab-sa
  triggers:
  - name: gitlab-push-events-trigger
    interceptors:
    - ref:
        name: "gitlab"
      params:
      - name: "secretRef"
        value:
          secretName: gitlab-webhook-token
          secretKey: webhookToken
      - name: "eventTypes"
        value:
          - "Push Hook"
          - "Tag Push Hook"
          - "Merge Request Hook"
    bindings:
    - ref: s2i-binding
    template:
      ref: s2i-tt

9、运行

kubectl apply -f .

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kx6fet5F-1684738152155)(images\image-20230113172916650.png)]

10、在gitlab上增加eventlistener的webhook, 取消SSL验证

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-iB0oAxda-1684738152156)(images\image-20230113173218082.png)]

2.4.3 项目测试

本地修改main分支文件,然后push, 查看tekton的dashboard上是否会触发:

1、gitlab上已经push 到main分支:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2732L9UX-1684738503653)(images\image-20230113174046921.png)]

2、查看是否触发tekton的pipeline执行

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-35tgfFOV-1684738503656)(images\image-20230113174154178.png)]

3、查看dockerhub以及kubernetes是否部署成功

在这里插入图片描述
在这里插入图片描述
未完待续 。。。

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

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

相关文章

海睿思分享 | 类chatgpt模型在信息抽取领域的应用

大语言模型(LLM,Large Language Model)是指能够处理海量数据、拥有百亿级参数的深度学习模型,它已成为⼈⼯智能领域中的新热点。2022 年 11 ⽉ 30 号 ChatGPT 发布,其卓越的性能表现给整个⾏业带来了巨⼤的冲击。⼈们不…

2023年春季期末网球理论复习资料

(含2023/2022/2021时事题,基于2012年期末网球理论复习资料修改) 目录 网球的起源 网球的主要赛事 三大网球协会 大满贯 网球的场地 1. 球场线 2. 网球的球网 3.场地的类型 网球的规则 1.发球规则 2.计分方法 3.通则 4.赛…

在Octane中提升渲染速度的技巧(第1部分)

Mike Griggs是一位数字内容创建者,在为众多客户创建Mograph,VFX和CGI方面拥有超过二十年的经验。迈克格里格斯(Mike Griggs)在Creative Bloq上写了很多博客,该博客是国际媒体集团和领先的数字出版商Future plc的一部分…

手势识别q

本文介绍使用光电传感器的手势识别。 光电传感器手势识别区别于视觉手势识别,没有复杂的算法。LED发射光,当光线接触到手发生反射,反射光被传感器检测到,传感器检测到不同的手势反射的光不同,再根据芯片的内置算法判别…

【笔试强训编程题】Day5.( 统计回文 45842 ) 和( 连续最大和 58539)

作者简介:大家好,我是未央; 博客首页:未央.303 系列专栏:笔试强训编程题 每日一句:人的一生,可以有所作为的时机只有一次,那就是现在!!!! 文章目录…

【嵌入式烧录/刷写文件】-2.5-Fill填充Intel Hex文件

案例背景(共8页精讲):该篇将告诉你,如何对一个Hex文件进行填充: 对“起始地址”和“结束地址”内的非连续的Block块,进行填充;自定义填充范围。 目录 1 为什么要“Fill填充” 2 使用Vector HexView工具“填充”Hex…

【Linux0.11代码分析】09 之 ELF可执行程序02 - Section Headers解析

【Linux0.11代码分析】09 之 ELF可执行程序02 - Section Headers解析 一、ELF概述二、ELF的组成结构2.1 ELF header:解析出 section headers 含31个section节和 program headers 含13个segment段2.2 Section Headers:获取当前程序的31个section节区信息2…

18-03 MySQL高可用方案与选择

主从复制 读写分离 流程 原理 bin log STATEMENT 优点:记录的是执行的SQL,比较省空间,降低了主从复制时的IO开销缺点:由于记录的是SQL,所以MySQL多个节点之间复制的时候,特定场景下会导致数据不一致的情况 ROW 优点…

【多线程进阶二】JUC工具类 线程安全的集合类 死锁

目录 一、JUC工具类 🍅1、Callable接口 🍅2、ReentrantLock 🍅3、原子类 🍅4、Semaphore信号量 🍅5、CountDownLatch 二、线程安全的集合类 ​🍅1、多线程环境下,怎么使用线程安全…

第十三届蓝桥杯国赛JavaB组题解

A. 重合次数 思路: 枚举不同的时刻,判断哪些时刻秒针和分针表示的数字是相同的。这道题坑就坑在:xx:59:59 xx:00:00分针和时。也就是说一个小时会重叠两次。 题目要求是分钟和秒钟的重叠次数,故时钟,分钟,秒钟同时重叠的次数不算(这题还是有点咬文嚼字了…

MySQL---事务

1. 事务操作 开启事务:Start Transaction 任何一条DML语句(insert、update、delete)执行, 标志事务的开启命令:BEGIN 或 START TRANSACTION 提交事务:Commit Transaction 成功的结束,将所有的DML语句操作历史记录…

G2O学习使用

g2o全称是General Graph Optimization,也就是图优化,我们在做SLAM后端或者更加常见的任何优化问题(曲线拟合)都可以使用G2O进行处理。 就经验而言,solvers给人的感觉是大同小异,而 types 的选取&#xff0…

C语言小游戏——扫雷

前言 结合前边我们所学的C语言知识,本期我们将使用C语言实现一个简单的小游戏——扫雷 目录 前言 总体框架设计 多文件分装程序 各功能模块化实现 初始化棋盘 棋盘打印 埋雷 判赢与排雷 游戏逻辑安排 总结 总体框架设计 和三子棋相同,游戏开始时…

32岁测试工程师,陷入中年危机,最终我裸辞了....

前言 今年32岁,我从公司离职了,是裸辞。 前段时间,我有一件事情一直憋在心里很难受,想了很久也没找到合适的人倾诉,就借着今天写出来。 我一个十几年IT经验,七年测试经验的职场老人,我慢慢涨…

02 Android开机启动之BootLoader及kernel的启动

Android开机启动之BootLoader及kernel的启动 1、booloader的启动流程 第一阶段:硬件初始化,SVC模式,关闭中断,关闭看门狗,初始化栈,进入C代码 第二阶段:cpu/board/中断初始化;初始化内存以及flash,将kernel从flash中拷贝到内存中,执行bootm,启动内核 2、kernel的启…

学习如何将Jenkins与UI测试报告完美整合,事半功倍,轻松获取高薪职位!

目录 引言 (一)在本地整合出报告 1.在cmd分别安装pytest和allure-pytest 2.进入需要执行的代码所在的路径 3.运行测试报告,代码如下 4.解析此json文件,代码如下(新打开cmd进入路径) 5.打开此HTML文件…

包管理工具

包 package,代表了一组特定功能的源码集合。 包管理工具 管理包的应用软件,可以对包进行下载安装、更新、删除、上传等操作。 借助包管理工具,可以快速开发项目,提升开发效率。 常用包管理工具 npm(nodejs官方内…

百度API实现自动写诗

作者介绍 张琪,男,西安工程大学电子信息学院,2022级研究生 研究方向:机器视觉与人工智能 电子邮件:3126743452qq.com 王泽宇,男,西安工程大学电子信息学院,2022级研究生&#xff0…

Spring——Spring_IOC

1.Spring_IOC概念引入 控制反转 2.Spring_IOC代码测试 IOC代码演示 控制反转:就是创建对象的权力交给了容器 1.创建一个接口,定义一个抽象方法 package org.example;public interface Empdao {int addemp(); } 2.创建一个实现类,实现这…

两台电脑之间怎么互相传文件?

​随着技术的发展,我们似乎可以从家中或工作电脑远程访问另一台电脑。同时,一些用户也在想,“我能不能把文件从一台电脑远程传输到另一台电脑,这样我就可以在本地电脑上随心所欲地查看和编辑文件了”。 这个问题的答案是…