×

使用 Red Hat OpenShift Pipelines,您可以创建自定义的 CI/CD 解决方案来构建、测试和部署您的应用程序。

要为应用程序创建功能齐全的自助式 CI/CD 管道,请执行以下任务

  • 创建自定义任务,或安装现有的可重用任务。

  • 创建并定义应用程序的交付管道。

  • 提供附加到管道执行工作区的存储卷或文件系统,使用以下方法之一

    • 指定创建持久卷声明的卷声明模板

    • 指定持久卷声明

  • 创建PipelineRun对象以实例化并调用管道。

  • 添加触发器以捕获源存储库中的事件。

本节使用pipelines-tutorial示例来演示上述任务。该示例使用一个简单的应用程序,它包括:

先决条件

创建项目并检查您的 Pipeline 服务账户

步骤
  1. 登录到您的 OpenShift Container Platform 集群

    $ oc login -u <login> -p <password> https://openshift.example.com:6443
  2. 为示例应用程序创建一个项目。在此示例工作流程中,创建 pipelines-tutorial 项目

    $ oc new-project pipelines-tutorial

    如果您创建的项目名称不同,请确保使用您的项目名称更新示例中使用的资源 URL。

  3. 查看 pipeline 服务账户

    Red Hat OpenShift Pipelines Operator 会添加和配置一个名为 pipeline 的服务账户,该账户具有构建和推送镜像的足够权限。此服务帐户由 PipelineRun 对象使用。

    $ oc get serviceaccount pipeline

创建 Pipeline 任务

步骤
  1. 从包含一系列可重用 Pipeline 任务的 pipelines-tutorial 仓库安装 apply-manifestsupdate-deployment 任务资源

    $ oc create -f https://raw.githubusercontent.com/openshift/pipelines-tutorial/pipelines-1.17/01_pipeline/01_apply_manifest_task.yaml
    $ oc create -f https://raw.githubusercontent.com/openshift/pipelines-tutorial/pipelines-1.17/01_pipeline/02_update_deployment_task.yaml
  2. 使用 tkn task list 命令列出您创建的任务

    $ tkn task list

    输出结果验证已创建 apply-manifestsupdate-deployment 任务资源

    NAME                DESCRIPTION   AGE
    apply-manifests                   1 minute ago
    update-deployment                 48 seconds ago

组装 Pipeline

Pipeline 代表 CI/CD 流程,由要执行的任务定义。它旨在在多个应用程序和环境中通用且可重用。

Pipeline 使用 fromrunAfter 参数指定任务如何相互交互以及它们的执行顺序。它使用 workspaces 字段来指定 Pipeline 中每个任务在执行期间需要的一个或多个卷。

在本节中,您将创建一个 Pipeline,它从 GitHub 获取应用程序的源代码,然后在 OpenShift Container Platform 上构建和部署它。

此 Pipeline 为后端应用程序 pipelines-vote-api 和前端应用程序 pipelines-vote-ui 执行以下任务:

  • 通过引用 git-urlgit-revision 参数从 Git 仓库克隆应用程序的源代码。

  • 使用 openshift-pipelines 命名空间中提供的 buildah 任务构建容器镜像。

  • 通过引用 image 参数将镜像推送到 OpenShift 镜像注册表。

  • 通过使用 apply-manifestsupdate-deployment 任务在 OpenShift Container Platform 上部署新镜像。

步骤
  1. 复制以下示例 Pipeline YAML 文件的内容并保存。

    apiVersion: tekton.dev/v1
    kind: Pipeline
    metadata:
      name: build-and-deploy
    spec:
      workspaces:
      - name: shared-workspace
      params:
      - name: deployment-name
        type: string
        description: name of the deployment to be patched
      - name: git-url
        type: string
        description: url of the git repo for the code of deployment
      - name: git-revision
        type: string
        description: revision to be used from repo of the code for deployment
        default: "pipelines-1.17"
      - name: IMAGE
        type: string
        description: image to be built from the code
      tasks:
      - name: fetch-repository
        taskRef:
          resolver: cluster
          params:
          - name: kind
            value: task
          - name: name
            value: git-clone
          - name: namespace
            value: openshift-pipelines
        workspaces:
        - name: output
          workspace: shared-workspace
        params:
        - name: URL
          value: $(params.git-url)
        - name: SUBDIRECTORY
          value: ""
        - name: DELETE_EXISTING
          value: "true"
        - name: REVISION
          value: $(params.git-revision)
      - name: build-image
        taskRef:
          resolver: cluster
          params:
          - name: kind
            value: task
          - name: name
            value: buildah
          - name: namespace
            value: openshift-pipelines
        workspaces:
        - name: source
          workspace: shared-workspace
        params:
        - name: IMAGE
          value: $(params.IMAGE)
        runAfter:
        - fetch-repository
      - name: apply-manifests
        taskRef:
          name: apply-manifests
        workspaces:
        - name: source
          workspace: shared-workspace
        runAfter:
        - build-image
      - name: update-deployment
        taskRef:
          name: update-deployment
        params:
        - name: deployment
          value: $(params.deployment-name)
        - name: IMAGE
          value: $(params.IMAGE)
        runAfter:
        - apply-manifests

    Pipeline 定义抽象了 Git 源代码库和镜像注册表的细节。这些细节在触发和执行 Pipeline 时作为 params 添加。

  2. 创建 Pipeline

    $ oc create -f <pipeline-yaml-file-name.yaml>

    或者,您也可以直接从 Git 仓库执行 YAML 文件。

    $ oc create -f https://raw.githubusercontent.com/openshift/pipelines-tutorial/pipelines-1.17/01_pipeline/04_pipeline.yaml
  3. 使用 tkn pipeline list 命令验证 Pipeline 是否已添加到应用程序。

    $ tkn pipeline list

    输出结果验证已创建 build-and-deploy Pipeline。

    NAME               AGE            LAST RUN   STARTED   DURATION   STATUS
    build-and-deploy   1 minute ago   ---        ---       ---        ---

镜像映像以在受限环境中运行 Pipeline

要在断开连接的集群或在受限环境中配置的集群中运行 OpenShift Pipelines,请确保已针对受限网络配置 Samples Operator,或者集群管理员已创建具有镜像注册表的集群。

以下步骤使用 pipelines-tutorial 示例在具有镜像注册表的受限环境中为应用程序创建 Pipeline。为确保 pipelines-tutorial 示例在受限环境中有效,您必须从镜像注册表镜像前端界面 pipelines-vote-ui、后端界面 pipelines-vote-apicli 的相应构建器镜像。

步骤
  1. 从镜像注册表镜像前端界面 pipelines-vote-ui 的构建器镜像。

    1. 验证所需的镜像标签未导入

      $ oc describe imagestream python -n openshift
      示例输出
      Name:			python
      Namespace:		openshift
      [...]
      
      3.8-ubi9 (latest)
        tagged from registry.redhat.io/ubi9/python-38:latest
          prefer registry pullthrough when referencing this tag
      
        Build and run Python 3.8 applications on UBI 8. For more information about using this builder image, including OpenShift considerations, see https://github.com/sclorg/s2i-python-container/blob/master/3.8/README.md.
        Tags: builder, python
        Supports: python:3.8, python
        Example Repo: https://github.com/sclorg/django-ex.git
      
      [...]
    2. 将支持的镜像标签镜像到私有注册表

      $ oc image mirror registry.redhat.io/ubi9/python-39:latest <mirror-registry>:<port>/ubi9/python-39
    3. 导入镜像

      $ oc tag <mirror-registry>:<port>/ubi9/python-39 python:latest --scheduled -n openshift

      您必须定期重新导入镜像。--scheduled 标志启用镜像的自动重新导入。

    4. 验证具有给定标签的镜像是否已导入。

      $ oc describe imagestream python -n openshift
      示例输出
      Name:			python
      Namespace:		openshift
      [...]
      
      latest
        updates automatically from registry  <mirror-registry>:<port>/ubi9/python-39
      
        *  <mirror-registry>:<port>/ubi9/python-39@sha256:3ee...
      
      [...]
  2. 从镜像注册表镜像后端界面 pipelines-vote-api 的构建器镜像。

    1. 验证所需的镜像标签未导入

      $ oc describe imagestream golang -n openshift
      示例输出
      Name:			golang
      Namespace:		openshift
      [...]
      
      1.14.7-ubi8 (latest)
        tagged from registry.redhat.io/ubi8/go-toolset:1.14.7
          prefer registry pullthrough when referencing this tag
      
        Build and run Go applications on UBI 8. For more information about using this builder image, including OpenShift considerations, see https://github.com/sclorg/golang-container/blob/master/README.md.
        Tags: builder, golang, go
        Supports: golang
        Example Repo: https://github.com/sclorg/golang-ex.git
      
      [...]
    2. 将支持的镜像标签镜像到私有注册表

      $ oc image mirror registry.redhat.io/ubi9/go-toolset:latest <mirror-registry>:<port>/ubi9/go-toolset
    3. 导入镜像

      $ oc tag <mirror-registry>:<port>/ubi9/go-toolset golang:latest --scheduled -n openshift

      您必须定期重新导入镜像。--scheduled 标志启用镜像的自动重新导入。

    4. 验证具有给定标签的镜像是否已导入。

      $ oc describe imagestream golang -n openshift
      示例输出
      Name:			golang
      Namespace:		openshift
      [...]
      
      latest
        updates automatically from registry <mirror-registry>:<port>/ubi9/go-toolset
      
        * <mirror-registry>:<port>/ubi9/go-toolset@sha256:59a74d581df3a2bd63ab55f7ac106677694bf612a1fe9e7e3e1487f55c421b37
      
      [...]
  3. 从镜像注册表镜像 cli 的构建器镜像。

    1. 验证所需的镜像标签未导入

      $ oc describe imagestream cli -n openshift
      示例输出
      Name:                   cli
      Namespace:              openshift
      [...]
      
      latest
        updates automatically from registry quay.io/openshift-release-dev/ocp-v4.0-art-dev@sha256:65c68e8c22487375c4c6ce6f18ed5485915f2bf612e41fef6d41cbfcdb143551
      
        * quay.io/openshift-release-dev/ocp-v4.0-art-dev@sha256:65c68e8c22487375c4c6ce6f18ed5485915f2bf612e41fef6d41cbfcdb143551
      
      [...]
    2. 将支持的镜像标签镜像到私有注册表

      $ oc image mirror quay.io/openshift-release-dev/ocp-v4.0-art-dev@sha256:65c68e8c22487375c4c6ce6f18ed5485915f2bf612e41fef6d41cbfcdb143551 <mirror-registry>:<port>/openshift-release-dev/ocp-v4.0-art-dev:latest
    3. 导入镜像

      $ oc tag <mirror-registry>:<port>/openshift-release-dev/ocp-v4.0-art-dev cli:latest --scheduled -n openshift

      您必须定期重新导入镜像。--scheduled 标志启用镜像的自动重新导入。

    4. 验证具有给定标签的镜像是否已导入。

      $ oc describe imagestream cli -n openshift
      示例输出
      Name:                   cli
      Namespace:              openshift
      [...]
      
      latest
        updates automatically from registry <mirror-registry>:<port>/openshift-release-dev/ocp-v4.0-art-dev
      
        * <mirror-registry>:<port>/openshift-release-dev/ocp-v4.0-art-dev@sha256:65c68e8c22487375c4c6ce6f18ed5485915f2bf612e41fef6d41cbfcdb143551
      
      [...]

运行 Pipeline

PipelineRun 资源启动 Pipeline 并将其绑定到应用于特定调用的 Git 和镜像资源。它会自动为 Pipeline 中的每个任务创建和启动 TaskRun 资源。

步骤
  1. 启动后端应用程序的 Pipeline

    $ tkn pipeline start build-and-deploy \
        -w name=shared-workspace,volumeClaimTemplateFile=https://raw.githubusercontent.com/openshift/pipelines-tutorial/pipelines-1.17/01_pipeline/03_persistent_volume_claim.yaml \
        -p deployment-name=pipelines-vote-api \
        -p git-url=https://github.com/openshift/pipelines-vote-api.git \
        -p IMAGE='image-registry.openshift-image-registry.svc:5000/pipelines-tutorial/pipelines-vote-api' \
        --use-param-defaults

    之前的命令使用卷声明模板,该模板为 Pipeline 执行创建持久卷声明。

  2. 要跟踪 Pipeline 运行的进度,请输入以下命令:

    $ tkn pipelinerun logs <pipelinerun_id> -f

    上述命令中的 <pipelinerun_id> 是在先前命令的输出中返回的 PipelineRun 的 ID。

  3. 启动前端应用程序的 Pipeline

    $ tkn pipeline start build-and-deploy \
        -w name=shared-workspace,volumeClaimTemplateFile=https://raw.githubusercontent.com/openshift/pipelines-tutorial/pipelines-1.17/01_pipeline/03_persistent_volume_claim.yaml \
        -p deployment-name=pipelines-vote-ui \
        -p git-url=https://github.com/openshift/pipelines-vote-ui.git \
        -p IMAGE='image-registry.openshift-image-registry.svc:5000/pipelines-tutorial/pipelines-vote-ui' \
        --use-param-defaults
  4. 要跟踪 Pipeline 运行的进度,请输入以下命令

    $ tkn pipelinerun logs <pipelinerun_id> -f

    上述命令中的 <pipelinerun_id> 是在先前命令的输出中返回的 PipelineRun 的 ID。

  5. 几分钟后,使用 tkn pipelinerun list 命令通过列出所有 Pipeline 运行来验证 Pipeline 是否成功运行。

    $ tkn pipelinerun list

    输出结果列出了 Pipeline 运行。

     NAME                         STARTED      DURATION     STATUS
     build-and-deploy-run-xy7rw   1 hour ago   2 minutes    Succeeded
     build-and-deploy-run-z2rz8   1 hour ago   19 minutes   Succeeded
  6. 获取应用程序路由

    $ oc get route pipelines-vote-ui --template='http://{{.spec.host}}'

    注意先前命令的输出。您可以使用此路由访问应用程序。

  7. 要使用先前 Pipeline 的 Pipeline 资源和服务帐户重新运行上次 Pipeline 运行,请运行

    $ tkn pipeline start build-and-deploy --last

向 Pipeline 添加触发器

触发器使 Pipeline 能够响应外部 GitHub 事件,例如推送事件和拉取请求。组装并启动应用程序的 Pipeline 后,添加 TriggerBindingTriggerTemplateTriggerEventListener 资源以捕获 GitHub 事件。

步骤
  1. 复制以下示例 TriggerBinding YAML 文件的内容并保存。

    apiVersion: triggers.tekton.dev/v1beta1
    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)
  2. 创建 TriggerBinding 资源

    $ oc create -f <triggerbinding-yaml-file-name.yaml>

    或者,您可以直接从 pipelines-tutorial Git 仓库创建 TriggerBinding 资源

    $ oc create -f https://raw.githubusercontent.com/openshift/pipelines-tutorial/pipelines-1.17/03_triggers/01_binding.yaml
  3. 复制以下示例 TriggerTemplate YAML 文件的内容并保存。

    apiVersion: triggers.tekton.dev/v1beta1
    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: pipelines-1.17
      - name: git-repo-name
        description: The name of the deployment to be created / patched
    
      resourcetemplates:
      - apiVersion: tekton.dev/v1
        kind: PipelineRun
        metadata:
          generateName: build-deploy-$(tt.params.git-repo-name)-
        spec:
          taskRunTemplate:
            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

    该模板指定一个卷声明模板,用于为定义工作区存储卷创建持久卷声明。因此,您无需创建持久卷声明来提供数据存储。

  4. 创建 TriggerTemplate 资源

    $ oc create -f <triggertemplate-yaml-file-name.yaml>

    或者,您可以直接从 pipelines-tutorial Git 仓库创建 TriggerTemplate 资源

    $ oc create -f https://raw.githubusercontent.com/openshift/pipelines-tutorial/pipelines-1.17/03_triggers/02_template.yaml
  5. 复制以下示例 Trigger YAML 文件的内容并保存。

    apiVersion: triggers.tekton.dev/v1beta1
    kind: Trigger
    metadata:
      name: vote-trigger
    spec:
      taskRunTemplate:
        serviceAccountName: pipeline
      bindings:
        - ref: vote-app
      template:
        ref: vote-app
  6. 创建 Trigger 资源

    $ oc create -f <trigger-yaml-file-name.yaml>

    或者,您可以直接从 pipelines-tutorial Git 仓库创建 Trigger 资源

    $ oc create -f https://raw.githubusercontent.com/openshift/pipelines-tutorial/pipelines-1.17/03_triggers/03_trigger.yaml
  7. 复制以下示例 EventListener YAML 文件的内容并保存。

    apiVersion: triggers.tekton.dev/v1beta1
    kind: EventListener
    metadata:
      name: vote-app
    spec:
      taskRunTemplate:
        serviceAccountName: pipeline
      triggers:
        - triggerRef: vote-trigger

    或者,如果您尚未定义触发器自定义资源,请将绑定和模板规范添加到 EventListener YAML 文件中,而不是引用触发器的名称。

    apiVersion: triggers.tekton.dev/v1beta1
    kind: EventListener
    metadata:
      name: vote-app
    spec:
      taskRunTemplate:
        serviceAccountName: pipeline
      triggers:
      - bindings:
        - ref: vote-app
        template:
          ref: vote-app
  8. 按照以下步骤创建EventListener 资源

    • 使用安全的 HTTPS 连接创建EventListener 资源

      1. 添加标签以启用Eventlistener资源的安全 HTTPS 连接

        $ oc label namespace <ns-name> operator.tekton.dev/enable-annotation=enabled
      2. 创建EventListener 资源

        $ oc create -f <eventlistener-yaml-file-name.yaml>

        或者,您可以直接从pipelines-tutorial Git 仓库创建EventListener 资源

        $ oc create -f https://raw.githubusercontent.com/openshift/pipelines-tutorial/pipelines-1.17/03_triggers/04_event_listener.yaml
      3. 创建一个具有重新加密 TLS 终端的路由

        $ oc create route reencrypt --service=<svc-name> --cert=tls.crt --key=tls.key --ca-cert=ca.crt --hostname=<hostname>

        或者,您可以创建一个重新加密 TLS 终端 YAML 文件来创建一个安全的路由。

        安全路由的重新加密 TLS 终端 YAML 示例
        apiVersion: route.openshift.io/v1
        kind: Route
        metadata:
          name: route-passthrough-secured (1)
        spec:
          host: <hostname>
          to:
            kind: Service
            name: frontend (1)
          tls:
            termination: reencrypt         (2)
            key: [as in edge termination]
            certificate: [as in edge termination]
            caCertificate: [as in edge termination]
            destinationCACertificate: |-   (3)
              -----BEGIN CERTIFICATE-----
              [...]
              -----END CERTIFICATE-----
        1 对象的名称,长度限制为 63 个字符。
        2 termination 字段设置为 reencrypt。这是唯一必需的 tls 字段。
        3 重新加密所需。destinationCACertificate 指定一个 CA 证书来验证端点证书,从而保护从路由器到目标 Pod 的连接安全。如果服务使用服务签名证书,或者管理员已为路由器指定默认 CA 证书,并且服务拥有由该 CA 签名的证书,则可以省略此字段。

        有关更多选项,请参见 oc create route reencrypt --help

    • 使用不安全的 HTTP 连接创建EventListener 资源

      1. 创建EventListener 资源。

      2. EventListener 服务公开为 OpenShift Container Platform 路由,使其可公开访问

        $ oc expose svc el-vote-app

配置事件监听器以服务多个命名空间

如果您只想创建一个基本的 CI/CD 管道,可以跳过此部分。但是,如果您的部署策略涉及多个命名空间,则可以配置事件监听器以服务多个命名空间。

为了提高EventListener 对象的可重用性,集群管理员可以将它们配置和部署为多租户事件监听器,以服务多个命名空间。

步骤
  1. 为事件监听器配置集群范围的获取权限。

    1. 设置要在ClusterRoleBindingEventListener 对象中使用的服务帐户名称。例如,el-sa

      ServiceAccount.yaml 示例
      apiVersion: v1
      kind: ServiceAccount
      metadata:
        name: el-sa
      ---
    2. ClusterRole.yaml 文件的rules 部分中,为每个事件监听器部署设置适当的权限,以便在集群范围内运行。

      ClusterRole.yaml 示例
      kind: ClusterRole
      apiVersion: rbac.authorization.k8s.io/v1
      metadata:
        name: el-sel-clusterrole
      rules:
      - apiGroups: ["triggers.tekton.dev"]
        resources: ["eventlisteners", "clustertriggerbindings", "clusterinterceptors", "triggerbindings", "triggertemplates", "triggers"]
        verbs: ["get", "list", "watch"]
      - apiGroups: [""]
        resources: ["configmaps", "secrets"]
        verbs: ["get", "list", "watch"]
      - apiGroups: [""]
        resources: ["serviceaccounts"]
        verbs: ["impersonate"]
      ...
    3. 使用适当的服务帐户名称和集群角色名称配置集群角色绑定。

      ClusterRoleBinding.yaml 示例
      apiVersion: rbac.authorization.k8s.io/v1
      kind: ClusterRoleBinding
      metadata:
        name: el-mul-clusterrolebinding
      subjects:
      - kind: ServiceAccount
        name: el-sa
        namespace: default
      roleRef:
        apiGroup: rbac.authorization.k8s.io
        kind: ClusterRole
        name: el-sel-clusterrole
      ...
  2. 在事件监听器的spec 参数中,添加服务帐户名称,例如el-sa。使用命名空间名称填充namespaceSelector 参数,事件监听器将在这些命名空间中提供服务。

    EventListener.yaml 示例
    apiVersion: triggers.tekton.dev/v1beta1
    kind: EventListener
    metadata:
      name: namespace-selector-listener
    spec:
      taskRunTemplate:
        serviceAccountName: el-sa
      namespaceSelector:
        matchNames:
        - default
        - foo
    ...
  3. 创建一个具有必要权限的服务帐户,例如foo-trigger-sa。将其用于绑定触发器的角色。

    ServiceAccount.yaml 示例
    apiVersion: v1
    kind: ServiceAccount
    metadata:
      name: foo-trigger-sa
      namespace: foo
    ...
    RoleBinding.yaml 示例
    apiVersion: rbac.authorization.k8s.io/v1
    kind: RoleBinding
    metadata:
      name: triggercr-rolebinding
      namespace: foo
    subjects:
    - kind: ServiceAccount
      name: foo-trigger-sa
      namespace: foo
    roleRef:
      apiGroup: rbac.authorization.k8s.io
      kind: ClusterRole
      name: tekton-triggers-eventlistener-roles
    ...
  4. 使用适当的触发器模板、触发器绑定和服务帐户名称创建一个触发器。

    Trigger.yaml 示例
    apiVersion: triggers.tekton.dev/v1beta1
    kind: Trigger
    metadata:
      name: trigger
      namespace: foo
    spec:
      taskRunTemplate:
        serviceAccountName: foo-trigger-sa
      interceptors:
        - ref:
            name: "github"
          params:
            - name: "secretRef"
              value:
                secretName: github-secret
                secretKey: secretToken
            - name: "eventTypes"
              value: ["push"]
      bindings:
        - ref: vote-app
      template:
        ref: vote-app
    ...

创建 Webhook

Webhook 是 HTTP POST 消息,每当存储库中发生配置的事件时,事件监听器都会收到这些消息。然后,事件有效负载将映射到触发器绑定,并由触发器模板处理。触发器模板最终会启动一个或多个管道运行,从而导致创建和部署 Kubernetes 资源。

在本节中,您将在分叉的 Git 存储库pipelines-vote-uipipelines-vote-api 上配置一个 Webhook URL。此 URL 指向公开可访问的EventListener 服务路由。

添加 Webhook 需要对存储库具有管理员权限。如果您没有对存储库的管理员访问权限,请联系您的系统管理员以添加 Webhook。

步骤
  1. 获取 Webhook URL

    • 对于安全的 HTTPS 连接

      $ echo "URL: $(oc  get route el-vote-app --template='https://{{.spec.host}}')"
    • 对于 HTTP(不安全)连接

      $ echo "URL: $(oc  get route el-vote-app --template='http://{{.spec.host}}')"

      记下输出中获得的 URL。

  2. 手动配置前端存储库上的 Webhook

    1. 在浏览器中打开前端 Git 存储库pipelines-vote-ui

    2. 点击设置Webhook添加 Webhook

    3. Webhook/添加 Webhook 页面上

      1. 有效负载 URL 字段中输入步骤 1 中的 Webhook URL

      2. 内容类型选择application/json

      3. 密钥字段中指定密钥

      4. 确保已选择仅推送事件

      5. 选择活动

      6. 点击添加 Webhook

  3. 对后端存储库pipelines-vote-api 重复步骤 2。

触发管道运行

每当 Git 存储库中发生push 事件时,配置的 Webhook 都会将事件有效负载发送到公开的EventListener 服务路由。应用程序的EventListener 服务处理有效负载,并将其传递给相关的TriggerBindingTriggerTemplate 资源对。TriggerBinding 资源提取参数,TriggerTemplate 资源使用这些参数并指定必须创建资源的方式。这可能会重建和重新部署应用程序。

在本节中,您将一个空提交推送到前端pipelines-vote-ui 存储库,然后触发管道运行。

步骤
  1. 从终端克隆您分叉的 Git 存储库pipelines-vote-ui

    $ git clone [email protected]:<your GitHub ID>/pipelines-vote-ui.git -b pipelines-1.17
  2. 推送一个空提交

    $ git commit -m "empty-commit" --allow-empty && git push origin pipelines-1.17
  3. 检查是否触发了管道运行

    $ tkn pipelinerun list

    注意,已启动新的管道运行。

为用户定义项目的触发器启用事件监听器的监控

作为集群管理员,要收集用户定义项目中Triggers 服务的事件监听器指标并在 OpenShift Container Platform Web 控制台中显示它们,您可以为每个事件监听器创建一个服务监控器。在收到 HTTP 请求后,Triggers 服务的事件监听器将返回三个指标——eventlistener_http_duration_secondseventlistener_event_counteventlistener_triggered_resources

先决条件
  • 您已登录到 OpenShift Container Platform Web 控制台。

  • 您已安装 Red Hat OpenShift Pipelines 运算符。

  • 您已为用户定义的项目启用监控。

步骤
  1. 对于每个事件监听器,创建一个服务监控器。例如,要查看test 命名空间中github-listener 事件监听器的指标,请创建以下服务监控器

    apiVersion: monitoring.coreos.com/v1
    kind: ServiceMonitor
    metadata:
      labels:
        app.kubernetes.io/managed-by: EventListener
        app.kubernetes.io/part-of: Triggers
        eventlistener: github-listener
      annotations:
        networkoperator.openshift.io/ignore-errors: ""
      name: el-monitor
      namespace: test
    spec:
      endpoints:
        - interval: 10s
          port: http-metrics
      jobLabel: name
      namespaceSelector:
        matchNames:
          - test
      selector:
        matchLabels:
          app.kubernetes.io/managed-by: EventListener
          app.kubernetes.io/part-of: Triggers
          eventlistener: github-listener
    ...
  2. 通过向事件监听器发送请求来测试服务监控器。例如,推送一个空提交

    $ git commit -m "empty-commit" --allow-empty && git push origin main
  3. 在 OpenShift Container Platform Web 控制台中,导航到管理员观察指标

  4. 要查看指标,请按名称搜索。例如,要查看github-listener 事件监听器的eventlistener_http_resources 指标的详细信息,请使用eventlistener_http_resources 关键字进行搜索。

配置 GitHub Interceptor 中的拉取请求功能

使用 GitHub Interceptor,您可以创建验证和过滤 GitHub Webhook 的逻辑。例如,您可以验证 Webhook 的来源并根据指定的条件过滤传入的事件。当您使用 GitHub Interceptor 过滤事件数据时,可以在字段中指定 Interceptor 可以接受的事件类型。在 Red Hat OpenShift Pipelines 中,您可以使用 GitHub Interceptor 的以下功能

  • 根据已更改的文件过滤拉取请求事件

  • 根据配置的 GitHub 所有者验证拉取请求

使用 GitHub Interceptor 过滤拉取请求

您可以根据 push 和 pull 事件中已更改的文件过滤 GitHub 事件。这有助于您仅对 Git 存储库中的相关更改执行流水线。GitHub Interceptor 会添加所有已更改文件的逗号分隔列表,并使用 CEL Interceptor 来根据已更改的文件过滤传入事件。更改的文件列表会添加到事件有效负载顶级 extensions 字段中的 changed_files 属性中。

先决条件
  • 您已安装 Red Hat OpenShift Pipelines 运算符。

步骤
  1. 执行以下步骤之一

    • 对于公共 GitHub 存储库,请在下面显示的 YAML 配置文件中将 addChangedFiles 参数的值设置为 true

      apiVersion: triggers.tekton.dev/v1beta1
      kind: EventListener
      metadata:
        name: github-add-changed-files-pr-listener
      spec:
        triggers:
          - name: github-listener
            interceptors:
              - ref:
                  name: "github"
                  kind: ClusterInterceptor
                  apiVersion: triggers.tekton.dev
                params:
                - name: "secretRef"
                  value:
                    secretName: github-secret
                    secretKey: secretToken
                - name: "eventTypes"
                  value: ["pull_request", "push"]
                - name: "addChangedFiles"
                  value:
                    enabled: true
              - ref:
                  name: cel
                params:
                - name: filter
                  value: extensions.changed_files.matches('controllers/')
      ...
    • 对于私有 GitHub 存储库,请将 addChangedFiles 参数的值设置为 true,并在下面显示的 YAML 配置文件中提供访问令牌详细信息、secretNamesecretKey

      apiVersion: triggers.tekton.dev/v1beta1
      kind: EventListener
      metadata:
        name: github-add-changed-files-pr-listener
      spec:
        triggers:
          - name: github-listener
            interceptors:
              - ref:
                  name: "github"
                  kind: ClusterInterceptor
                  apiVersion: triggers.tekton.dev
                params:
                - name: "secretRef"
                  value:
                    secretName: github-secret
                    secretKey: secretToken
                - name: "eventTypes"
                  value: ["pull_request", "push"]
                - name: "addChangedFiles"
                  value:
                    enabled: true
                    personalAccessToken:
                      secretName: github-pat
                      secretKey: token
              - ref:
                  name: cel
                params:
                - name: filter
                  value: extensions.changed_files.matches('controllers/')
      ...
  2. 保存配置文件。

使用 GitHub Interceptor 验证拉取请求

您可以使用 GitHub Interceptor 根据为存储库配置的 GitHub 所有者来验证拉取请求的处理过程。此验证可帮助您防止不必要地执行 PipelineRunTaskRun 对象。只有当用户名列为所有者或存储库所有者发布可配置的注释时,GitHub Interceptor 才会处理拉取请求。例如,当您以所有者的身份在拉取请求中评论 /ok-to-test 时,就会触发 PipelineRunTaskRun

所有者在存储库根目录下的 OWNERS 文件中配置。

先决条件
  • 您已安装 Red Hat OpenShift Pipelines 运算符。

步骤
  1. 创建一个秘密字符串值。

  2. 使用该值配置 GitHub webhook。

  3. 创建一个名为 secretRef 的 Kubernetes 密钥,其中包含您的秘密值。

  4. 将 Kubernetes 密钥作为引用传递给您的 GitHub Interceptor。

  5. 创建一个 owners 文件,并将审批者列表添加到 approvers 部分。

  6. 执行以下步骤之一

    • 对于公共 GitHub 存储库,请在下面显示的 YAML 配置文件中将 githubOwners 参数的值设置为 true

      apiVersion: triggers.tekton.dev/v1beta1
      kind: EventListener
      metadata:
        name: github-owners-listener
      spec:
        triggers:
          - name: github-listener
            interceptors:
              - ref:
                  name: "github"
                  kind: ClusterInterceptor
                  apiVersion: triggers.tekton.dev
                params:
                  - name: "secretRef"
                    value:
                      secretName: github-secret
                      secretKey: secretToken
                  - name: "eventTypes"
                    value: ["pull_request", "issue_comment"]
                  - name: "githubOwners"
                    value:
                      enabled: true
                      checkType: none
      ...
    • 对于私有 GitHub 存储库,请将 githubOwners 参数的值设置为 true,并在下面显示的 YAML 配置文件中提供访问令牌详细信息、secretNamesecretKey

      apiVersion: triggers.tekton.dev/v1beta1
      kind: EventListener
      metadata:
        name: github-owners-listener
      spec:
        triggers:
          - name: github-listener
            interceptors:
              - ref:
                  name: "github"
                  kind: ClusterInterceptor
                  apiVersion: triggers.tekton.dev
                params:
                  - name: "secretRef"
                    value:
                      secretName: github-secret
                      secretKey: secretToken
                  - name: "eventTypes"
                    value: ["pull_request", "issue_comment"]
                  - name: "githubOwners"
                    value:
                      enabled: true
                      personalAccessToken:
                        secretName: github-token
                        secretKey: secretToken
                      checkType: all
      ...

      checkType 参数用于指定需要身份验证的 GitHub 所有者。您可以将其值设置为 orgMembersrepoMembersall

  7. 保存配置文件。

其他资源