快速开始

目标受众

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

简介

你将会:

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

预计阅读时间

10-15 分钟

前提条件

  • PAC 已部署并对 Git 提供方网络开放。请参阅 管理 PAC 组件。
  • 你已拥有 PAC webhook URL。请参阅 获取 PAC Webhook URL。
  • 你知道 PAC namespace。默认值为 tekton-pipelines。
  • kubectl 已针对目标集群完成配置。
  • 已安装带有 pac 插件的 tkn CLI。请参阅 tkn pac 命令参考。
  • 你对 Git 仓库具有管理员或维护者访问权限。

步骤 1:配置 Repository

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

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

确认 tkn pac 插件

确认插件可用:

tkn pac version

示例输出:

0.39.2

如果命令失败,请从 tkn pac 命令参考 安装该插件。

创建 GitLab 个人访问令牌

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

使用 tkn pac 配置 Repository

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

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

如果 PAC 部署在其他 namespace 中,请替换 tekton-pipelines。

按照交互式提示进行操作:

  1. 输入 Git 仓库 URL(会从当前目录自动检测,或手动输入)
  2. 输入 pipelines 的 namespace,例如 project-pipelines。该 namespace 必须已存在。
  3. 此时将创建 Repository CR
  4. 输入 GitLab project ID(在 project settings → General 中查找)
  5. 输入来自 获取 PAC Webhook URL 的 PAC webhook URL
  6. 输入 webhook secret(或按 Enter 使用自动生成的默认值)
  7. 输入 GitLab access token(你创建的 Personal Access Token)
  8. 输入 GitLab API URL(默认:https://gitlab.com,或输入你自托管的 GitLab URL)

该命令将会:

  • 在所选 namespace 中创建 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]"
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 触发

创建一个 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 中创建一个 Merge Request。

检查 Pipeline 状态

查看该 namespace 中的 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!

后续步骤