快速开始

Target Audience

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

Introduction

您将:

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

Estimated Reading Time

10-15 分钟

Prerequisites

  • PAC 已部署,并已对 Git provider 网络暴露。请参见 Manage PAC Component
  • 您已获取 PAC webhook URL。请参见 Get the PAC Webhook URL
  • 您知道 PAC namespace。默认值为 tekton-pipelines
  • 已为目标集群配置 kubectl
  • 已安装带有 pac 插件的 tkn CLI。请参见 tkn pac Command Reference
  • 您对 Git 仓库具有管理员或维护者访问权限。

Step 1: Configure Repository

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

Namespace Parameters
  • --pac-namespace 是 PAC 部署所在的 namespace。默认值为 tekton-pipelines
  • 用于 pipeline namespace 的交互式提示会在其中创建 RepositoryPipelineRun 资源。请在运行命令之前先创建该 namespace。

Confirm tkn pac Plugin

确认插件可用:

tkn pac version

示例输出:

0.39.2

Create GitLab Personal Access Token

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

Configure Repository with tkn pac

从 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. 输入来自 Get the 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 模板

Step 2: Create Your First 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> 替换为您的分支名称(例如,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/*]"

Step 3: Test the Pipeline

Trigger via Push

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

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

Trigger via 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。

Check Pipeline Status

查看该 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] 来自 Pipelines as Code 的问候!

Next Steps