#2. 构建并运行组件 CI
创建一个包含三个任务的流水线,手动运行前端,并将经过验证的配置转换为运行模板。然后对 Go 后端重复此过程。完成后,每个组件都会拥有已发布的镜像摘要和可复用的执行设置。
前提条件:已完成准备应用和环境中的 CI 检查点。 目前不需要 Triggers、应用 Deployment 或发布审批。
#1. 创建组件流水线
打开流水线,选择练习命名空间,然后选择创建。使用 YAML 编辑器粘贴下面对应的完整示例。两种语言的图有意保持相同:
clone -> test -> build-image| 任务 | 目录条目 | 输入 | 输出 |
|---|---|---|---|
clone | git-clone:0.9 | Repository URL、revision 和 Git authentication Workspace | 源文件和已解析的 commit SHA |
test | nodejs:0.1 或 golang:0.1 | Source Workspace 以及测试/构建命令 | 通过的检查和构建输出 |
build-image | buildah:0.10 | Source Workspace、Containerfile、镜像引用和 registry 配置 | 已推送的镜像和摘要 |
Complete Node.js Pipeline YAML
apiVersion: tekton.dev/v1
kind: Pipeline
metadata:
name: application-cicd-node
namespace: <pipeline-namespace>
annotations:
integrations.tekton.dev/integrations: |
- name: code
interface:
apiVersion: connectors.alauda.io/v1alpha1
category: GitCodeRepository
connectorRef:
setAtRuntime: true
apiVersion: connectors.alauda.io/v1alpha1
attributes:
- name: url
param: git-url
refPath: [tasks.clone.params.url]
- name: revision
param: revision
refPath: [tasks.clone.params.revision]
workspaces:
- name: git-basic-auth
workspace: git-auth
refPath: [tasks.clone.workspaces.basic-auth]
- name: image
interface:
apiVersion: connectors.alauda.io/v1alpha1
category: OCIArtifact
connectorRef:
setAtRuntime: true
apiVersion: connectors.alauda.io/v1alpha1
workspaces:
- name: registry-config
workspace: registry-config
refPath: [tasks.build-image.workspaces.registry-config]
integrations.tekton.dev/set-at-runtime: "true"
labels:
app.kubernetes.io/part-of: application-cicd-demo
spec:
params:
- name: git-url
type: string
description: Repository URL. The component Run Template normally fixes this value.
- name: revision
type: string
default: refs/heads/main
description: Branch, tag, or commit to build.
- name: image
type: string
description: Registry image reference, including the tag to publish.
- name: node-command
type: string
default: npm test && npm run lint && npm run build
description: Verified Node.js test, lint, and build command.
- name: skip-push
type: string
default: "false"
description: Set true only for local validation that must not publish an image.
workspaces:
- name: source
- name: git-auth
- name: npm-config
optional: true
- name: ca-bundle
optional: true
- name: npm-cache
optional: true
- name: registry-config
results:
- name: source-commit
value: $(tasks.clone.results.commit)
- name: image-url
value: $(tasks.build-image.results.IMAGE_URL)
- name: image-digest
value: $(tasks.build-image.results.IMAGE_DIGEST)
tasks:
- name: clone
taskRef:
resolver: hub
params:
- name: catalog
value: catalog
- name: type
value: artifact
- name: kind
value: task
- name: name
value: git-clone
- name: version
value: "0.9"
params:
- name: url
value: $(params.git-url)
- name: revision
value: $(params.revision)
- name: deleteExisting
value: "true"
- name: gitInitImage
value: <internal-git-image>
workspaces:
- name: output
workspace: source
- name: basic-auth
workspace: git-auth
- name: test
runAfter:
- clone
taskRef:
resolver: hub
params:
- name: catalog
value: catalog
- name: type
value: artifact
- name: kind
value: task
- name: name
value: nodejs
- name: version
value: "0.1"
params:
- name: command
value: $(params.node-command)
- name: nodeImage
value: <internal-node-image>
workspaces:
- name: source
workspace: source
- name: npm-config
workspace: npm-config
- name: ca-bundle
workspace: ca-bundle
- name: cache
workspace: npm-cache
- name: build-image
runAfter:
- test
taskRef:
resolver: hub
params:
- name: catalog
value: catalog
- name: type
value: artifact
- name: kind
value: task
- name: name
value: buildah
- name: version
value: "0.10"
params:
- name: images
value:
- $(params.image)
- name: containerfile
value: ./Containerfile
- name: context
value: .
- name: storageDriver
value: vfs
- name: skipPush
value: $(params.skip-push)
- name: builderImage
value: <internal-buildah-image>
workspaces:
- name: source
workspace: source
- name: registry-config
workspace: registry-configComplete Go Pipeline YAML
apiVersion: tekton.dev/v1
kind: Pipeline
metadata:
name: application-cicd-go
namespace: <pipeline-namespace>
annotations:
integrations.tekton.dev/integrations: |
- name: code
interface:
apiVersion: connectors.alauda.io/v1alpha1
category: GitCodeRepository
connectorRef:
setAtRuntime: true
apiVersion: connectors.alauda.io/v1alpha1
attributes:
- name: url
param: git-url
refPath: [tasks.clone.params.url]
- name: revision
param: revision
refPath: [tasks.clone.params.revision]
workspaces:
- name: git-basic-auth
workspace: git-auth
refPath: [tasks.clone.workspaces.basic-auth]
- name: image
interface:
apiVersion: connectors.alauda.io/v1alpha1
category: OCIArtifact
connectorRef:
setAtRuntime: true
apiVersion: connectors.alauda.io/v1alpha1
workspaces:
- name: registry-config
workspace: registry-config
refPath: [tasks.build-image.workspaces.registry-config]
integrations.tekton.dev/set-at-runtime: "true"
labels:
app.kubernetes.io/part-of: application-cicd-demo
spec:
params:
- name: git-url
type: string
description: Repository URL. The component Run Template normally fixes this value.
- name: revision
type: string
default: refs/heads/main
description: Branch, tag, or commit to build.
- name: image
type: string
description: Registry image reference, including the tag to publish.
- name: go-command
type: string
default: go test -mod=readonly ./... && go vet ./... && go build -o bin/server
./cmd/server
description: Verified Go test and build command.
- name: skip-push
type: string
default: "false"
description: Set true only for local validation that must not publish an image.
workspaces:
- name: source
- name: git-auth
- name: go-cache
optional: true
- name: ca-bundle
optional: true
- name: registry-config
results:
- name: source-commit
value: $(tasks.clone.results.commit)
- name: image-url
value: $(tasks.build-image.results.IMAGE_URL)
- name: image-digest
value: $(tasks.build-image.results.IMAGE_DIGEST)
tasks:
- name: clone
taskRef:
resolver: hub
params:
- name: catalog
value: catalog
- name: type
value: artifact
- name: kind
value: task
- name: name
value: git-clone
- name: version
value: "0.9"
params:
- name: url
value: $(params.git-url)
- name: revision
value: $(params.revision)
- name: deleteExisting
value: "true"
- name: gitInitImage
value: <internal-git-image>
workspaces:
- name: output
workspace: source
- name: basic-auth
workspace: git-auth
- name: test
runAfter:
- clone
taskRef:
resolver: hub
params:
- name: catalog
value: catalog
- name: type
value: artifact
- name: kind
value: task
- name: name
value: golang
- name: version
value: "0.1"
params:
- name: command
value: $(params.go-command)
- name: goImage
value: <internal-go-image>
- name: environments
value:
- CGO_ENABLED=0
- GOPROXY=off
- GOTOOLCHAIN=local
workspaces:
- name: source
workspace: source
- name: cache
workspace: go-cache
- name: ca-bundle
workspace: ca-bundle
- name: build-image
runAfter:
- test
taskRef:
resolver: hub
params:
- name: catalog
value: catalog
- name: type
value: artifact
- name: kind
value: task
- name: name
value: buildah
- name: version
value: "0.10"
params:
- name: images
value:
- $(params.image)
- name: containerfile
value: ./Containerfile
- name: context
value: .
- name: storageDriver
value: vfs
- name: skipPush
value: $(params.skip-push)
- name: builderImage
value: <internal-buildah-image>
workspaces:
- name: source
workspace: source
- name: registry-config
workspace: registry-configtest 任务将检查合并到一个简短的初始流水线中。它们通过
&& 连接,因此测试失败会阻止构建和镜像推送。
如果需要单独重试或报告,稍后再将它们拆分为独立任务。
要练习图形编辑器,请通过添加三个目录任务、连接它们的 Workspace,并按上述顺序设置
runAfter,创建相同的图。请将完整 YAML 用作参数、镜像、
Integrations 和结果的参考,而不要将其视为三个互不相关的任务定义。
如果使用 CLI,请在替换其中的占位符后,将选定的代码块保存为 ci-node.yaml 或 ci-go.yaml
,然后运行:
kubectl apply -f ci-node.yaml验证:保存的流水线包含三个按顺序排列的任务,而不是三个并行任务。仅保存流水线不会创建 PipelineRun。
#2. 绑定首次运行的输入
打开 Pipeline 并选择 Execute。下方的 Run Template 保存了固定的仓库、Connector、身份和 Workspace 配置。它仅为手动运行公开修订版本和镜像标签。
Complete frontend Run Template YAML
apiVersion: triggers.tekton.dev/v1beta1
kind: TriggerTemplate
metadata:
name: application-cicd-frontend-run
namespace: <pipeline-namespace>
labels:
triggertemplate.triggers.tekton.dev/usage: runtime-config
tekton.dev/pipeline: application-cicd-node
app.kubernetes.io/part-of: application-cicd-demo
annotations:
tekton.dev/displayName: Frontend component run template
integrations.tekton.dev/disabled: "false"
spec:
params:
- name: revision
description: Runtime Git revision from a manual run or event.
default: refs/heads/main
- name: image-tag
description: Runtime image tag or release label.
default: manual-001
resourcetemplates:
- apiVersion: tekton.dev/v1
kind: PipelineRun
metadata:
generateName: application-cicd-frontend-
namespace: <pipeline-namespace>
labels:
app.kubernetes.io/part-of: application-cicd-demo
app.kubernetes.io/component: frontend
annotations:
integrations.tekton.dev/integrations: |
- name: code
interface:
apiVersion: connectors.alauda.io/v1alpha1
category: GitCodeRepository
connectorRef:
setAtRuntime: false
name: <git-connector>
namespace: <connector-namespace>
apiVersion: connectors.alauda.io/v1alpha1
attributes:
- name: url
value: <fixed-frontend-repository-url>
param: git-url
bindings:
- path: tasks.clone.params.url
- path: pipelineRuns.0.params.git-url
- name: revision
value: $(tt.params.revision)
param: revision
bindings:
- path: tasks.clone.params.revision
- path: pipelineRuns.0.params.revision
configuration:
connectors: []
params:
- name: repository
setAtRuntime: false
value: <fixed-frontend-repository-url>
- name: revision
setAtRuntime: true
value: $(tt.params.revision)
workspaces:
- name: git-basic-auth
workspace: git-auth
refPath:
- tasks.clone.workspaces.basic-auth
- pipelineRuns.0.workspaces.git-auth
- name: image
interface:
apiVersion: connectors.alauda.io/v1alpha1
category: OCIArtifact
connectorRef:
setAtRuntime: false
name: <staging-oci-connector>
namespace: <connector-namespace>
apiVersion: connectors.alauda.io/v1alpha1
configuration:
connectors: []
workspaces:
- name: registry-config
workspace: registry-config
refPath:
- tasks.build-image.workspaces.registry-config
- pipelineRuns.0.workspaces.registry-config
integrations.tekton.dev/set-at-runtime: "true"
style.tekton.dev/descriptors: |
- path: params.git-url
x-descriptors:
- urn:alm:descriptor:com.tectonic.ui:hidden
- path: params.revision
x-descriptors: []
- path: params.image
x-descriptors: []
- path: params.node-command
x-descriptors:
- urn:alm:descriptor:com.tectonic.ui:hidden
- path: params.skip-push
x-descriptors:
- urn:alm:descriptor:com.tectonic.ui:hidden
- path: workspaces.source
x-descriptors:
- urn:alm:descriptor:com.tectonic.ui:hidden
- path: workspaces.git-auth
x-descriptors:
- urn:alm:descriptor:com.tectonic.ui:hidden
- path: workspaces.npm-config
x-descriptors:
- urn:alm:descriptor:com.tectonic.ui:hidden
- path: workspaces.ca-bundle
x-descriptors:
- urn:alm:descriptor:com.tectonic.ui:hidden
- path: workspaces.npm-cache
x-descriptors:
- urn:alm:descriptor:com.tectonic.ui:hidden
- path: workspaces.registry-config
x-descriptors:
- urn:alm:descriptor:com.tectonic.ui:hidden
spec:
pipelineRef:
name: application-cicd-node
params:
- name: git-url
value: <fixed-frontend-repository-url>
- name: revision
value: $(tt.params.revision)
- name: image
value: <staging-registry>/<team>/frontend:$(tt.params.image-tag)
- name: node-command
value: npm test && npm run lint
&& npm run build
workspaces:
- name: source
volumeClaimTemplate:
spec:
accessModes:
- ReadWriteOnce
storageClassName: <workspace-storage-class>
resources:
requests:
storage: 1Gi
- name: git-auth
csi:
driver: connectors-csi
readOnly: true
volumeAttributes:
connector.name: <git-connector>
connector.namespace: <connector-namespace>
configuration.names: gitconfig
- name: npm-cache
emptyDir: {}
- name: registry-config
csi:
driver: connectors-csi
readOnly: true
volumeAttributes:
connector.name: <staging-oci-connector>
connector.namespace: <connector-namespace>
configuration.names: registry-config
taskRunTemplate:
serviceAccountName: <pipeline-service-account>
podTemplate:
securityContext:
fsGroup: 65532
timeouts:
pipeline: 1hComplete backend Run Template YAML
apiVersion: triggers.tekton.dev/v1beta1
kind: TriggerTemplate
metadata:
name: application-cicd-backend-run
namespace: <pipeline-namespace>
labels:
triggertemplate.triggers.tekton.dev/usage: runtime-config
tekton.dev/pipeline: application-cicd-go
app.kubernetes.io/part-of: application-cicd-demo
spec:
params:
- name: revision
default: refs/heads/main
- name: image-tag
default: manual-001
resourcetemplates:
- apiVersion: tekton.dev/v1
kind: PipelineRun
metadata:
generateName: application-cicd-backend-
namespace: <pipeline-namespace>
annotations:
integrations.tekton.dev/integrations: |
- name: code
interface:
apiVersion: connectors.alauda.io/v1alpha1
category: GitCodeRepository
connectorRef:
setAtRuntime: false
name: <git-connector>
namespace: <connector-namespace>
apiVersion: connectors.alauda.io/v1alpha1
attributes:
- name: url
value: <fixed-backend-repository-url>
param: git-url
bindings:
- path: tasks.clone.params.url
- path: pipelineRuns.0.params.git-url
- name: revision
value: $(tt.params.revision)
param: revision
bindings:
- path: tasks.clone.params.revision
- path: pipelineRuns.0.params.revision
configuration:
connectors: []
workspaces:
- name: git-basic-auth
workspace: git-auth
refPath:
- tasks.clone.workspaces.basic-auth
- pipelineRuns.0.workspaces.git-auth
params:
- name: repository
setAtRuntime: false
value: <fixed-backend-repository-url>
- name: revision
setAtRuntime: true
value: $(tt.params.revision)
- name: image
interface:
apiVersion: connectors.alauda.io/v1alpha1
category: OCIArtifact
connectorRef:
setAtRuntime: false
name: <staging-oci-connector>
namespace: <connector-namespace>
apiVersion: connectors.alauda.io/v1alpha1
configuration:
connectors: []
workspaces:
- name: registry-config
workspace: registry-config
refPath:
- tasks.build-image.workspaces.registry-config
- pipelineRuns.0.workspaces.registry-config
integrations.tekton.dev/set-at-runtime: "true"
style.tekton.dev/descriptors: |
- path: params.git-url
x-descriptors:
- urn:alm:descriptor:com.tectonic.ui:hidden
- path: params.revision
x-descriptors: []
- path: params.image
x-descriptors: []
- path: params.go-command
x-descriptors:
- urn:alm:descriptor:com.tectonic.ui:hidden
- path: params.skip-push
x-descriptors:
- urn:alm:descriptor:com.tectonic.ui:hidden
- path: workspaces.source
x-descriptors:
- urn:alm:descriptor:com.tectonic.ui:hidden
- path: workspaces.git-auth
x-descriptors:
- urn:alm:descriptor:com.tectonic.ui:hidden
- path: workspaces.go-cache
x-descriptors:
- urn:alm:descriptor:com.tectonic.ui:hidden
- path: workspaces.ca-bundle
x-descriptors:
- urn:alm:descriptor:com.tectonic.ui:hidden
- path: workspaces.registry-config
x-descriptors:
- urn:alm:descriptor:com.tectonic.ui:hidden
labels:
app.kubernetes.io/part-of: application-cicd-demo
app.kubernetes.io/component: backend
spec:
pipelineRef:
name: application-cicd-go
params:
- name: git-url
value: <fixed-backend-repository-url>
- name: revision
value: $(tt.params.revision)
- name: image
value: <staging-registry>/<team>/backend:$(tt.params.image-tag)
- name: go-command
value: go test -mod=readonly ./... && go vet ./... && go build -o bin/server
./cmd/server
workspaces:
- name: source
volumeClaimTemplate:
spec:
accessModes:
- ReadWriteOnce
storageClassName: <workspace-storage-class>
resources:
requests:
storage: 1Gi
- name: git-auth
csi:
driver: connectors-csi
readOnly: true
volumeAttributes:
connector.name: <git-connector>
connector.namespace: <connector-namespace>
configuration.names: gitconfig
- name: go-cache
emptyDir: {}
- name: registry-config
csi:
driver: connectors-csi
readOnly: true
volumeAttributes:
connector.name: <staging-oci-connector>
connector.namespace: <connector-namespace>
configuration.names: registry-config
taskRunTemplate:
serviceAccountName: <pipeline-service-account>
podTemplate:
securityContext:
fsGroup: 65532
timeouts:
pipeline: 1h首次运行时,请在 Execute 表单中配置以下值:
| Run 字段 | 首次运行的值 |
|---|---|
git-url | 组件仓库的固定克隆 URL |
revision | 教程 1 中记录的完整提交 SHA |
image | 带有新标签的暂存组件仓库,例如 manual-001 |
node-command | npm test && npm run lint && npm run build |
go-command | go test -mod=readonly ./... && go vet ./... && go build -o bin/server ./cmd/server |
source Workspace | 从选定存储类创建的新卷声明,而不是共享的现有 PVC |
git-auth Workspace | Git Connector 的 gitconfig CSI 配置 |
registry-config Workspace | 暂存 OCI Connector 的 registry-config CSI 配置 |
| language cache Workspace | emptyDir;只有匹配的语言 Task 会使用它 |
| Execution identity and timeout | 已批准的 CI 服务账户;Pipeline 超时设置为一小时 |
| Run label | app.kubernetes.io/part-of: application-cicd-demo,包括本次首次手动运行 |
启用 Integrations,然后选择匹配的 Git 和 OCI Connector。检查 YAML,确认 Connector 名称、命名空间和 Integration 映射均得到保留。示例还将 Pod 的 fsGroup 设置为 65532,以便共享源卷可由 catalog Task 的非 root 用户写入。
在此无依赖示例中,不绑定可选的 package-config 和 CA Workspace。如果端点需要自定义 CA,请绑定正确的 ConfigMap,并同时配置 Git 和 registry 信任;空的 emptyDir 不是 CA bundle。对于实际的 NPM 依赖项,请使用
NPM Connector guide。
提交运行。如果表单无法表达某个绑定,请使用其 YAML 视图;Run Template 代码块记录了确切的 CSI 和卷声明字段。
#3. 检查 Task、日志和结果
打开新的 PipelineRun。依次选择 clone、test,然后选择 build-image
以检查每个 Task 的步骤和日志。运行失败时,请检查第一个失败的步骤;未经检查便重试可能会掩盖配置问题。
Pipeline 暴露三个结果:
results:
- name: source-commit
value: $(tasks.clone.results.commit)
- name: image-url
value: $(tasks.build-image.results.IMAGE_URL)
- name: image-digest
value: $(tasks.build-image.results.IMAGE_DIGEST)$(params.revision) 是运行的输入;source-commit 记录实际检出的内容。Workspace 在 Task 之间传递文件;结果传递提交或摘要等较小的值。即使之后有人移动其标签,image-digest 仍可标识构建的镜像。
也可以从 CLI 检查相同的证据:
tkn pipelinerun describe <run-name> -n <pipeline-namespace>
tkn pipelinerun logs <run-name> -n <pipeline-namespace>
kubectl get pipelinerun <run-name> -n <pipeline-namespace> -o jsonpath='{.status.results}'验证:PipelineRun 已成功,source-commit 与你的提交匹配,并且 image-digest 包含一个 sha256: 摘要。在暂存 registry 中确认标签和摘要。记录运行名称、提交、仓库和摘要,以供教程 4 使用。
#4. 保存并重复使用 Run Template
在成功的 PipelineRun 执行条目中,选择 Save Run Template,然后创建组件 Run Template。或者,打开 Run Template,为相应的 Pipeline 创建模板。
固定 Git 仓库、Connector 选择、命令、Workspace、身份和超时。仅将 revision 和 image 标记为 Set at Runtime。对于手动运行,image 是暂存仓库中的完整引用,包括新标签。之后的事件会同时将提交 SHA 提供为修订版本和镜像标签。
将保存的资源与上方的选项卡进行比较。产品会将 Run
Template 存储为带有 triggertemplate.triggers.tekton.dev/usage: runtime-config 标签的 TriggerTemplate。不要丢失
Integration 注解,也不要将源 Workspace 更改为已完成运行中的 PVC 名称。保留
volumeClaimTemplate,以便后续运行获得全新的存储。
也可以将选定的区块保存为本地 YAML 文件并应用:
kubectl apply -f ci-frontend-run-template.yaml从 Run Template 启动另一次运行,提供提交和新的镜像标签,例如 manual-002。保持仓库不变。该运行应仅要求提供运行时值,并使用不同的源 PVC。
#5. 对后端重复执行
从相应选项卡创建 Go Pipeline 和后端 Run Template。图和
Workspace 模式保持不变。语言 Task 更改为 golang:0.1,命令运行
go test、go vet 和 go build,Containerfile
则打包 bin/server。
示例设置 CGO_ENABLED=0、GOPROXY=off 和 GOTOOLCHAIN=local。使用与应用所需版本和运行时
集群 CPU 架构匹配的 Go 工具镜像。对于这个仅使用标准库的应用,不需要软件包代理或自动下载工具链。
使用后端提交和全新标签运行后端模板。确认它仅发布后端镜像。保持两个源代码仓库、镜像仓库和 Run Template 相互独立。
#检查点和故障排查
现在你已拥有两个成功的手动构建和可重复使用的 Run Template。要练习失败路径,请在练习分支中引入失败测试并
运行该提交。test 必须失败,build-image 不得运行,并且不应发布新镜像。在继续之前恢复测试更改。
| 症状 | 首要检查 |
|---|---|
| Catalog 解析失败 | Catalog 快照、解析器参数、Task 名称和版本。 |
| 源 PVC 保持 Pending | StorageClass、配额和置备事件。使用 kubernetes.io/no-provisioner 的类不会动态创建存储。 |
| Clone 失败 | 仓库 URL、Connector 就绪状态、Git CA 信任和 CSI 配置。 |
| 源在 Task 之间消失 | source 必须为每次运行使用一个 PVC,而不是 emptyDir。 |
| Node.js 或 Go 检查失败 | 仓库根目录、Task 日志、运行时版本和构建命令。 |
Buildah 在 unshare(CLONE_NEWUSER) 处失败 | 节点无法提供构建器的用户命名空间。请管理员解决构建器前置条件;不要禁用命名空间安全控制。 |
| Buildah 或镜像推送失败 | 已批准的构建器安全设置、基础镜像访问权限、registry Connector、CA 和配额。 |
你可以在此处停下,练习创建、执行和检查 Pipelines。 下一步:使用事件自动化 CI。
上一页:准备应用和环境。