#4. 批准并交付一个版本
将一个经过测试的镜像提升到目标 registry,在批准后部署该确切镜像,并验证应用。最后练习回滚并清理练习环境。
前提条件:frontend 和 backend CI 运行成功,已记录其 staging digest,以及事件驱动 CI 检查点。手动交付可以独立于 webhooks 学习。
#目录
1. 管理员:准备版本边界2. 创建并验证初始原生应用3. 创建组件 CD Pipeline 和 Run Template跟踪摘要完成晋级4. 运行并批准一次手动发布5. 启用基于制品的交付6. 验证回滚并保留审计记录故障排除7. 清理练习资源检查点和后续步骤#1. 管理员:准备版本边界
使用单独的练习运行时命名空间,例如 cicd-tutorial-app。
安装 Manual Approval Gate,
授权选定的版本批准者,并准备以下内容:
| 资源 | 所需配置 |
|---|---|
| 目标 OCI Connector | 对练习版本 registry 的推送访问权限。 |
| Kubernetes Connector | 仅限练习应用命名空间的部署访问权限。 |
| 版本 service account | <delivery-service-account> 可以使用这些 Connector 并拉取工具镜像。 |
| 应用拉取 Secret | 节点可以拉取初始 staging 镜像以及后续目标镜像。 |
| Catalog | run-script:0.1、skopeo-copy:0.1 和 workload-apply:0.1,并包含下文使用的 digest 结果契约。 |
| 批准策略 | 已启用 Connector 批准和代理权限检查。 |
请参阅 Kubernetes Connector
和 Connector 批准。
通过受支持的 Connector 配置工作流设置 enable-connectors-approval 和 enable-connector-proxy-permissions。
在替换其占位符后,将以下 AccessPolicy 应用到 Connector 命名空间:
Complete destination AccessPolicy YAML
apiVersion: connectors.alauda.io/v1alpha1
kind: AccessPolicy
metadata:
name: application-cicd-production
namespace: <connector-namespace>
labels:
app.kubernetes.io/part-of: application-cicd-demo
spec:
connector:
names:
- <production-oci-connector>
- <production-kubernetes-connector>
checkGrantedPermission:
spec:
checks:
- name: manual-approval
ref:
configMap:
name: connectors-approvals-in-pipeline
roleTemplate:
ref:
configMap:
name: connectors-use-connectors-proxy-in-podkubectl apply -f production-access-policy.yamlApprovalTask 会暂停执行并记录决策。AccessPolicy
控制 Connector 代理权限;仅依靠任务排序并不能构成凭证授权策略。验证已安装的 Connector 是否对当前 PipelineRun 使用批准契约。
固定的 Run Template 字段不是安全边界。能够创建任意 PipelineRun 的用户可能会选择其他 service account 或更改图。这些示例共享一个练习 Pipeline 命名空间,以便学习。在使用真实生产凭证之前,请在受保护的命名空间中隔离交付,并使用平台的授权控制限制 Pipeline、模板、批准、身份和策略的更改。不要向普通 CI 作者授予可被其自由重复使用的版本身份。
#2. 创建并验证初始原生应用
使用教程 2 中前端和后端的 staging
摘要填充以下工作负载清单。两个值都包含 sha256:。设置运行时
命名空间、registry pull Secret、Ingress 类、主机名和 TLS Secret。
Complete application workload YAML
apiVersion: v1
kind: ConfigMap
metadata:
name: application-cicd-settings
namespace: <runtime-namespace>
labels:
app.kubernetes.io/part-of: application-cicd-demo
data:
APP_MESSAGE: Hello from the Go backend (from ConfigMap)
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: application-cicd-backend
namespace: <runtime-namespace>
labels:
app.kubernetes.io/part-of: application-cicd-demo
spec:
replicas: 2
selector:
matchLabels:
app.kubernetes.io/name: application-cicd-backend
template:
metadata:
labels:
app.kubernetes.io/name: application-cicd-backend
spec:
automountServiceAccountToken: false
imagePullSecrets:
- name: <runtime-image-pull-secret>
containers:
- name: backend
image: <staging-registry>/<team>/backend@<backend-initial-digest>
ports:
- name: http
containerPort: 8080
envFrom:
- configMapRef:
name: application-cicd-settings
resources:
requests:
cpu: 100m
memory: 64Mi
limits:
cpu: 500m
memory: 128Mi
readinessProbe:
httpGet:
path: /readyz
port: http
livenessProbe:
httpGet:
path: /healthz
port: http
securityContext:
runAsNonRoot: true
runAsUser: 65532
readOnlyRootFilesystem: true
allowPrivilegeEscalation: false
capabilities:
drop: [ALL]
seccompProfile:
type: RuntimeDefault
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: application-cicd-frontend
namespace: <runtime-namespace>
labels:
app.kubernetes.io/part-of: application-cicd-demo
spec:
replicas: 2
selector:
matchLabels:
app.kubernetes.io/name: application-cicd-frontend
template:
metadata:
labels:
app.kubernetes.io/name: application-cicd-frontend
spec:
automountServiceAccountToken: false
imagePullSecrets:
- name: <runtime-image-pull-secret>
containers:
- name: frontend
image: <staging-registry>/<team>/frontend@<frontend-initial-digest>
ports:
- name: http
containerPort: 8080
resources:
requests:
cpu: 100m
memory: 64Mi
limits:
cpu: 500m
memory: 128Mi
readinessProbe:
httpGet:
path: /readyz
port: http
livenessProbe:
httpGet:
path: /healthz
port: http
securityContext:
runAsNonRoot: true
runAsUser: 65532
readOnlyRootFilesystem: true
allowPrivilegeEscalation: false
capabilities:
drop: [ALL]
seccompProfile:
type: RuntimeDefault
---
apiVersion: v1
kind: Service
metadata:
name: application-cicd-backend
namespace: <runtime-namespace>
labels:
app.kubernetes.io/part-of: application-cicd-demo
spec:
selector:
app.kubernetes.io/name: application-cicd-backend
ports:
- name: http
port: 8080
targetPort: http
---
apiVersion: v1
kind: Service
metadata:
name: application-cicd-frontend
namespace: <runtime-namespace>
labels:
app.kubernetes.io/part-of: application-cicd-demo
spec:
selector:
app.kubernetes.io/name: application-cicd-frontend
ports:
- name: http
port: 8080
targetPort: http
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: application-cicd
namespace: <runtime-namespace>
labels:
app.kubernetes.io/part-of: application-cicd-demo
spec:
ingressClassName: <ingress-class>
tls:
- hosts: [<application-host>]
secretName: <application-tls-secret>
rules:
- host: <application-host>
http:
paths:
- path: /api
pathType: Prefix
backend:
service:
name: application-cicd-backend
port:
name: http
- path: /
pathType: Prefix
backend:
service:
name: application-cicd-frontend
port:
name: http使用运行时集群上下文应用它,该上下文可能与 Pipeline 集群不同。在单集群练习中,使用相同的上下文:
kubectl --context <runtime-context> apply -f workloads.yaml
kubectl --context <runtime-context> -n <runtime-namespace> rollout status deployment/application-cicd-backend --timeout=5m
kubectl --context <runtime-context> -n <runtime-namespace> rollout status deployment/application-cicd-frontend --timeout=5m
curl --fail https://<application-host>/api/messageAPI 应返回 {"message":"Hello from the Go backend (from ConfigMap)"}。在浏览器中打开
https://<application-host>/,确认页面显示该消息。如果端点使用内部 CA,请使用
已批准的 CA 信任存储;不要禁用 TLS 验证。
验证:两个 Deployment 均可用,两个 Service 均具有就绪端点,
并且浏览器可通过 /api 访问后端。在添加 CD 之前,修复应用、registry
和 Ingress 访问问题。交付 Task 会更新现有的
Deployment;不会创建应用的 Service 或 Ingress。
#3. 创建组件 CD Pipeline 和 Run Template
将下方选项卡中的匹配 Pipeline 和 Run Template 粘贴到 YAML 编辑器中。为组件资源分别创建独立资源,以免前端版本意外选择后端工作负载。
validate-digest -> wait-for-approval -> promote-image -> deploy| Task | 应观察到的现象 |
|---|---|
validate-digest | 在请求批准前拒绝格式错误的源摘要或 release tag。 |
wait-for-approval | 暂停以等待配置的批准者,并显示源镜像、目标命名空间和工作负载。 |
promote-image | 按摘要将源镜像复制到一个目标,并记录其实际目标摘要。 |
deploy | 按目标摘要更新选定的容器,等待其就绪,并在发布失败时尝试回滚。 |
Complete backend CD Pipeline YAML
apiVersion: tekton.dev/v1
kind: Pipeline
metadata:
name: application-cicd-backend-cd
namespace: <pipeline-namespace>
annotations:
integrations.tekton.dev/integrations: |
- name: source-image
interface:
apiVersion: connectors.alauda.io/v1alpha1
category: OCIArtifact
connectorRef:
setAtRuntime: true
apiVersion: connectors.alauda.io/v1alpha1
workspaces:
- name: registry-config
workspace: source-registry-config
refPath:
- tasks.promote-image.workspaces.src-registry-config
- name: destination-image
interface:
apiVersion: connectors.alauda.io/v1alpha1
category: OCIArtifact
connectorRef:
setAtRuntime: true
apiVersion: connectors.alauda.io/v1alpha1
workspaces:
- name: registry-config
workspace: destination-registry-config
refPath:
- tasks.promote-image.workspaces.dst-registry-config
- name: deployment
interface:
apiVersion: connectors.alauda.io/v1alpha1
category: Kubernetes
connectorRef:
setAtRuntime: true
apiVersion: connectors.alauda.io/v1alpha1
workspaces:
- name: kubeconfig
workspace: kubeconfig
refPath:
- tasks.deploy.workspaces.kubeconfig
integrations.tekton.dev/set-at-runtime: "true"
labels:
app.kubernetes.io/part-of: application-cicd-demo
spec:
params:
- name: source-image
type: string
description: Image in the protected source repository, qualified by an immutable
sha256 digest.
- name: target-repository
type: string
description: Protected destination repository without a tag or digest; fixed by
the Run Template.
- name: target-namespace
type: string
description: Protected workload namespace fixed by the Run Template.
- name: workload-name
type: string
description: Deployment or StatefulSet to update.
- name: container-name
type: string
description: Container in the workload to replace.
- name: release-label
type: string
default: manual
description: Audit label for the release.
workspaces:
- name: source-registry-config
- name: destination-registry-config
- name: kubeconfig
tasks:
- name: validate-digest
taskRef:
resolver: hub
params:
- name: catalog
value: catalog
- name: type
value: artifact
- name: kind
value: task
- name: name
value: run-script
- name: version
value: "0.1"
params:
- name: image
value: <internal-script-image>
- name: script
value: |
#!/bin/sh
set -eu
# Reject malformed references before requesting approval.
case "$1$2" in *[[:space:]]*) exit 1 ;; esac
printf "%s\n" "$1" | grep -Eq "^[^@[:space:]]+@sha256:[0-9a-f]{64}$"
printf "%s\n" "$2" | grep -Eq "^[A-Za-z0-9_][A-Za-z0-9_.-]{0,127}$"
- name: args
value:
- $(params.source-image)
- $(params.release-label)
- name: wait-for-approval
runAfter:
- validate-digest
taskRef:
apiVersion: openshift-pipelines.org/v1alpha1
kind: ApprovalTask
timeout: 24h
params:
- name: approvers
value:
- <release-manager>
- name: numberOfApprovalsRequired
value: "1"
- name: description
value: Approve $(params.source-image) for
$(params.target-namespace)/$(params.workload-name), release
$(params.release-label).
- name: promote-image
runAfter:
- wait-for-approval
taskRef:
resolver: hub
params:
- name: catalog
value: catalog
- name: type
value: artifact
- name: kind
value: task
- name: name
value: skopeo-copy
- name: version
value: "0.1"
params:
- name: srcImage
value: $(params.source-image)
- name: dstImages
value:
- $(params.target-repository):$(params.release-label)
- name: srcTLSVerify
value: "true"
- name: dstTLSVerify
value: "true"
- name: continueOnError
value: "false"
- name: skopeoImage
value: <internal-skopeo-image>
workspaces:
- name: src-registry-config
workspace: source-registry-config
- name: dst-registry-config
workspace: destination-registry-config
- name: deploy
runAfter:
- promote-image
taskRef:
resolver: hub
params:
- name: catalog
value: catalog
- name: type
value: artifact
- name: kind
value: task
- name: name
value: workload-apply
- name: version
value: "0.1"
params:
- name: workloadKind
value: deployment
- name: namespace
value: $(params.target-namespace)
- name: workloadName
value: $(params.workload-name)
- name: workloadContainers
value:
- $(params.container-name)=$(params.target-repository)@$(tasks.promote-image.results.dst-IMAGE_DIGEST)
- name: rolloutTimeout
value: 10m
- name: rollback
value: "true"
- name: kubectlImage
value: <internal-kubectl-image>
workspaces:
- name: kubeconfig
workspace: kubeconfig
results:
- name: deployed-image
value: $(tasks.promote-image.results.IMAGES)
- name: rollout-status
value: $(tasks.deploy.results.rollout-status)Complete backend CD Run Template YAML
apiVersion: triggers.tekton.dev/v1beta1
kind: TriggerTemplate
metadata:
name: application-cicd-backend-promotion
namespace: <pipeline-namespace>
labels:
triggertemplate.triggers.tekton.dev/usage: runtime-config
tekton.dev/pipeline: application-cicd-backend-cd
app.kubernetes.io/part-of: application-cicd-demo
spec:
params:
- name: artifact-digest
- name: release-label
default: event
resourcetemplates:
- apiVersion: tekton.dev/v1
kind: PipelineRun
metadata:
generateName: application-cicd-backend-promotion-
namespace: <pipeline-namespace>
annotations:
integrations.tekton.dev/integrations: |
- name: source-image
interface:
apiVersion: connectors.alauda.io/v1alpha1
category: OCIArtifact
connectorRef:
setAtRuntime: false
name: <staging-oci-connector>
namespace: <connector-namespace>
apiVersion: connectors.alauda.io/v1alpha1
workspaces:
- name: registry-config
workspace: source-registry-config
refPath:
- tasks.promote-image.workspaces.src-registry-config
- pipelineRuns.0.workspaces.source-registry-config
- name: destination-image
interface:
apiVersion: connectors.alauda.io/v1alpha1
category: OCIArtifact
connectorRef:
setAtRuntime: false
name: <production-oci-connector>
namespace: <connector-namespace>
apiVersion: connectors.alauda.io/v1alpha1
workspaces:
- name: registry-config
workspace: destination-registry-config
refPath:
- tasks.promote-image.workspaces.dst-registry-config
- pipelineRuns.0.workspaces.destination-registry-config
- name: deployment
interface:
apiVersion: connectors.alauda.io/v1alpha1
category: Kubernetes
connectorRef:
setAtRuntime: false
name: <production-kubernetes-connector>
namespace: <connector-namespace>
apiVersion: connectors.alauda.io/v1alpha1
workspaces:
- name: kubeconfig
workspace: kubeconfig
refPath:
- tasks.deploy.workspaces.kubeconfig
- pipelineRuns.0.workspaces.kubeconfig
integrations.tekton.dev/set-at-runtime: "true"
style.tekton.dev/descriptors: |
- path: params.source-image
x-descriptors: []
- path: params.target-repository
x-descriptors:
- urn:alm:descriptor:com.tectonic.ui:hidden
- path: params.target-namespace
x-descriptors:
- urn:alm:descriptor:com.tectonic.ui:hidden
- path: params.workload-name
x-descriptors:
- urn:alm:descriptor:com.tectonic.ui:hidden
- path: params.container-name
x-descriptors:
- urn:alm:descriptor:com.tectonic.ui:hidden
- path: params.release-label
x-descriptors: []
- path: workspaces.source-registry-config
x-descriptors:
- urn:alm:descriptor:com.tectonic.ui:hidden
- path: workspaces.destination-registry-config
x-descriptors:
- urn:alm:descriptor:com.tectonic.ui:hidden
- path: workspaces.kubeconfig
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-backend-cd
params:
- name: source-image
value: <staging-registry>/<team>/backend@$(tt.params.artifact-digest)
- name: target-repository
value: <production-registry>/<team>/backend
- name: target-namespace
value: <runtime-namespace>
- name: workload-name
value: application-cicd-backend
- name: container-name
value: backend
- name: release-label
value: $(tt.params.release-label)
workspaces:
- name: source-registry-config
csi:
driver: connectors-csi
readOnly: true
volumeAttributes:
connector.name: <staging-oci-connector>
connector.namespace: <connector-namespace>
configuration.names: registry-config
- name: destination-registry-config
csi:
driver: connectors-csi
readOnly: true
volumeAttributes:
connector.name: <production-oci-connector>
connector.namespace: <connector-namespace>
configuration.names: registry-config
- name: kubeconfig
csi:
driver: connectors-csi
readOnly: true
volumeAttributes:
connector.name: <production-kubernetes-connector>
connector.namespace: <connector-namespace>
configuration.names: kubeconfig
taskRunTemplate:
serviceAccountName: <delivery-service-account>
timeouts:
pipeline: 25h
tasks: 25hComplete frontend CD Pipeline YAML
apiVersion: tekton.dev/v1
kind: Pipeline
metadata:
name: application-cicd-frontend-cd
namespace: <pipeline-namespace>
annotations:
integrations.tekton.dev/integrations: |
- name: source-image
interface:
apiVersion: connectors.alauda.io/v1alpha1
category: OCIArtifact
connectorRef:
setAtRuntime: true
apiVersion: connectors.alauda.io/v1alpha1
workspaces:
- name: registry-config
workspace: source-registry-config
refPath:
- tasks.promote-image.workspaces.src-registry-config
- name: destination-image
interface:
apiVersion: connectors.alauda.io/v1alpha1
category: OCIArtifact
connectorRef:
setAtRuntime: true
apiVersion: connectors.alauda.io/v1alpha1
workspaces:
- name: registry-config
workspace: destination-registry-config
refPath:
- tasks.promote-image.workspaces.dst-registry-config
- name: deployment
interface:
apiVersion: connectors.alauda.io/v1alpha1
category: Kubernetes
connectorRef:
setAtRuntime: true
apiVersion: connectors.alauda.io/v1alpha1
workspaces:
- name: kubeconfig
workspace: kubeconfig
refPath:
- tasks.deploy.workspaces.kubeconfig
integrations.tekton.dev/set-at-runtime: "true"
labels:
app.kubernetes.io/part-of: application-cicd-demo
spec:
params:
- name: source-image
type: string
description: Image in the protected source repository, qualified by an immutable
sha256 digest.
- name: target-repository
type: string
description: Protected destination repository without a tag or digest; fixed by
the Run Template.
- name: target-namespace
type: string
description: Protected workload namespace fixed by the Run Template.
- name: workload-name
type: string
description: Deployment or StatefulSet to update.
- name: container-name
type: string
description: Container in the workload to replace.
- name: release-label
type: string
default: manual
description: Audit label for the release.
workspaces:
- name: source-registry-config
- name: destination-registry-config
- name: kubeconfig
tasks:
- name: validate-digest
taskRef:
resolver: hub
params:
- name: catalog
value: catalog
- name: type
value: artifact
- name: kind
value: task
- name: name
value: run-script
- name: version
value: "0.1"
params:
- name: image
value: <internal-script-image>
- name: script
value: |
#!/bin/sh
set -eu
# Reject malformed references before requesting approval.
case "$1$2" in *[[:space:]]*) exit 1 ;; esac
printf "%s\n" "$1" | grep -Eq "^[^@[:space:]]+@sha256:[0-9a-f]{64}$"
printf "%s\n" "$2" | grep -Eq "^[A-Za-z0-9_][A-Za-z0-9_.-]{0,127}$"
- name: args
value:
- $(params.source-image)
- $(params.release-label)
- name: wait-for-approval
runAfter:
- validate-digest
taskRef:
apiVersion: openshift-pipelines.org/v1alpha1
kind: ApprovalTask
timeout: 24h
params:
- name: approvers
value:
- <release-manager>
- name: numberOfApprovalsRequired
value: "1"
- name: description
value: Approve $(params.source-image) for
$(params.target-namespace)/$(params.workload-name), release
$(params.release-label).
- name: promote-image
runAfter:
- wait-for-approval
taskRef:
resolver: hub
params:
- name: catalog
value: catalog
- name: type
value: artifact
- name: kind
value: task
- name: name
value: skopeo-copy
- name: version
value: "0.1"
params:
- name: srcImage
value: $(params.source-image)
- name: dstImages
value:
- $(params.target-repository):$(params.release-label)
- name: srcTLSVerify
value: "true"
- name: dstTLSVerify
value: "true"
- name: continueOnError
value: "false"
- name: skopeoImage
value: <internal-skopeo-image>
workspaces:
- name: src-registry-config
workspace: source-registry-config
- name: dst-registry-config
workspace: destination-registry-config
- name: deploy
runAfter:
- promote-image
taskRef:
resolver: hub
params:
- name: catalog
value: catalog
- name: type
value: artifact
- name: kind
value: task
- name: name
value: workload-apply
- name: version
value: "0.1"
params:
- name: workloadKind
value: deployment
- name: namespace
value: $(params.target-namespace)
- name: workloadName
value: $(params.workload-name)
- name: workloadContainers
value:
- $(params.container-name)=$(params.target-repository)@$(tasks.promote-image.results.dst-IMAGE_DIGEST)
- name: rolloutTimeout
value: 10m
- name: rollback
value: "true"
- name: kubectlImage
value: <internal-kubectl-image>
workspaces:
- name: kubeconfig
workspace: kubeconfig
results:
- name: deployed-image
value: $(tasks.promote-image.results.IMAGES)
- name: rollout-status
value: $(tasks.deploy.results.rollout-status)Complete frontend CD Run Template YAML
apiVersion: triggers.tekton.dev/v1beta1
kind: TriggerTemplate
metadata:
name: application-cicd-frontend-promotion
namespace: <pipeline-namespace>
labels:
triggertemplate.triggers.tekton.dev/usage: runtime-config
tekton.dev/pipeline: application-cicd-frontend-cd
app.kubernetes.io/part-of: application-cicd-demo
spec:
params:
- name: artifact-digest
- name: release-label
default: event
resourcetemplates:
- apiVersion: tekton.dev/v1
kind: PipelineRun
metadata:
generateName: application-cicd-frontend-promotion-
namespace: <pipeline-namespace>
annotations:
integrations.tekton.dev/integrations: |
- name: source-image
interface:
apiVersion: connectors.alauda.io/v1alpha1
category: OCIArtifact
connectorRef:
setAtRuntime: false
name: <staging-oci-connector>
namespace: <connector-namespace>
apiVersion: connectors.alauda.io/v1alpha1
workspaces:
- name: registry-config
workspace: source-registry-config
refPath:
- tasks.promote-image.workspaces.src-registry-config
- pipelineRuns.0.workspaces.source-registry-config
- name: destination-image
interface:
apiVersion: connectors.alauda.io/v1alpha1
category: OCIArtifact
connectorRef:
setAtRuntime: false
name: <production-oci-connector>
namespace: <connector-namespace>
apiVersion: connectors.alauda.io/v1alpha1
workspaces:
- name: registry-config
workspace: destination-registry-config
refPath:
- tasks.promote-image.workspaces.dst-registry-config
- pipelineRuns.0.workspaces.destination-registry-config
- name: deployment
interface:
apiVersion: connectors.alauda.io/v1alpha1
category: Kubernetes
connectorRef:
setAtRuntime: false
name: <production-kubernetes-connector>
namespace: <connector-namespace>
apiVersion: connectors.alauda.io/v1alpha1
workspaces:
- name: kubeconfig
workspace: kubeconfig
refPath:
- tasks.deploy.workspaces.kubeconfig
- pipelineRuns.0.workspaces.kubeconfig
integrations.tekton.dev/set-at-runtime: "true"
style.tekton.dev/descriptors: |
- path: params.source-image
x-descriptors: []
- path: params.target-repository
x-descriptors:
- urn:alm:descriptor:com.tectonic.ui:hidden
- path: params.target-namespace
x-descriptors:
- urn:alm:descriptor:com.tectonic.ui:hidden
- path: params.workload-name
x-descriptors:
- urn:alm:descriptor:com.tectonic.ui:hidden
- path: params.container-name
x-descriptors:
- urn:alm:descriptor:com.tectonic.ui:hidden
- path: params.release-label
x-descriptors: []
- path: workspaces.source-registry-config
x-descriptors:
- urn:alm:descriptor:com.tectonic.ui:hidden
- path: workspaces.destination-registry-config
x-descriptors:
- urn:alm:descriptor:com.tectonic.ui:hidden
- path: workspaces.kubeconfig
x-descriptors:
- urn:alm:descriptor:com.tectonic.ui:hidden
labels:
app.kubernetes.io/part-of: application-cicd-demo
app.kubernetes.io/component: frontend
spec:
pipelineRef:
name: application-cicd-frontend-cd
params:
- name: source-image
value: <staging-registry>/<team>/frontend@$(tt.params.artifact-digest)
- name: target-repository
value: <production-registry>/<team>/frontend
- name: target-namespace
value: <runtime-namespace>
- name: workload-name
value: application-cicd-frontend
- name: container-name
value: frontend
- name: release-label
value: $(tt.params.release-label)
workspaces:
- name: source-registry-config
csi:
driver: connectors-csi
readOnly: true
volumeAttributes:
connector.name: <staging-oci-connector>
connector.namespace: <connector-namespace>
configuration.names: registry-config
- name: destination-registry-config
csi:
driver: connectors-csi
readOnly: true
volumeAttributes:
connector.name: <production-oci-connector>
connector.namespace: <connector-namespace>
configuration.names: registry-config
- name: kubeconfig
csi:
driver: connectors-csi
readOnly: true
volumeAttributes:
connector.name: <production-kubernetes-connector>
connector.namespace: <connector-namespace>
configuration.names: kubeconfig
taskRunTemplate:
serviceAccountName: <delivery-service-account>
timeouts:
pipeline: 25h
tasks: 25hRun Template 固定目标 repository、工作负载、容器、
命名空间和 Connector 绑定。对于手动执行,仅在运行时设置 source-image
和 release-label。将整个 source-image 值
替换为组件 staging
repository 中包含完整摘要限定引用的值,其中包括 repository 和 @sha256:...;不要只编辑
摘要部分,也不要将事件占位符留空。artifact-digest
参数没有手动默认值,仅由 artifact Trigger 填充。
对于事件执行,模板改为使用固定的 staging repository 和该事件变量组合引用。事件变量不是额外的手动运行表单字段。
模板将 spec.timeouts.pipeline 和 spec.timeouts.tasks 设置为 25h,
以容纳批准 Task 的 24h 超时以及其后的工作。
平台的超时限制必须允许这些值。
#跟踪摘要完成晋级
源是 <staging-repository>@sha256:...。复制操作会创建一个供人类参考的目标
标签,但部署不会使用该可变标签:
- name: workloadContainers
value:
- $(params.container-name)=$(params.target-repository)@$(tasks.promote-image.results.dst-IMAGE_DIGEST)dst-IMAGE_DIGEST 是第一个成功目标的 skopeo-copy 结果;
此示例恰好复制到一个目标,并设置 continueOnError: "false"。目标摘要具有权威性,因为
registry 或清单转换可能导致其与源摘要不同。
在使用此示例之前,请确认已安装的 catalog 快照中存在此结果。若此结果不可用,不要悄悄将其替换为 release tag。
Pipeline 的 deployed-image 结果会转发复制 Task 的 IMAGES
结果。由于示例只有一个目标,这是一条完整的
repository@sha256:... 引用,可将其与运行中
工作负载的镜像进行比较。
#4. 运行并批准一次手动发布
从 Run Template 启动 application-cicd-backend-promotion。提供后端由成功 CI 摘要限定的 staging 镜像引用,以及一个新的发布标签,例如 release-001。一次只运行每个组件的一个发布;并发发布可能会争相更新同一个 Deployment。使用唯一标签,并在可用时采用不可变标签策略。
批准前,检查 PipelineRun 和 Approvals 视图:
- 摘要属于预期组件的成功 CI 运行。
- 运行正在
wait-for-approval处等待;晋级和部署尚未开始,Deployment 镜像未发生变化。 - Connector 权限检查尚未在批准前授予发布访问权限。
作为配置的批准者,审核摘要和目标,然后批准。等待
promote-image 和 deploy 完成。在 PipelineRun 结果中,检查
deployed-image 和 rollout-status。在运行时集群中,确认
Deployment 使用该确切的 deployed-image:
kubectl --context <runtime-context> -n <runtime-namespace> get deployment application-cicd-backend -o jsonpath='{.spec.template.spec.containers[0].image}'
curl --fail https://<application-host>/api/message同时检查前端页面。使用前端自身的摘要和 Run Template 重复此操作。每次发布期间保持另一个组件不变。
测试第二次练习发布并拒绝它。该发布必须在未复制镜像或更改 Deployment 的情况下终止。未经授权的用户不得批准它。处于待处理状态或超时的批准不属于成功发布。
#5. 启用基于制品的交付
完成手动交付和拒绝操作后,返回
tutorial 3 中的 Harbor Trigger 块并应用它。管理员在 Pipeline 命名空间中创建 application-cicd-harbor-webhook,其键为
authorization,包含准确配置的 Harbor Authorization 标头值。CEL 拦截器会将该标头与此 Secret 进行比较;在已安装的 Triggers 配置中,验证其对该 Secret 的访问权限。
将 staging Harbor webhook 配置为使用独立 artifact listener 的内部 TLS 端点和匹配的身份验证。仅对 staging 项目启用 PUSH_ARTIFACT。不要将 production registry 注册为此 listener 的发送方;晋级不得触发自身。
向一个应用仓库推送新的提交,并让其 Git 触发的 CI 发布 commit-SHA 镜像标签。该事件会创建匹配的 CD 运行,而该运行必须在批准处停止。手动标签(例如 manual-001)会被有意过滤掉。此示例每个事件接受一个镜像资源,并验证摘要和标签格式;如果你的 Harbor 版本使用不同的负载或批量事件,请明确调整它。
验证无效的 Authorization 标头、其他仓库或格式错误的摘要不会创建 CD 运行。在批准前,将接受的摘要与成功的 CI 结果进行关联;PUSH_ARTIFACT 不是扫描或签名判定结果。
#6. 验证回滚并保留审计记录
记录每次发布的组件、源提交、CI 运行、源摘要、目标摘要、 CD 运行、批准决策和发布结果。在清理练习资源之前,将这些信息保留在 平台的运行历史记录中。
要有意执行回滚,请使用同一组件的 CD Run Template,并传入一个带有摘要限定符的 先前已知良好的 staging 镜像引用、新的发布标签和新的批准。不要重新构建旧提交来重新创建镜像。在回滚期间,将原始源镜像保留在 staging 中; 本示例从该固定源仓库读取镜像。
workload-apply 还具有 rollback: "true":如果发布失败,它会尝试
恢复先前的工作负载。检查其 rollout-status、rollback-status
和日志。尝试回滚并不能证明已恢复,即使恢复成功,失败的
发布仍保持失败状态。再次检查 Deployment 就绪情况和应用端点。
#故障排除
| 症状 | 首要检查项 |
|---|---|
| 未显示批准 | Approval operator 就绪情况、CustomRun 状态以及 Task 的 approver 参数。 |
| 运行在等待时超时 | Approval Task 超时、PipelineRun 超时以及平台最大超时时间。 |
| Connector 访问被拒绝 | Connector 命名空间、选定的 AccessPolicy、批准检查以及 AccessRequest 状态。 |
| 复制失败 | 源摘要可用性、源/目标 Connector 权限、CA 信任以及 registry 配额。 |
| 发布失败 | 现有工作负载名称、容器名称、镜像拉取 Secret、节点 registry 访问权限以及探针结果。 |
| 页面加载但后端不可用 | 后端 Service 端点、Ingress /api 路由以及后端日志。 |
#7. 清理练习资源
首先禁用 GitLab 和 Harbor Webhook。停止或完成活动运行,并保留所需的任何审计证据。所有示例都使用
app.kubernetes.io/part-of=application-cicd-demo 标签,因此无需依赖本地 YAML 文件即可清理本练习创建的资源。在安装了 Tekton 和 Connectors 的上下文中运行
Pipeline 集群命令,并针对 <runtime-context> 运行工作负载命令:
kubectl -n <pipeline-namespace> delete \
eventlistener,trigger,triggerbinding,pipelinerun,pipeline,triggertemplate,customrun \
-l app.kubernetes.io/part-of=application-cicd-demo --ignore-not-found
kubectl -n <connector-namespace> delete accesspolicy \
-l app.kubernetes.io/part-of=application-cicd-demo --ignore-not-found
kubectl --context <runtime-context> -n <runtime-namespace> delete \
configmap,deployment,service,ingress \
-l app.kubernetes.io/part-of=application-cicd-demo --ignore-not-found每次运行的 PVC 通常会遵循其 PipelineRun 所属关系。根据平台的保留策略,检查是否存在保留的卷、批准资源、监听器 Ingress 规则以及 registry 镜像。请管理员仅删除为本练习专门创建的 Secret、Connector、身份和策略。不要删除共享凭证、集群组件或包含他人资源的整个命名空间。
#检查点和后续步骤
你已构建每个组件、自动化其 CI、按摘要交付经过批准的镜像、检查应用,并练习了回滚和清理。
上一页:使用事件自动化 CI。