$ tkn pac bootstrap github-app
安装流水线即代码后,集群管理员可以配置 Git 代码库托管服务提供商。当前,支持以下服务:
GitHub App
GitHub Webhook
GitLab
Bitbucket Server
Bitbucket Cloud
建议将 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 应用。
您可以使用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 应用与 OpenShift Container Platform 集群配置为使用 Pipelines as Code。此配置允许您执行构建部署所需的一组任务。
如果您创建了其他流水线即代码控制器来支持其他 GitHub 应用,则只能对主控制器使用此过程。要为其他控制器创建 GitHub 应用,请使用手动过程。 |
您已从 Operator Hub 安装了 Red Hat OpenShift Pipelines `pipelines-1.17` 运算符。
在管理员视角中,使用导航窗格导航到**Pipelines**。
单击**Pipelines**页面上的**设置 GitHub 应用**。
输入您的 GitHub 应用名称。例如,`pipelines-ci-clustername-testui`。
单击**设置**。
在浏览器中提示时输入您的 Git 密码。
单击**为
成功创建 GitHub 应用后,OpenShift Container Platform Web 控制台将打开并显示有关该应用程序的详细信息。
GitHub 应用的详细信息将作为密钥保存在 `openShift-pipelines` 命名空间中。
要查看与 GitHub 应用程序关联的名称、链接和密钥等详细信息,请导航到**Pipelines** 并单击**查看 GitHub 应用**。
您可以使用 GitHub 用户界面创建 GitHub 应用。然后,您必须创建一个密钥,以配置 Pipelines as Code 连接到 GitHub 应用。
如果您创建了其他 Pipelines as Code 控制器以支持其他 GitHub 应用,则必须对其他控制器使用此步骤。
登录您的 GitHub 帐户。
在 GitHub 菜单中,选择**设置** → **开发者设置** → **GitHub 应用**,然后单击**新建 GitHub 应用**。
在 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
在**仓库权限**部分中选择以下项目
**检查**: `读写`
**内容**: `读写`
**问题**: `读写`
**元数据**: `只读`
**拉取请求**: `读写`
在**组织权限**部分中选择以下项目
**成员**: `只读`
**计划**: `只读`
订阅以下事件
检查运行
检查套件
提交评论
问题评论
拉取请求
推送
单击**创建 GitHub 应用**。
在新创建的 GitHub 应用的**详细信息**页面上,注意顶部显示的**应用 ID**。
在**私钥**部分,单击**生成私钥**以自动生成并下载 GitHub 应用的私钥。安全地存储私钥以备将来参考和使用。
在您要与 Pipelines as Code 一起使用的仓库上安装已创建的应用。
通过输入以下命令配置 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 协同工作。 |
Pipelines as Code 使用 GitHub 应用生成 GitHub 访问令牌。Pipelines as Code 使用此令牌从仓库检索管道有效负载,并使 CI/CD 进程能够与 GitHub 仓库交互。
默认情况下,访问令牌的范围仅限于 Pipelines as Code 从中检索管道定义的仓库。在某些情况下,您可能希望令牌访问其他仓库。例如,可能存在一个 CI 仓库,其中包含 `.tekton/pr.yaml` 文件和源有效负载,但 `pr.yaml` 中定义的构建过程从单独的私有 CD 仓库获取任务。
您可以通过两种方式扩展 GitHub 令牌的范围
全局配置:您可以将 GitHub 令牌扩展到不同命名空间中的仓库列表。您必须具有管理员权限才能设置此配置。
仓库级别配置:您可以将 GitHub 令牌扩展到与原始仓库位于同一命名空间中的仓库列表。您不需要管理员权限即可设置此配置。
在 `TektonConfig` 自定义资源 (CR) 中,在 `pipelinesAsCode.settings` 规范中,将 `secret-github-app-token-scoped` 参数设置为 `false`。此设置启用将 GitHub 令牌的范围扩展到全局和仓库级别配置中列出的私有和公共仓库。
要设置 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"
要设置 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 令牌的范围设置将失败并显示错误消息。
|
生成的 GitHub 令牌启用对您在全局和仓库级别配置中配置的其他仓库以及 Pipelines as Code 有效负载文件所在的原始仓库的访问。
如果您同时提供全局配置和仓库级别配置,则令牌的范围将包括两个配置中的所有仓库,如下例所示。
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"
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/project
、owner1/project1
、owner2/project2
、owner3/project3
和 linda/project
。
如果您无法创建 GitHub 应用,请在您的代码库中使用带有 GitHub Webhook 的代码流水线。但是,使用带有 GitHub Webhook 的代码流水线无法访问 GitHub 检查运行 API。任务的状态会作为评论添加到拉取请求中,并且在**检查**选项卡下不可用。
使用 GitHub Webhook 的代码流水线不支持 GitOps 注释,例如
|
确保在集群上安装了代码流水线。
为了授权,请在 GitHub 上创建一个个人访问令牌。
要生成安全且细粒度的令牌,请将其作用域限制为特定代码库,并授予以下权限
名称 | 访问 |
---|---|
管理 |
只读 |
元数据 |
只读 |
内容 |
只读 |
提交状态 |
读写 |
拉取请求 |
读写 |
Webhooks |
读写 |
要使用经典令牌,请针对公共代码库将作用域设置为public_repo
,针对私有代码库将作用域设置为repo
。此外,请提供较短的令牌过期时间,并在其他位置记录该令牌。
如果您想使用 |
配置 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,请执行以下步骤
在您的 OpenShift 集群上,提取代码流水线的控制器公共 URL。
$ echo https://$(oc get route -n openshift-pipelines pipelines-as-code-controller -o jsonpath='{.spec.host}')
在您的 GitHub 代码库或组织中,执行以下步骤
转到**设置** -> **Webhooks**,然后单击**添加 webhook**。
将**有效负载 URL**设置为代码流水线控制器公共 URL。
选择内容类型为**application/json**。
添加 webhook 密钥,并在其他位置记录它。如果您的本地机器上安装了openssl
,请生成一个随机密钥。
$ openssl rand -hex 20
单击**让我选择单个事件**,然后选择这些事件:**提交评论**、**问题评论**、**拉取请求**和**推送**。
单击**添加 webhook**。
在您的 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>"
创建一个Repository
CR。
Repository
CRapiVersion: "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 |
可选:对于现有的Repository
CR,添加多个 GitHub Webhook 密钥,或提供已删除密钥的替代项。
使用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.
更新现有 OpenShift Secret
对象中的webhook.secret
密钥。
可选:对于现有的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 来更新个人访问令牌。
查找Repository
CR 中密钥的名称。
apiVersion: "pipelinesascode.tekton.dev/v1alpha1"
kind: Repository
metadata:
name: my-repo
namespace: target-namespace
spec:
# ...
git_provider:
secret:
name: "github-webhook-config"
# ...
使用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 上的 webhook 来处理您的代码库。
确保在集群上安装了代码流水线。
为了授权,请在 GitLab 上作为项目的管理员或组织的管理员生成个人访问令牌。
|
配置 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,请执行以下步骤
在您的 OpenShift 集群上,提取代码流水线的控制器公共 URL。
$ echo https://$(oc get route -n openshift-pipelines pipelines-as-code-controller -o jsonpath='{.spec.host}')
在您的 GitLab 项目上,执行以下步骤
使用左侧边栏转到**设置** -> **Webhooks**。
将**URL**设置为代码流水线控制器公共 URL。
添加 webhook 密钥,并在其他位置记录它。如果您的本地机器上安装了openssl
,请生成一个随机密钥。
$ openssl rand -hex 20
单击**让我选择单个事件**,然后选择这些事件:**提交评论**、**问题评论**、**拉取请求**和**推送**。
单击**保存更改**。
在您的 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>"
创建一个Repository
CR。
Repository
CRapiVersion: "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/ 。 |
|
可选:对于现有的Repository
CR,添加多个 GitLab Webhook 密钥,或提供已删除密钥的替代项。
使用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.
更新现有 OpenShift Secret
对象中的webhook.secret
密钥。
可选:对于现有的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 来更新个人访问令牌。
查找Repository
CR 中密钥的名称。
...
spec:
git_provider:
secret:
name: "gitlab-webhook-config"
...
使用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 上的 webhook 来处理您的代码库。
确保在集群上安装了代码流水线。
在 Bitbucket Cloud 上创建一个应用密码。
选中以下复选框,以向令牌添加相应的权限
帐户:电子邮件
、读取
工作区成员身份:读取
、写入
项目:读取
、写入
问题:读取
、写入
拉取请求:读取
、写入
|
配置 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,请执行以下步骤
在您的 OpenShift 集群上,提取代码流水线的控制器公共 URL。
$ echo https://$(oc get route -n openshift-pipelines pipelines-as-code-controller -o jsonpath='{.spec.host}')
在 Bitbucket Cloud 上,执行以下步骤
使用 Bitbucket Cloud 代码库的左侧导航窗格转到**代码库设置** -> **Webhooks**,然后单击**添加 webhook**。
设置一个**标题**。例如,“代码流水线”。
将**URL**设置为代码流水线控制器公共 URL。
选择这些事件:**代码库:推送**、**拉取请求:创建**、**拉取请求:更新**和**拉取请求:创建评论**。
单击**保存**。
在您的 OpenShift 集群上,在目标命名空间中使用应用密码创建一个Secret
对象。
$ oc -n target-namespace create secret generic bitbucket-cloud-token \
--from-literal provider.token="<BITBUCKET_APP_PASSWORD>"
创建一个Repository
CR。
Repository
CRapiVersion: "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 位于同一个命名空间。 |
|
可选:对于现有的 Repository
CR,添加多个 Bitbucket Cloud Webhook 密钥,或提供已删除密钥的替代密钥。
使用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.
只有当 |
更新现有 OpenShift Secret
对象中的webhook.secret
密钥。
可选:对于现有的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 来更新个人访问令牌。
查找Repository
CR 中密钥的名称。
...
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)\"}}"
如果您的组织或项目使用 Bitbucket Server 作为首选平台,您可以使用 Pipelines as Code 来处理您的带有 Bitbucket Server Webhook 的代码库。
确保在集群上安装了代码流水线。
在 Bitbucket Server 上,以项目管理员的身份生成一个个人访问令牌,并将它的副本保存在其他位置。
|
在您的 OpenShift 集群上,提取代码流水线的控制器公共 URL。
$ echo https://$(oc get route -n openshift-pipelines pipelines-as-code-controller -o jsonpath='{.spec.host}')
在 Bitbucket Server 上,执行以下步骤
使用 Bitbucket Data Center 代码库的左侧导航窗格,转到 **代码库设置** -> **Webhooks** 并点击 **添加 Webhook**。
设置一个**标题**。例如,“代码流水线”。
将**URL**设置为代码流水线控制器公共 URL。
添加一个 Webhook 密钥,并将它的副本保存在其他位置。如果您的本地机器上安装了 openssl
,请使用以下命令生成一个随机密钥
$ openssl rand -hex 20
选择以下事件:
代码库:推送
代码库:修改
拉取请求:已打开
拉取请求:源分支已更新
拉取请求:添加评论
单击**保存**。
在您的 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>"
创建一个Repository
CR。
Repository
CRapiVersion: "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 位于同一个命名空间。 |
|
要使用私有签名或自定义证书访问的 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 通过在目标命名空间中创建或更新包含用户令牌的密钥来支持私有代码库。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
参数设置为 false
或 true
值来修改此配置,该参数位于 TektonConfig
自定义资源的 pipelinesAsCode.settings
规范中。