使用 Tekton 构建CI流程
安装 Tekton
安装 Tekton 核心及Tekton pipelines
curl -o tekton-pipeline.yaml https://storage.googleapis.com/tekton-releases/pipeline/latest/release.yaml允许同一个Pipeline挂载两个pvc
默认tekton 禁用了同一个pipeline 挂载两个不同pvc, 当前 TriggerTemplate 示例中 source、 cache 两个workspaces 使用了不同的pvc, 所以需要开放限制
编辑 tekton-pipeline.yaml,找到 configmap feature-flags, 看到 disable-affinity-assistant, 值修改为true 。
如果安装前没有修改,也可以安装后kubectl edit configmap feature-flags -n tekton-pipelines 修改
安装 tekton-pipeline
kubectl apply -f tekton-pipeline.yaml配置Tekton在运行工作流时使用 manual 存储类请求10Gi的持久化卷
kubectl create configmap config-artifact-pvc \
--from-literal=size=10Gi \
--from-literal=storageClassName=manual \
-o yaml -n tekton-pipelines \
--dry-run=client | kubectl replace -f -检查pods
安装 Tekton Cli
安装 Tekton Triggers
检查 pods
安装 Tekton Dashboard
检查 pods
创建 tekton-dashboard-ingress.yaml, tekton 没有用户管理,所以建议限制ip段访问
构建 CI 流程

概念描述
Tekton Pipelines 实体列表
Task
定义一系列启动特定构建或交付工具的步骤,这些工具接收特定的输入并产生特定的输出。
TaskRun
用特定的输入、输出和执行参数实例化一个Task以供执行。可以单独调用,也可以作为Pipeline的一部分调用。
Pipeline
定义一系列完成特定构建或交付目标的任务。可以由事件触发或从PipelineRun调用。
PipelineRun
用特定的输入、输出和执行参数实例化一个Pipeline以供执行。
触发器(Triggers) 和 事件监听(EventListeners)
Tekton Triggers 包含在 Kubernetes 集群上运行的控制器服务以及 Kubernetes 自定义资源 (CRD),它们扩展了 Tekton Pipelines 的功能以支持事件:
EventListener- 在Kubernetes集群的指定端口监听事件。指定一个或多个Triggers。Trigger-EventListener检测到事件时会发生什么。一个Trigger指定一个TriggerTemplate,一个TriggerBinding,还有一个可选的Interceptor。TriggerTemplate- 在EventListener检测到事件时实例化或执行的资源(例如TaskRun或PipelineRun)指定蓝图。它暴露了可以在模板资源中的任何位置使用的参数。TriggerBinding- 从EventListeners的Trigger定义中的提取扩展的变量数据,以及填充相应的TriggerTemplate中定义的参数。然后TriggerTemplate根据参数值填充相关的TaskRun或PipelineRun中的参数。ClusterTriggerBinding-TriggerBinding的集群范围版本,特别适合在集群中重用。Interceptor-Interceptor是在 TriggerBinding 之前运行的特定平台的catch-all事件处理器。 它允许您执行有效数据过滤、验证(使用秘密)、转换、定义和测试触发条件,以及实现其他有用的处理。 一旦事件数据通过Interceptor,它就会在您将有效数据传递给TriggerBinding之前进入Trigger。 您还可以使用Interceptor来修改关联Trigger的行为。
拉取模板代码
Tasks/Pipelines 可以从https://hub.tekton.dev/ 中寻找已有项目参考
clone编写好的示例和模板, 期中部分tasks 是从https://hub.tekton.dev/ 中下载修改的。
目录结构
创建 Tasks
创建 Pipelines
示例 Pipelines 中的Pipeline 流程图

执行创建pipelines
创建 pvcs
酌情修改pvcs 中的yaml 内容,创建cache pvc, size 10Gi , storageClassName: manual
创建 Triggers
创建EventListernets 和 Triggers使用的sa, 并授权
酌情修改并创建 TriggerBindings
TriggerTemplates根据 Pipeline 创建PipelineRun, 创建 TriggerTemplates
创建 EventListerners
创建tekton 访问 gitlab 的认证 secret
gitlab-secret.yaml
创建tekton 访问registry 仓库的认证secret
config.json
项目代码集成webhook
创建 gitlab webhook 使用的 secret gitlab-webhook-secret.yaml
gitlab 项目中
设置-集成 输入tekton webhook地址
Add webhook
酌情修改并创建EventListerners
示例跟默认是只监听了 项目 Tag push event,Code Git Repo 打tag 会通知 EventListerner, EventListerner 再通过 Interceptors、TriggerBindings、TriggerTempates 生成对应PipelineRun
参考
https://github.com/paradeum-team/tekton-catalog/
https://www.qikqiak.com/post/create-ci-pipeline-with-tekton-1/
https://www.bianchengquan.com/article/511721.html
https://www.qikqiak.com/k8strain2/devops/tekton/overview/
https://github.com/tektoncd/dashboard/blob/main/docs/install.md
https://github.com/tektoncd/pipeline/blob/v0.31.0/docs/install.md
https://github.com/tektoncd/triggers/blob/v0.16.0/docs/install.md
https://github.com/tektoncd/operator
https://tekton.dev/docs/getting-started/
https://tekton.dev/docs/triggers/install/
https://github.com/IBM/ibm-garage-tekton-tasks
https://github.com/tektoncd/catalog/
https://kubernetes.io/zh/docs/tasks/debug-application-cluster/debug-running-pod/
http://hbchen.com/post/devops/2021-08-09-tekton-golang/
https://cloud.tencent.com/developer/article/1645052
https://www.51cto.com/article/668514.html
https://seankhliao.com/blog/12020-04-28-tekton-pipelines/
https://spex.top/archives/Kubernetes-cronjon-auto-cleanup-tekton-pipelinerun.html
Last updated