快速开始

目标读者

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

简介

你将:

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

预计阅读时间

10-15 分钟

先决条件

  • PAC 已部署并暴露给 Git 提供商网络。请参见 管理 PAC 组件
  • 你拥有 PAC webhook URL。请参见 获取 PAC Webhook URL
  • 你知道 PAC 命名空间。默认值为 tekton-pipelines
  • 已为目标集群配置 kubectl
  • 已安装带有 pac 插件的 tkn CLI。请参见 tkn pac 命令参考
  • 你对该 Git 仓库拥有管理员或维护者权限。

第 1 步:配置 Repository

以下步骤使用 tkn pac CLI 和 GitLab 作为示例。对于 GitHub 或基于 manifest 的设置,请参见 配置 GitHub Repository配置 GitLab Repository

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

确认 tkn pac 插件

确认该插件可用:

tkn pac version

示例输出:

0.39.2

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

创建 GitLab Personal Access Token

  1. 前往 GitLab → Settings → Access Tokens
  2. 创建一个具有 api 作用域的令牌
  3. 安全地保存该令牌

使用 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. 输入 pipeline 的命名空间,例如 project-pipelines。该命名空间必须已存在。
  3. 此时将创建 Repository CR
  4. 输入 GitLab 项目 ID(可在项目设置 → General 中找到)
  5. 输入来自 获取 PAC Webhook URL 的 PAC webhook URL
  6. 输入 webhook 密钥(或按 Enter 使用自动生成的默认值)
  7. 输入 GitLab 访问令牌(你创建的 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]"
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> 替换为你的分支名称(例如 mainmasterdevelop
  • 确保注解 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 注解中指定的分支以触发 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 状态

查看该命名空间中的 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!

下一步