4.6. 在 Pipeline 中添加触发器
触发器(Trigger)使 Pipelines 可以响应外部 GitHub 事件,如推送事件和拉取请求。在为应用程序组装并启动管道后,添加 TriggerBinding
、TriggerTemplate
、Trigger
和 EventListener
资源来捕获 GitHub 事件。
流程
复制以下
TriggerBinding
YAML 示例文件的内容并保存:apiVersion: triggers.tekton.dev/v1alpha1 kind: TriggerBinding metadata: name: vote-app spec: params: - name: git-repo-url value: $(body.repository.url) - name: git-repo-name value: $(body.repository.name) - name: git-revision value: $(body.head_commit.id)
创建
TriggerBinding
资源:$ oc create -f <triggerbinding-yaml-file-name.yaml>
或者,您可以直接从
pipelines-tutorial
Git 仓库创建TriggerBinding
资源:$ oc create -f https://raw.githubusercontent.com/openshift/pipelines-tutorial/release-tech-preview-3/03_triggers/01_binding.yaml
复制以下
TriggerTemplate
YAML 示例文件的内容并保存:apiVersion: triggers.tekton.dev/v1alpha1 kind: TriggerTemplate metadata: name: vote-app spec: params: - name: git-repo-url description: The git repository url - name: git-revision description: The git revision default: release-tech-preview-3 - name: git-repo-name description: The name of the deployment to be created / patched resourcetemplates: - apiVersion: tekton.dev/v1beta1 kind: PipelineRun metadata: name: build-deploy-$(tt.params.git-repo-name)-$(uid) spec: serviceAccountName: pipeline pipelineRef: name: build-and-deploy params: - name: deployment-name value: $(tt.params.git-repo-name) - name: git-url value: $(tt.params.git-repo-url) - name: git-revision value: $(tt.params.git-revision) - name: IMAGE value: image-registry.openshift-image-registry.svc:5000/pipelines-tutorial/$(tt.params.git-repo-name) workspaces: - name: shared-workspace volumeClaimTemplate: spec: accessModes: - ReadWriteOnce resources: requests: storage: 500Mi
模板指定一个卷声明模板,用于创建用于为工作空间定义存储卷的持久性卷声明。因此,您不需要创建持久性卷声明来提供数据存储。
创建
TriggerTemplate
资源:$ oc create -f <triggertemplate-yaml-file-name.yaml>
另外,您还可以从
pipelines-tutorial
Git 仓库直接创建TriggerTemplate
资源:$ oc create -f https://raw.githubusercontent.com/openshift/pipelines-tutorial/release-tech-preview-3/03_triggers/02_template.yaml
复制以下
Trigger
YAML 示例文件的内容并保存:apiVersion: triggers.tekton.dev/v1alpha1 kind: Trigger metadata: name: vote-trigger spec: serviceAccountName: pipeline bindings: - ref: vote-app template: name: vote-app
创建
Trigger
资源:$ oc create -f <trigger-yaml-file-name.yaml>
另外,您还可以直接从
pipelines-tutorial
Git 仓库创建Trigger
资源:$ oc create -f https://github.com/openshift/pipelines-tutorial/blob/release-tech-preview-3/03_triggers/03_trigger.yaml
复制以下
EventListener
YAML 示例文件的内容并保存:apiVersion: triggers.tekton.dev/v1alpha1 kind: EventListener metadata: name: vote-app spec: serviceAccountName: pipeline triggers: - triggerRef: vote-trigger
或者,如果您还没有定义触发器自定义资源,将绑定和模板规格添加到
EventListener
YAML 文件中,而不是引用触发器的名称:apiVersion: triggers.tekton.dev/v1alpha1 kind: EventListener metadata: name: vote-app spec: serviceAccountName: pipeline triggers: - bindings: - ref: vote-app template: name: vote-app
创建
EventListener
资源:$ oc create -f <eventlistener-yaml-file-name.yaml>
或者,您可以直接从
pipelines-tutorial
Git 仓库创建EvenListener
资源:$ oc create -f https://raw.githubusercontent.com/openshift/pipelines-tutorial/release-tech-preview-3/03_triggers/04_event_listener.yaml
将
EventListener
服务公开为 OpenShift Container Platform 路由,使其可以被公开访问:$ oc expose svc el-vote-app