3.4. 在 Bitbucket Cloud 中使用 Pipelines as Code
如果您的机构或项目使用 Bitbucket Cloud 作为首选平台,您可以在 Bitbucket Cloudb 上使用带有 webhook 的仓库的 Pipelines as Code。
先决条件
- 确保在集群中安装了 Pipelines as Code。
在 Bitbucket Cloud 上创建一个应用程序密码。
选中以下框,为令牌添加适当的权限:
-
账户:
Email,Read -
工作区成员资格:
Read、Write -
项目:
Read,Write -
Issues:
Read,Write Pull requests:
Read,Write注意-
如果要使用
tkn pacCLI 配置 webhook,请在令牌中添加Webhooks:Read和Write权限。 - 生成后,在另外一个位置保持密码或令牌的副本。
-
如果要使用
-
账户:
流程
配置 Webhook 并创建一个
RepositoryCR。要配置 webhook 并使用
tkn pacCLI 工具自动创建一个RepositoryCR,请使用以下命令:$ 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 并 手动创建
RepositoryCR,请执行以下步骤:在 OpenShift 集群中,提取 Pipelines as Code 控制器的公共 URL。
$ echo https://$(oc get route -n openshift-pipelines pipelines-as-code-controller -o jsonpath='{.spec.host}')在 Bitbucket Cloud 上,执行以下步骤:
- 使用 Bitbucket Cloud 存储库的左侧导航窗格,进入 Repository settings -> Webhooks,然后点 Add webhook。
- 设置 Title。例如,"Pipelines as Code"。
- 将 URL 设置为 Pipelines as Code 控制器公共 URL。
- 选择这些事件: Repository: Push,Pull Request: Created,Pull Request: Updated, 和 Pull Request: Comment created.
- 点击 Save。
在 OpenShift 集群中,使用目标命名空间中的 app 密码创建一个
Secret对象。$ oc -n target-namespace create secret generic bitbucket-cloud-token \ --from-literal provider.token="<BITBUCKET_APP_PASSWORD>"创建
RepositoryCR。示例:
RepositoryCRapiVersion: "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
注意-
Bitbucket 云不支持
tkn pac create和tkn pac bootstrap命令。 Bitbucket 云不支持 Webhook secret。为了保护有效负载并防止 CI 被劫持,Pipelines as Code 会获取 Bitbucket 云 IP 地址列表,并确保 Webhook 接收仅来自这些 IP 地址。
-
要禁用默认行为,在
TektonConfig自定义资源中将bitbucket-cloud-check-source-ip参数设置为false,在pipelinesAsCode.settingsspec 中。 -
要允许额外的安全 IP 地址或网络,请将它们作为逗号分隔的值添加到
TektonConfig自定义资源中的bitbucket-cloud-additional-source-ip参数中,在pipelinesAsCode.settingsspec 中。
-
要禁用默认行为,在
可选: 对于现有的
RepositoryCR,请添加多个 Bitbucket Cloud Webhook secret 或为已删除的 secret 提供替换。使用
tkn pacCLI 工具添加 webhook。示例:使用
tkn pacCLI 添加额外的 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.注意只有在
RepositoryCR 在 default 命名空间以外的命名空间中存在时,才在运行tkn pac webhook add命令时使用[-n <namespace>]选项。-
更新现有 OpenShift
Secret对象中的webhook.secret密钥。
可选: 对于现有的
RepositoryCR,更新个人访问令牌。使用
tkn pacCLI 工具更新个人访问令牌。示例:使用
tkn pacCLI 更新个人访问令牌$ 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.注意只有在
RepositoryCR 在 default 命名空间以外的命名空间中存在时,才在tkn pac webhook update-token命令中使用[-n <namespace>]选项。或者,通过修改
RepositoryCR 来更新个人访问令牌。在
RepositoryCR 中查找 secret 的名称。... spec: git_provider: user: "<BITBUCKET_USERNAME>" secret: name: "bitbucket-cloud-token" key: "provider.token" ...使用
oc patch命令更新$target_namespace命名空间中的$password的值。$ oc -n $target_namespace patch secret bitbucket-cloud-token -p "{\"data\": {\"provider.token\": \"$(echo -n $NEW_TOKEN|base64 -w0)\"}}"