×

使用 GitHub App 的流水线即代码

GitHub Apps 充当与 Red Hat OpenShift Pipelines 集成的点,并将基于 Git 的工作流程的优势引入 OpenShift Pipelines。集群管理员可以为所有集群用户配置单个 GitHub App。要使 GitHub Apps 与流水线即代码一起工作,请确保 GitHub App 的 Webhook 指向监听 GitHub 事件的流水线即代码控制器路由(或入口端点)。

有三种方法可以为流水线即代码设置 GitHub 应用

  • 使用tkn命令行实用程序。

  • 使用 Web 控制台的管理员视角。

  • 在 GitHub 中手动设置应用程序,然后为流水线即代码创建密钥。

默认情况下,流水线即代码可以与一个 GitHub 应用通信。如果您配置了其他流水线即代码控制器来与其他 GitHub 应用通信,请分别配置每个 GitHub 应用。您必须手动为任何其他控制器设置 GitHub 应用。

使用命令行界面配置 GitHub App

您可以使用tkn命令行实用程序创建 GitHub 应用并为 GitHub 应用配置流水线即代码控制器。

如果您创建了其他流水线即代码控制器来支持其他 GitHub 应用,则只能对主控制器使用此过程。要为其他控制器创建 GitHub 应用,请使用手动过程。

先决条件
  • 您已以集群管理员身份登录 OpenShift Container Platform 集群。

  • 您已使用 `tkn pac` 插件安装了 `tkn` 命令行实用程序。

步骤
  • 输入以下命令

    $ tkn pac bootstrap github-app

    此命令假设您的帐户使用标准 github.com API 端点。如果您使用不同的 GitHub API 端点(例如,如果您使用 GitHub Enterprise),请使用 `--github-api-url` 选项指定端点,如下例所示

    示例命令
    $ tkn pac bootstrap github-app --github-api-url https://github.com/enterprises/example-enterprise

以管理员身份创建 GitHub 应用

作为集群管理员,您可以将您的 GitHub 应用与 OpenShift Container Platform 集群配置为使用 Pipelines as Code。此配置允许您执行构建部署所需的一组任务。

如果您创建了其他流水线即代码控制器来支持其他 GitHub 应用,则只能对主控制器使用此过程。要为其他控制器创建 GitHub 应用,请使用手动过程。

先决条件

您已从 Operator Hub 安装了 Red Hat OpenShift Pipelines `pipelines-1.17` 运算符。

步骤
  1. 在管理员视角中,使用导航窗格导航到**Pipelines**。

  2. 单击**Pipelines**页面上的**设置 GitHub 应用**。

  3. 输入您的 GitHub 应用名称。例如,`pipelines-ci-clustername-testui`。

  4. 单击**设置**。

  5. 在浏览器中提示时输入您的 Git 密码。

  6. 单击**为 创建 GitHub 应用**,其中 `` 是您的 GitHub 用户名。

验证

成功创建 GitHub 应用后,OpenShift Container Platform Web 控制台将打开并显示有关该应用程序的详细信息。

Github app details

GitHub 应用的详细信息将作为密钥保存在 `openShift-pipelines` 命名空间中。

要查看与 GitHub 应用程序关联的名称、链接和密钥等详细信息,请导航到**Pipelines** 并单击**查看 GitHub 应用**。

手动配置 GitHub 应用并为 Pipelines as Code 创建密钥

您可以使用 GitHub 用户界面创建 GitHub 应用。然后,您必须创建一个密钥,以配置 Pipelines as Code 连接到 GitHub 应用。

如果您创建了其他 Pipelines as Code 控制器以支持其他 GitHub 应用,则必须对其他控制器使用此步骤。

步骤
  1. 登录您的 GitHub 帐户。

  2. 在 GitHub 菜单中,选择**设置** → **开发者设置** → **GitHub 应用**,然后单击**新建 GitHub 应用**。

  3. 在 GitHub 应用表单中提供以下信息

    • **GitHub 应用名称**: `OpenShift Pipelines`

    • **主页 URL**: OpenShift 控制台 URL

    • **Webhook URL**: Pipelines as Code 路由或入口 URL。您可以通过运行以下命令找到它

      $ echo https://$(oc get route -n openshift-pipelines pipelines-as-code-controller -o jsonpath='{.spec.host}')

      或者,要为其他 Pipelines as Code 控制器配置 GitHub 应用,请将 `pipelines-as-code-controller` 替换为您配置的控制器的名称,如下例所示

      示例命令
      $ echo https://$(oc get route -n openshift-pipelines pac_controller_2 -o jsonpath='{.spec.host}')
    • **Webhook 密钥**: 一个任意的密钥。您可以通过运行以下命令生成密钥

      $ openssl rand -hex 20
  4. 在**仓库权限**部分中选择以下项目

    • **检查**: `读写`

    • **内容**: `读写`

    • **问题**: `读写`

    • **元数据**: `只读`

    • **拉取请求**: `读写`

  5. 在**组织权限**部分中选择以下项目

    • **成员**: `只读`

    • **计划**: `只读`

  6. 订阅以下事件

    • 检查运行

    • 检查套件

    • 提交评论

    • 问题评论

    • 拉取请求

    • 推送

  7. 单击**创建 GitHub 应用**。

  8. 在新创建的 GitHub 应用的**详细信息**页面上,注意顶部显示的**应用 ID**。

  9. 在**私钥**部分,单击**生成私钥**以自动生成并下载 GitHub 应用的私钥。安全地存储私钥以备将来参考和使用。

  10. 在您要与 Pipelines as Code 一起使用的仓库上安装已创建的应用。

  11. 通过输入以下命令配置 Pipelines as Code 以访问新创建的 GitHub 应用

    $ oc -n openshift-pipelines create secret generic pipelines-as-code-secret \ (1)
            --from-literal github-private-key="$(cat <PATH_PRIVATE_KEY>)" \ (2)
            --from-literal github-application-id="<APP_ID>" \ (3)
            --from-literal webhook.secret="<WEBHOOK_SECRET>" (4)
    
    1 如果您创建了其他 Pipelines as Code 控制器以支持其他 GitHub 应用,并且您正在为其他控制器配置应用,请将 `pipelines-as-code-secret` 替换为您在控制器的 `secretName` 参数中配置的名称。
    2 您在配置 GitHub 应用时下载的私钥的路径。
    3 GitHub 应用的**应用 ID**。
    4 创建 GitHub 应用时提供的 Webhook 密钥。

Pipelines as Code 通过检测从 GitHub Enterprise 设置的标头并将其用于 GitHub Enterprise API 授权 URL 来自动与 GitHub Enterprise 协同工作。

将 GitHub 令牌的范围扩展到其他仓库

Pipelines as Code 使用 GitHub 应用生成 GitHub 访问令牌。Pipelines as Code 使用此令牌从仓库检索管道有效负载,并使 CI/CD 进程能够与 GitHub 仓库交互。

默认情况下,访问令牌的范围仅限于 Pipelines as Code 从中检索管道定义的仓库。在某些情况下,您可能希望令牌访问其他仓库。例如,可能存在一个 CI 仓库,其中包含 `.tekton/pr.yaml` 文件和源有效负载,但 `pr.yaml` 中定义的构建过程从单独的私有 CD 仓库获取任务。

您可以通过两种方式扩展 GitHub 令牌的范围

  • 全局配置:您可以将 GitHub 令牌扩展到不同命名空间中的仓库列表。您必须具有管理员权限才能设置此配置。

  • 仓库级别配置:您可以将 GitHub 令牌扩展到与原始仓库位于同一命名空间中的仓库列表。您不需要管理员权限即可设置此配置。

步骤
  1. 在 `TektonConfig` 自定义资源 (CR) 中,在 `pipelinesAsCode.settings` 规范中,将 `secret-github-app-token-scoped` 参数设置为 `false`。此设置启用将 GitHub 令牌的范围扩展到全局和仓库级别配置中列出的私有和公共仓库。

  2. 要设置 GitHub 令牌范围的全局配置,请在 `TektonConfig` CR 中,在 `pipelinesAsCode.settings` 规范中,在 `secret-github-app-scope-extra-repos` 参数中指定其他仓库,如下例所示

    apiVersion: operator.tekton.dev/v1alpha1
    kind: TektonConfig
    metadata:
      name: config
    spec:
      platforms:
        openshift:
          pipelinesAsCode:
            enable: true
            settings:
              secret-github-app-token-scoped: false
              secret-github-app-scope-extra-repos: "owner2/project2, owner3/project3"
  3. 要设置 GitHub 令牌范围的仓库级别配置,请在 `Repository` CR 的 `github_app_token_scope_repos` 参数中指定其他仓库,如下例所示

    apiVersion: "pipelinesascode.tekton.dev/v1alpha1"
    kind: Repository
    metadata:
      name: test
      namespace: test-repo
    spec:
      url: "https://github.com/linda/project"
      settings:
        github_app_token_scope_repos:
        - "owner/project"
        - "owner1/project1"

    在此示例中,`Repository` 自定义资源与 `test-repo` 命名空间中的 `linda/project` 仓库相关联。生成的 GitHub 令牌的范围扩展到 `owner/project` 和 `owner1/project1` 仓库以及 `linda/project` 仓库。这些仓库必须存在于 `test-repo` 命名空间下。

    其他仓库可以是公共的或私有的,但必须与 `Repository` 资源关联的仓库位于同一命名空间。

    如果任何仓库不存在于命名空间中,则 GitHub 令牌的范围设置将失败并显示错误消息。

    failed to scope GitHub token as repo owner1/project1 does not exist in namespace test-repo
结果

生成的 GitHub 令牌启用对您在全局和仓库级别配置中配置的其他仓库以及 Pipelines as Code 有效负载文件所在的原始仓库的访问。

如果您同时提供全局配置和仓库级别配置,则令牌的范围将包括两个配置中的所有仓库,如下例所示。

`TektonConfig` 自定义资源
apiVersion: operator.tekton.dev/v1alpha1
kind: TektonConfig
metadata:
  name: config
spec:
  platforms:
    openshift:
      pipelinesAsCode:
        enable: true
        settings:
          secret-github-app-token-scoped: false
          secret-github-app-scope-extra-repos: "owner2/project2, owner3/project3"
`Repository` 自定义资源
apiVersion: "pipelinesascode.tekton.dev/v1alpha1"
kind: Repository
metadata:
 name: test
 namespace: test-repo
spec:
 url: "https://github.com/linda/project"
 settings:
   github_app_token_scope_repos:
   - "owner/project"
   - "owner1/project1"

GitHub 令牌的作用域限定在以下代码库:owner/projectowner1/project1owner2/project2owner3/project3linda/project

使用 GitHub Webhook 的代码流水线

如果您无法创建 GitHub 应用,请在您的代码库中使用带有 GitHub Webhook 的代码流水线。但是,使用带有 GitHub Webhook 的代码流水线无法访问 GitHub 检查运行 API。任务的状态会作为评论添加到拉取请求中,并且在**检查**选项卡下不可用。

使用 GitHub Webhook 的代码流水线不支持 GitOps 注释,例如/retest/ok-to-test。要重新启动持续集成 (CI),请向代码库创建新的提交。例如,要创建没有任何更改的新提交,可以使用以下命令

$ git --amend -a --no-edit && git push --force-with-lease <origin> <branchname>
先决条件
  • 确保在集群上安装了代码流水线。

  • 为了授权,请在 GitHub 上创建一个个人访问令牌。

    • 要生成安全且细粒度的令牌,请将其作用域限制为特定代码库,并授予以下权限

      表 1. 细粒度令牌的权限
      名称 访问

      管理

      只读

      元数据

      只读

      内容

      只读

      提交状态

      读写

      拉取请求

      读写

      Webhooks

      读写

    • 要使用经典令牌,请针对公共代码库将作用域设置为public_repo,针对私有代码库将作用域设置为repo。此外,请提供较短的令牌过期时间,并在其他位置记录该令牌。

      如果您想使用tkn pac CLI 配置 webhook,请添加admin:repo_hook作用域。

步骤
  1. 配置 webhook 并创建一个Repository自定义资源 (CR)。

    • 要使用tkn pac CLI 工具自动配置 webhook 并创建Repository CR,请使用以下命令

      $ tkn pac create repo
      示例交互式输出
      ? Enter the Git repository url (default: https://github.com/owner/repo):
      ? Please enter the namespace where the pipeline should run (default: repo-pipelines):
      ! Namespace repo-pipelines is not found
      ? Would you like me to create the namespace repo-pipelines? Yes
      ✓ Repository owner-repo has been created in repo-pipelines namespace
      ✓ Setting up GitHub Webhook for Repository https://github.com/owner/repo
      👀 I have detected a controller url: https://pipelines-as-code-controller-openshift-pipelines.apps.example.com
      ? Do you want me to use it? Yes
      ? Please enter the secret to configure the webhook for payload validation (default: sJNwdmTifHTs):  sJNwdmTifHTs
      ℹ ️You now need to create a GitHub personal access token, please checkout the docs at https://githubdocs.cn/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token for the required scopes
      ? Please enter the GitHub access token:  ****************************************
      ✓ Webhook has been created on repository owner/repo
      🔑 Webhook Secret owner-repo has been created in the repo-pipelines namespace.
      🔑 Repository CR owner-repo has been updated with webhook secret in the repo-pipelines namespace
      ℹ Directory .tekton has been created.
      ✓ We have detected your repository using the programming language Go.
      ✓ A basic template has been created in /home/Go/src/github.com/owner/repo/.tekton/pipelinerun.yaml, feel free to customize it.
    • 手动配置 webhook 并创建Repository CR,请执行以下步骤

      1. 在您的 OpenShift 集群上,提取代码流水线的控制器公共 URL。

        $ echo https://$(oc get route -n openshift-pipelines pipelines-as-code-controller -o jsonpath='{.spec.host}')
      2. 在您的 GitHub 代码库或组织中,执行以下步骤

        1. 转到**设置** -> **Webhooks**,然后单击**添加 webhook**。

        2. 将**有效负载 URL**设置为代码流水线控制器公共 URL。

        3. 选择内容类型为**application/json**。

        4. 添加 webhook 密钥,并在其他位置记录它。如果您的本地机器上安装了openssl,请生成一个随机密钥。

          $ openssl rand -hex 20
        5. 单击**让我选择单个事件**,然后选择这些事件:**提交评论**、**问题评论**、**拉取请求**和**推送**。

        6. 单击**添加 webhook**。

      3. 在您的 OpenShift 集群上,使用个人访问令牌和 webhook 密钥创建一个Secret对象。

        $ oc -n target-namespace create secret generic github-webhook-config \
          --from-literal provider.token="<GITHUB_PERSONAL_ACCESS_TOKEN>" \
          --from-literal webhook.secret="<WEBHOOK_SECRET>"
      4. 创建一个Repository CR。

        示例:Repository CR
        apiVersion: "pipelinesascode.tekton.dev/v1alpha1"
        kind: Repository
        metadata:
          name: my-repo
          namespace: target-namespace
        spec:
          url: "https://github.com/owner/repo"
          git_provider:
            secret:
              name: "github-webhook-config"
              key: "provider.token" # Set this if you have a different key in your secret
            webhook_secret:
              name: "github-webhook-config"
              key: "webhook.secret" # Set this if you have a different key for your secret

        代码流水线假定 OpenShift Secret 对象和Repository CR 位于同一个命名空间。

  2. 可选:对于现有的Repository CR,添加多个 GitHub Webhook 密钥,或提供已删除密钥的替代项。

    1. 使用tkn pac CLI 工具添加 webhook。

      示例:使用tkn pac CLI 添加其他 webhook
      $ tkn pac webhook add -n repo-pipelines
      示例交互式输出
      ✓ Setting up GitHub Webhook for Repository https://github.com/owner/repo
      👀 I have detected a controller url: https://pipelines-as-code-controller-openshift-pipelines.apps.example.com
      ? Do you want me to use it? Yes
      ? Please enter the secret to configure the webhook for payload validation (default: AeHdHTJVfAeH):  AeHdHTJVfAeH
      ✓ Webhook has been created on repository owner/repo
      🔑 Secret owner-repo has been updated with webhook secert in the repo-pipelines namespace.
    2. 更新现有 OpenShift Secret 对象中的webhook.secret密钥。

  3. 可选:对于现有的Repository CR,更新个人访问令牌。

    • 使用tkn pac CLI 工具更新个人访问令牌。

      示例:使用tkn pac CLI 更新个人访问令牌
      $ tkn pac webhook update-token -n repo-pipelines
      示例交互式输出
      ? Please enter your personal access token:  ****************************************
      🔑 Secret owner-repo has been updated with new personal access token in the repo-pipelines namespace.
    • 或者,通过修改Repository CR 来更新个人访问令牌。

      1. 查找Repository CR 中密钥的名称。

        apiVersion: "pipelinesascode.tekton.dev/v1alpha1"
        kind: Repository
        metadata:
          name: my-repo
          namespace: target-namespace
        spec:
        # ...
          git_provider:
            secret:
              name: "github-webhook-config"
        # ...
      2. 使用oc patch命令更新$target_namespace命名空间中的$NEW_TOKEN值。

        $ oc -n $target_namespace patch secret github-webhook-config -p "{\"data\": {\"provider.token\": \"$(echo -n $NEW_TOKEN|base64 -w0)\"}}"

使用 GitLab 的代码流水线

如果您的组织或项目使用 GitLab 作为首选平台,则可以使用代码流水线以及 GitLab 上的 webhook 来处理您的代码库。

先决条件
  • 确保在集群上安装了代码流水线。

  • 为了授权,请在 GitLab 上作为项目的管理员或组织的管理员生成个人访问令牌。

    • 如果您想使用tkn pac CLI 配置 webhook,请向令牌添加admin:repo_hook作用域。

    • 使用针对特定项目限定作用域的令牌无法提供对从派生代码库发送的合并请求 (MR) 的 API 访问权限。在这种情况下,代码流水线会将管道的结果显示为 MR 上的注释。

步骤
  1. 配置 webhook 并创建一个Repository自定义资源 (CR)。

    • 要使用tkn pac CLI 工具自动配置 webhook 并创建Repository CR,请使用以下命令

      $ tkn pac create repo
      示例交互式输出
      ? Enter the Git repository url (default: https://gitlab.com/owner/repo):
      ? Please enter the namespace where the pipeline should run (default: repo-pipelines):
      ! Namespace repo-pipelines is not found
      ? Would you like me to create the namespace repo-pipelines? Yes
      ✓ Repository repositories-project has been created in repo-pipelines namespace
      ✓ Setting up GitLab Webhook for Repository https://gitlab.com/owner/repo
      ? Please enter the project ID for the repository you want to be configured,
        project ID refers to an unique ID (e.g. 34405323) shown at the top of your GitLab project : 17103
      👀 I have detected a controller url: https://pipelines-as-code-controller-openshift-pipelines.apps.example.com
      ? Do you want me to use it? Yes
      ? Please enter the secret to configure the webhook for payload validation (default: lFjHIEcaGFlF):  lFjHIEcaGFlF
      ℹ ️You now need to create a GitLab personal access token with `api` scope
      ℹ ️Go to this URL to generate one https://gitlab.com/-/profile/personal_access_tokens, see https://is.gd/rOEo9B for documentation
      ? Please enter the GitLab access token:  **************************
      ? Please enter your GitLab API URL::  https://gitlab.com
      ✓ Webhook has been created on your repository
      🔑 Webhook Secret repositories-project has been created in the repo-pipelines namespace.
      🔑 Repository CR repositories-project has been updated with webhook secret in the repo-pipelines namespace
      ℹ Directory .tekton has been created.
      ✓ A basic template has been created in /home/Go/src/gitlab.com/repositories/project/.tekton/pipelinerun.yaml, feel free to customize it.
    • 手动配置 webhook 并创建Repository CR,请执行以下步骤

      1. 在您的 OpenShift 集群上,提取代码流水线的控制器公共 URL。

        $ echo https://$(oc get route -n openshift-pipelines pipelines-as-code-controller -o jsonpath='{.spec.host}')
      2. 在您的 GitLab 项目上,执行以下步骤

        1. 使用左侧边栏转到**设置** -> **Webhooks**。

        2. 将**URL**设置为代码流水线控制器公共 URL。

        3. 添加 webhook 密钥,并在其他位置记录它。如果您的本地机器上安装了openssl,请生成一个随机密钥。

          $ openssl rand -hex 20
        4. 单击**让我选择单个事件**,然后选择这些事件:**提交评论**、**问题评论**、**拉取请求**和**推送**。

        5. 单击**保存更改**。

      3. 在您的 OpenShift 集群上,使用个人访问令牌和 webhook 密钥创建一个Secret对象。

        $ oc -n target-namespace create secret generic gitlab-webhook-config \
          --from-literal provider.token="<GITLAB_PERSONAL_ACCESS_TOKEN>" \
          --from-literal webhook.secret="<WEBHOOK_SECRET>"
      4. 创建一个Repository CR。

        示例:Repository CR
        apiVersion: "pipelinesascode.tekton.dev/v1alpha1"
        kind: Repository
        metadata:
          name: my-repo
          namespace: target-namespace
        spec:
          url: "https://gitlab.com/owner/repo" # The repository URL
          git_provider:
            #url: "https://gitlab.example.com/" (1)
            secret:
              name: "gitlab-webhook-config"
              key: "provider.token" # Set this if you have a different key in your secret
            webhook_secret:
              name: "gitlab-webhook-config"
              key: "webhook.secret" # Set this if you have a different key for your secret
        1 如果您使用的是 GitLab 私有实例而不是 GitLab.com,请取消注释此字段并将其设置为 GitLab API 的 URL。GitLab API 与代码库的主机相同。例如,如果代码库是https://gitlab.example.com/owner/repo,则 API URL 为https://gitlab.example.com/
    • 代码流水线假定 OpenShift Secret 对象和Repository CR 位于同一个命名空间。

  2. 可选:对于现有的Repository CR,添加多个 GitLab Webhook 密钥,或提供已删除密钥的替代项。

    1. 使用tkn pac CLI 工具添加 webhook。

      示例:使用tkn pac CLI 添加其他 webhook
      $ tkn pac webhook add -n repo-pipelines
      示例交互式输出
      ✓ Setting up GitLab Webhook for Repository https://gitlab.com/owner/repo
      👀 I have detected a controller url: https://pipelines-as-code-controller-openshift-pipelines.apps.example.com
      ? Do you want me to use it? Yes
      ? Please enter the secret to configure the webhook for payload validation (default: AeHdHTJVfAeH):  AeHdHTJVfAeH
      ✓ Webhook has been created on repository owner/repo
      🔑 Secret owner-repo has been updated with webhook secert in the repo-pipelines namespace.
    2. 更新现有 OpenShift Secret 对象中的webhook.secret密钥。

  3. 可选:对于现有的Repository CR,更新个人访问令牌。

    • 使用tkn pac CLI 工具更新个人访问令牌。

      示例:使用tkn pac CLI 更新个人访问令牌
      $ tkn pac webhook update-token -n repo-pipelines
      示例交互式输出
      ? Please enter your personal access token:  ****************************************
      🔑 Secret owner-repo has been updated with new personal access token in the repo-pipelines namespace.
    • 或者,通过修改Repository CR 来更新个人访问令牌。

      1. 查找Repository CR 中密钥的名称。

        ...
        spec:
          git_provider:
            secret:
              name: "gitlab-webhook-config"
        ...
      2. 使用oc patch命令更新$target_namespace命名空间中的$NEW_TOKEN值。

        $ oc -n $target_namespace patch secret gitlab-webhook-config -p "{\"data\": {\"provider.token\": \"$(echo -n $NEW_TOKEN|base64 -w0)\"}}"

使用 Bitbucket Cloud 的代码流水线

如果您的组织或项目使用 Bitbucket Cloud 作为首选平台,则可以使用代码流水线以及 Bitbucket Cloud 上的 webhook 来处理您的代码库。

先决条件
  • 确保在集群上安装了代码流水线。

  • 在 Bitbucket Cloud 上创建一个应用密码。

    • 选中以下复选框,以向令牌添加相应的权限

      • 帐户:电子邮件读取

      • 工作区成员身份:读取写入

      • 项目:读取写入

      • 问题:读取写入

      • 拉取请求:读取写入

        • 如果您想使用tkn pac CLI 配置 webhook,请向令牌添加Webhooks读取写入权限。

        • 生成后,请在其他位置保存密码或令牌的副本。

步骤
  1. 配置 webhook 并创建一个Repository CR。

    • 要使用tkn pac CLI 工具自动配置 webhook 并创建Repository CR,请使用以下命令

      $ tkn pac create repo
      示例交互式输出
      ? Enter the Git repository url (default: https://bitbucket.org/workspace/repo):
      ? Please enter the namespace where the pipeline should run (default: repo-pipelines):
      ! Namespace repo-pipelines is not found
      ? Would you like me to create the namespace repo-pipelines? Yes
      ✓ Repository workspace-repo has been created in repo-pipelines namespace
      ✓ Setting up Bitbucket Webhook for Repository https://bitbucket.org/workspace/repo
      ? Please enter your bitbucket cloud username:  <username>
      ℹ ️You now need to create a Bitbucket Cloud app password, please checkout the docs at https://is.gd/fqMHiJ for the required permissions
      ? Please enter the Bitbucket Cloud app password:  ************************************
      👀 I have detected a controller url: https://pipelines-as-code-controller-openshift-pipelines.apps.example.com
      ? Do you want me to use it? Yes
      ✓ Webhook has been created on repository workspace/repo
      🔑 Webhook Secret workspace-repo has been created in the repo-pipelines namespace.
      🔑 Repository CR workspace-repo has been updated with webhook secret in the repo-pipelines namespace
      ℹ Directory .tekton has been created.
      ✓ A basic template has been created in /home/Go/src/bitbucket/repo/.tekton/pipelinerun.yaml, feel free to customize it.
    • 手动配置 webhook 并创建Repository CR,请执行以下步骤

      1. 在您的 OpenShift 集群上,提取代码流水线的控制器公共 URL。

        $ echo https://$(oc get route -n openshift-pipelines pipelines-as-code-controller -o jsonpath='{.spec.host}')
      2. 在 Bitbucket Cloud 上,执行以下步骤

        1. 使用 Bitbucket Cloud 代码库的左侧导航窗格转到**代码库设置** -> **Webhooks**,然后单击**添加 webhook**。

        2. 设置一个**标题**。例如,“代码流水线”。

        3. 将**URL**设置为代码流水线控制器公共 URL。

        4. 选择这些事件:**代码库:推送**、**拉取请求:创建**、**拉取请求:更新**和**拉取请求:创建评论**。

        5. 单击**保存**。

      3. 在您的 OpenShift 集群上,在目标命名空间中使用应用密码创建一个Secret对象。

        $ oc -n target-namespace create secret generic bitbucket-cloud-token \
          --from-literal provider.token="<BITBUCKET_APP_PASSWORD>"
      4. 创建一个Repository CR。

        示例:Repository CR
        apiVersion: "pipelinesascode.tekton.dev/v1alpha1"
        kind: Repository
        metadata:
          name: my-repo
          namespace: target-namespace
        spec:
          url: "https://bitbucket.com/workspace/repo"
          branch: "main"
          git_provider:
            user: "<BITBUCKET_USERNAME>" (1)
            secret:
              name: "bitbucket-cloud-token" (2)
              key: "provider.token" # Set this if you have a different key in your secret
        1 您只能在所有者文件中使用ACCOUNT_ID引用用户。
        2 代码流水线假定git_provider.secret规范中引用的密钥和Repository CR 位于同一个命名空间。
    • tkn pac createtkn pac bootstrap 命令在 Bitbucket Cloud 上不受支持。

    • Bitbucket Cloud 不支持 webhook 密钥。为了保护有效负载并防止 CI 被劫持,Pipelines as Code 会获取 Bitbucket Cloud IP 地址列表,并确保 webhook 接收只来自这些 IP 地址。

      • 要禁用默认行为,请在 TektonConfig 自定义资源中,pipelinesAsCode.settings规范内将 bitbucket-cloud-check-source-ip 参数设置为 false

      • 要允许其他安全的 IP 地址或网络,请将它们作为逗号分隔的值添加到 TektonConfig 自定义资源中,pipelinesAsCode.settings规范内的 bitbucket-cloud-additional-source-ip 参数。

  2. 可选:对于现有的 Repository CR,添加多个 Bitbucket Cloud Webhook 密钥,或提供已删除密钥的替代密钥。

    1. 使用tkn pac CLI 工具添加 webhook。

      示例:使用tkn pac CLI 添加其他 webhook
      $ tkn pac webhook add -n repo-pipelines
      示例交互式输出
      ✓ Setting up Bitbucket Webhook for Repository https://bitbucket.org/workspace/repo
      ? Please enter your bitbucket cloud username:  <username>
      👀 I have detected a controller url: https://pipelines-as-code-controller-openshift-pipelines.apps.example.com
      ? Do you want me to use it? Yes
      ✓ Webhook has been created on repository workspace/repo
      🔑 Secret workspace-repo has been updated with webhook secret in the repo-pipelines namespace.

      只有当 Repository CR 存在于默认命名空间以外的命名空间中时,才应在 tkn pac webhook add 命令中使用 [-n <namespace>] 选项。

    2. 更新现有 OpenShift Secret 对象中的webhook.secret密钥。

  3. 可选:对于现有的Repository CR,更新个人访问令牌。

    • 使用tkn pac CLI 工具更新个人访问令牌。

      示例:使用tkn pac CLI 更新个人访问令牌
      $ tkn pac webhook update-token -n repo-pipelines
      示例交互式输出
      ? Please enter your personal access token:  ****************************************
      🔑 Secret owner-repo has been updated with new personal access token in the repo-pipelines namespace.

      只有当 Repository CR 存在于默认命名空间以外的命名空间中时,才应在 tkn pac webhook update-token 命令中使用 [-n <namespace>] 选项。

    • 或者,通过修改Repository CR 来更新个人访问令牌。

      1. 查找Repository CR 中密钥的名称。

        ...
        spec:
          git_provider:
            user: "<BITBUCKET_USERNAME>"
            secret:
              name: "bitbucket-cloud-token"
              key: "provider.token"
        ...
      2. 使用 oc patch 命令更新 $target_namespace 命名空间中 $password 的值。

        $ oc -n $target_namespace patch secret bitbucket-cloud-token -p "{\"data\": {\"provider.token\": \"$(echo -n $NEW_TOKEN|base64 -w0)\"}}"

使用 Pipelines as Code 与 Bitbucket Server

如果您的组织或项目使用 Bitbucket Server 作为首选平台,您可以使用 Pipelines as Code 来处理您的带有 Bitbucket Server Webhook 的代码库。

先决条件
  • 确保在集群上安装了代码流水线。

  • 在 Bitbucket Server 上,以项目管理员的身份生成一个个人访问令牌,并将它的副本保存在其他位置。

    • 该令牌必须具有 PROJECT_ADMINREPOSITORY_ADMIN 权限。

    • 该令牌必须能够访问拉取请求中的分支代码库。

步骤
  1. 在您的 OpenShift 集群上,提取代码流水线的控制器公共 URL。

    $ echo https://$(oc get route -n openshift-pipelines pipelines-as-code-controller -o jsonpath='{.spec.host}')
  2. 在 Bitbucket Server 上,执行以下步骤

    1. 使用 Bitbucket Data Center 代码库的左侧导航窗格,转到 **代码库设置** -> **Webhooks** 并点击 **添加 Webhook**。

    2. 设置一个**标题**。例如,“代码流水线”。

    3. 将**URL**设置为代码流水线控制器公共 URL。

    4. 添加一个 Webhook 密钥,并将它的副本保存在其他位置。如果您的本地机器上安装了 openssl,请使用以下命令生成一个随机密钥

      $ openssl rand -hex 20
    5. 选择以下事件:

      • 代码库:推送

      • 代码库:修改

      • 拉取请求:已打开

      • 拉取请求:源分支已更新

      • 拉取请求:添加评论

    6. 单击**保存**。

  3. 在您的 OpenShift 集群上,在目标命名空间中使用应用密码创建一个Secret对象。

    $ oc -n target-namespace create secret generic bitbucket-server-webhook-config \
      --from-literal provider.token="<PERSONAL_TOKEN>" \
      --from-literal webhook.secret="<WEBHOOK_SECRET>"
  4. 创建一个Repository CR。

    示例:Repository CR
    apiVersion: "pipelinesascode.tekton.dev/v1alpha1"
    kind: Repository
    metadata:
      name: my-repo
      namespace: target-namespace
    spec:
      url: "https://bitbucket.com/workspace/repo"
      git_provider:
        url: "https://bitbucket.server.api.url/rest" (1)
        user: "<BITBUCKET_USERNAME>" (2)
        secret: (3)
          name: "bitbucket-server-webhook-config"
          key: "provider.token" # Set this if you have a different key in your secret
        webhook_secret:
          name: "bitbucket-server-webhook-config"
          key: "webhook.secret" # Set this if you have a different key for your secret
    1 确保您拥有正确的 Bitbucket Server API URL,不包含 /api/v1.0 后缀。通常,默认安装具有 /rest 后缀。
    2 您只能在所有者文件中使用ACCOUNT_ID引用用户。
    3 代码流水线假定git_provider.secret规范中引用的密钥和Repository CR 位于同一个命名空间。

    tkn pac createtkn pac bootstrap 命令在 Bitbucket Server 上不受支持。

Pipelines as Code 与自定义证书的接口

要使用私有签名或自定义证书访问的 Git 代码库配置 Pipelines as Code,您可以将证书公开给 Pipelines as Code。

步骤
  • 如果您使用 Red Hat OpenShift Pipelines Operator 安装了 Pipelines as Code,您可以使用 Proxy 对象将自定义证书添加到集群。Operator 会在所有 Red Hat OpenShift Pipelines 组件和工作负载(包括 Pipelines as Code)中公开证书。

使用 Pipelines as Code 使用私有代码库

Pipelines as Code 通过在目标命名空间中创建或更新包含用户令牌的密钥来支持私有代码库。Tekton Hub 中的 git-clone 任务使用用户令牌克隆私有代码库。

每当 Pipelines as Code 在目标命名空间中创建一个新的管道运行时,它都会创建一个或更新一个格式为 pac-gitauth-<REPOSITORY_OWNER>-<REPOSITORY_NAME>-<RANDOM_STRING> 的密钥。

您必须在管道运行和管道定义中使用 basic-auth 工作区引用该密钥,然后将其传递给 git-clone 任务。

...
  workspace:
  - name: basic-auth
    secret:
      secretName: "{{ git_auth_secret }}"
...

在管道中,您可以引用 git-clone 任务的 basic-auth 工作区以重复使用。

...
workspaces:
  - name basic-auth
params:
    - name: repo_url
    - name: revision
...
tasks:
  workspaces:
    - name: basic-auth
      workspace: basic-auth
  ...
  tasks:
  - name: git-clone-from-catalog
      taskRef:
        name: git-clone (1)
      params:
        - name: url
          value: $(params.repo_url)
        - name: revision
          value: $(params.revision)
...
1 git-clone 任务会获取 basic-auth 工作区并使用它来克隆私有代码库。

您可以通过根据需要将 secret-auto-create 参数设置为 falsetrue 值来修改此配置,该参数位于 TektonConfig 自定义资源的 pipelinesAsCode.settings 规范中。