使用 Git 操作触发

对于普通用户

本指南提供了通过 Git 操作触发 PAC 流水线的 Git 命令示例。

本指南展示了通过 Git 提供商事件触发 PAC 流水线的常用 Git 命令。

PAC 触发机制说明

PAC 使用来自 Git 提供商(GitHub、GitLab、Bitbucket 等)的 webhook 来检测事件:

  1. Webhook 配置:创建 Repository CR 时,PAC 会自动在你的 Git 提供商中配置 webhook
  2. 事件检测:当发生事件(push、pull request、评论)时,Git 提供商会向 PAC 控制器发送 webhook
  3. 流水线匹配:PAC 控制器根据 PipelineRun 文件中的注解匹配事件
  4. 流水线执行:匹配成功后,PAC 会在集群中创建 PipelineRun

有关流水线定义和事件注解的更多详情,请参见 Maintain Pipeline Code

目录

测试 Push 触发

基本 Push

修改内容并 push 以触发流水线:

echo "test" >> test.txt
git add test.txt
git commit -m "Test push trigger"
git push origin main

Push 到指定分支

创建并 push 到 feature 分支:

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

空提交

无需代码变更即可触发流水线:

git commit --allow-empty -m "Trigger pipeline"
git push origin main

测试合并请求触发

创建合并请求

  1. 创建 feature 分支:

    git checkout -b feature/test-mr
    echo "feature" >> feature.txt
    git add feature.txt
    git commit -m "Add feature for MR test"
    git push origin feature/test-mr
  2. 通过你的 Git 提供商 UI 创建针对 main 分支的 Pull/Merge Request

更新合并请求

推送更多提交以再次触发流水线:

echo "update" >> feature.txt
git add feature.txt
git commit -m "Update feature"
git push origin feature/test-mr

强制推送合并请求

重写历史并强制推送:

git commit --amend --no-edit
git push --force-with-lease origin feature/test-mr

注意:使用 --force-with-lease 替代 --force 以防止覆盖他人工作。

使用评论命令

评论命令仅在合并请求(Pull Requests)中有效,普通提交或 push 事件不支持。

通过评论触发

  1. 在 Git 提供商 UI 中打开你的 Pull/Merge Request

  2. 添加包含命令的评论:

    /retest

/test
  1. PAC 会检测该评论并触发相应流水线

常用评论命令

命令说明
/retest重新运行所有失败的测试
/test运行测试
/ok-to-test批准运行测试(如有需要)
/cancel取消正在运行的流水线

注意:具体命令取决于你的流水线配置。你可以在 PipelineRun 文件的 on-comment 注解中定义命令。

查看流水线状态

查看 PipelineRuns

检查流水线是否被触发:

kubectl get pipelineruns -n <namespace>

查看流水线日志

查看指定 PipelineRun 的日志:

tkn pipelinerun logs <pipelinerun-name> -n <namespace>

或实时跟踪日志:

tkn pipelinerun logs <pipelinerun-name> -n <namespace> -f

查看最新 PipelineRun

获取最新的 PipelineRun:

kubectl get pipelineruns -n <namespace> --sort-by=.metadata.creationTimestamp | tail -1

Git 操作故障排查

查看当前分支

确认当前所在分支:

git branch --show-current

查看最近一次提交

检查最近一次提交详情:

git log --oneline -1

查看远程分支

确认远程是否存在该分支:

git branch -r | grep <branch-name>

查看 Git 远程地址

检查配置的远程 URL:

git remote -v

强制同步远程分支

将本地分支重置为远程分支状态:

git fetch origin
git reset --hard origin/<branch-name>

警告:此操作会丢弃本地更改。

后续步骤