搜索

3.4. 在 Bitbucket Cloud 中使用 Pipelines as Code

download PDF

如果您的机构或项目使用 Bitbucket Cloud 作为首选平台,您可以在 Bitbucket Cloudb 上使用带有 webhook 的仓库的 Pipelines as Code。

先决条件

  • 确保在集群中安装了 Pipelines as Code。
  • 在 Bitbucket Cloud 上创建一个应用程序密码。

    • 选中以下框,为令牌添加适当的权限:

      • 账户:Email, Read
      • 工作区成员资格: ReadWrite
      • 项目: Read,Write
      • Issues: Read,Write
      • Pull requests: Read,Write

        注意
        • 如果要使用 tkn pac CLI 配置 webhook,请在令牌中添加 Webhooks:ReadWrite 权限。
        • 生成后,在另外一个位置保持密码或令牌的副本。

流程

  1. 配置 Webhook 并创建一个 Repository CR。

    • 要配置 webhook 并使用 tkn pac CLI 工具自动创建一个 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 集群中,提取 Pipelines as Code 控制器的公共 URL。

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

        1. 使用 Bitbucket Cloud 存储库的左侧导航窗格,进入 Repository settings -> Webhooks,然后点 Add webhook
        2. 设置 Title。例如,"Pipelines as Code"。
        3. URL 设置为 Pipelines as Code 控制器公共 URL。
        4. 选择这些事件: Repository: Push,Pull Request: Created,Pull Request: Updated, 和 Pull Request: Comment created.
        5. 点击 Save
      3. 在 OpenShift 集群中,使用目标命名空间中的 app 密码创建一个 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
        Pipelines as Code 假设 git_provider.secret spec 引用的 secret 和Repository CR 位于同一命名空间中。
    注意
    • Bitbucket 云不支持 tkn pac createtkn pac bootstrap 命令。
    • Bitbucket 云不支持 Webhook secret。为了保护有效负载并防止 CI 被劫持,Pipelines as Code 会获取 Bitbucket 云 IP 地址列表,并确保 Webhook 接收仅来自这些 IP 地址。

      • 要禁用默认行为,在 TektonConfig 自定义资源中将 bitbucket-cloud-check-source-ip 参数设置为 false,在 pipelinesAsCode.settings spec 中。
      • 要允许额外的安全 IP 地址或网络,请将它们作为逗号分隔的值添加到 TektonConfig 自定义资源中的 bitbucket-cloud-additional-source-ip 参数中,在 pipelinesAsCode.settings spec 中。
  2. 可选: 对于现有的 Repository CR,请添加多个 Bitbucket Cloud Webhook secret 或为已删除的 secret 提供替换。

    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 在 default 命名空间以外的命名空间中存在时,才在运行 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 在 default 命名空间以外的命名空间中存在时,才在 tkn pac webhook update-token 命令中使用 [-n <namespace>] 选项。

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

      1. Repository CR 中查找 secret 的名称。

        ...
        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)\"}}"
Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.