快速开始

目标读者

本指南适用于希望将 Git 仓库连接到现有 PAC 安装并触发第一个 PipelineRun 的用户。

简介

你将会:

  • 使用 tkn pac 创建一个 PAC Repository 连接
  • 向仓库中添加一个简单的 PipelineRun 定义
  • 触发并检查首次运行

预计阅读时间

10-15 分钟

先决条件

  • PAC 已部署并暴露到 Git 提供程序网络。请参阅 管理 PAC Component。
  • 如果你使用 UI 创建或管理 PAC Repository 资源,管理员已启用 Repository UI 功能标志。请参阅 启用 Repository UI 功能。
  • pipelines-as-code-info ConfigMap 包含公开的 PAC controller-url。
  • 如果使用 tkn pac,你需要知道 PAC 命名空间。默认值为 tekton-pipelines。
  • 当自动注册被禁用或失败时,你需要 PAC webhook URL。请参阅 获取 PAC Webhook URL。
  • kubectl 已为目标集群完成配置。
  • 带有 pac 插件的 tkn CLI 已安装。请参阅 tkn pac 命令参考。
  • 你对 Git 仓库拥有管理员或维护者访问权限。

步骤 1:配置 Repository

下面的步骤以 tkn pac CLI 和 GitLab 为例。对于 GitHub 或基于 manifest 的设置,请参阅 配置 GitHub Repository 或 配置 GitLab Repository。

当 Repository 的 spec.git_provider.type 设置为 github 或 gitlab,并且设置了 spec.git_provider.webhook_secret,同时具备可用的 Repository 本地 provider token 或匹配的集中式 webhook 注册凭证时,webhook 注册 controller 会自动注册或更新 Git 提供程序 webhook。不支持的 spec.git_provider.type 的 Repository 会被跳过,原因会记录为 Repository Event 和 status annotations。

命名空间参数
  • --pac-namespace 是 PAC 部署所在的命名空间。默认值为 tekton-pipelines。
  • pipeline 命名空间的交互式提示用于创建 Repository 和 PipelineRun 资源。请在运行命令之前创建该命名空间。

确认 tkn pac 插件

确认该插件可用:

tkn pac version

示例输出:

0.39.2

创建 GitLab Personal Access Token

  1. 转到 GitLab → Settings → Access Tokens
  2. 创建一个带有 api scope 的 token
  3. 安全地保存该 token

使用 tkn pac 配置 Repository

请在 Git 仓库目录中运行该命令。.tekton 目录将会创建在当前工作目录中。

cd /path/to/your/gitlab/repo
tkn pac create repo --pac-namespace tekton-pipelines

如果 PAC 部署在其他命名空间中,请替换 tekton-pipelines。

按照交互式提示操作:

  1. 输入 Git 仓库 URL(会从当前目录自动检测,或者手动输入)
  2. 输入 pipelines 的命名空间,例如 project-pipelines。该命名空间必须已存在。
  3. 此时将创建 Repository CR
  4. 输入 GitLab project ID(可在 project settings → General 中找到)
  5. 如果提示,请输入 PAC webhook URL。在启用了自动注册的集群中,此 URL 从 pipelines-as-code-info 中读取。
  6. 输入 webhook secret(或按 Enter 使用自动生成的默认值)
  7. 输入 GitLab access token(你创建的 Personal Access Token)
  8. 输入 GitLab API URL(默认值:https://gitlab.com,或者输入你自托管的 GitLab URL)

该命令将会:

  • 在所选命名空间中创建 Repository 资源
  • 自动配置 GitLab webhook
  • 创建包含凭证的 Kubernetes Secret
  • 在你的仓库中生成 .tekton/pipelinerun.yaml 模板

步骤 2:创建你的第一个 Pipeline

tkn pac create repo 命令会在 .tekton/pipelinerun.yaml 中创建一个基础模板。编辑它以定义你的 pipeline:

apiVersion: tekton.dev/v1
kind: PipelineRun
metadata:
  name: simple-pipeline
  annotations:
    pipelinesascode.tekton.dev/on-target-branch: "[refs/heads/main]"
    pipelinesascode.tekton.dev/on-event: "[push, pull_request]"
spec:
  pipelineSpec:
    tasks:
    - name: hello
      taskSpec:
        steps:
        - name: echo
          image: alpine:latest
          script: |
            echo "Hello from Pipelines as Code!"

提交并推送到你的仓库:

git add .tekton/pipelinerun.yaml
git commit -m "Add PAC pipeline"
git push origin <your-branch-name>

注意:

  • 将 <your-branch-name> 替换为你的分支名称(例如 main、master 或 develop)
  • 确保注解 pipelinesascode.tekton.dev/on-target-branch 与你的分支名称匹配。例如,如果你的分支是 main,使用 "[refs/heads/main]";如果是 test,使用 "[refs/heads/test]"
  • 要匹配多个分支,请使用逗号分隔的值:"[main, develop]" 或 "[refs/heads/main,refs/heads/develop]"
  • 要匹配所有分支,请使用:"[refs/heads/*]"

步骤 3:测试 Pipeline

通过 Push 触发

向 pipeline 注解中指定的分支推送一个 commit,以触发 pipeline:

echo "test" >> README.md
git add README.md
git commit -m "Test pipeline trigger"
git push origin <your-branch-name>

通过 Merge Request 触发

创建一个合并请求以触发 pipeline:

git checkout -b feature/test
echo "feature" >> feature.txt
git add feature.txt
git commit -m "Add feature"
git push origin feature/test

然后在 GitLab 中创建一个合并请求。

检查 Pipeline 状态

查看该命名空间中的 PipelineRuns:

kubectl get pipelineruns -n project-pipelines

示例输出:

NAME                    STARTED        DURATION   STATUS
simple-pipeline-xxxxx   2 minutes ago  30s        Succeeded

查看 pipeline 日志:

# List all PipelineRuns
tkn pipelinerun list -n project-pipelines

示例输出:

NAME                    STARTED        DURATION   STATUS
simple-pipeline-xxxxx   2 minutes ago  30s        Succeeded
# View logs for a specific PipelineRun
tkn pipelinerun logs <pipelinerun-name> -n project-pipelines

示例输出:

[hello : echo] Hello from Pipelines as Code!

下一步