通过 Git 操作触发

面向普通用户

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

本指南展示了通过 Git provider 事件触发 PAC pipeline 的常用 Git 命令。

PAC 触发工作原理

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

  1. Webhook 配置:当您创建 Repository CR 时,PAC 会自动在您的 Git provider 中配置 webhook
  2. 事件检测:当事件发生时(push、pull request、comments),Git provider 会向 PAC controller 发送 webhook
  3. pipeline 匹配:PAC controller 将事件与您的 PipelineRun 文件中的注解进行匹配
  4. pipeline 执行:如果找到匹配项,PAC 会在您的集群中创建一个 PipelineRun

有关 pipeline 定义和事件注解的更多详情,请参阅 Maintain Pipeline Code

测试 push 触发

基本 push

进行更改并 push 以触发 pipeline:

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

空提交

无需代码更改即可触发 pipeline:

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

测试 Merge Request 触发

创建 Merge Request

  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 provider UI 创建一个目标为 main 分支的 Pull/Merge Request

更新 Merge Request

继续 push 更多提交以再次触发 pipeline:

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

对 Merge Request 执行强制 push

重写历史并强制 push:

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

注意:请使用 --force-with-lease 而不是 --force,以防止覆盖他人的工作。

使用评论命令

评论命令仅适用于 Merge Requests(Pull Requests),不适用于常规提交或 push 事件。

通过评论触发

  1. 在 Git provider UI 中打开您的 Pull/Merge Request

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

    /retest

/test
  1. PAC 将检测该评论并触发相应的 pipeline

常见评论命令

CommandDescription
/retest重新运行所有失败的测试
/test运行测试
/ok-to-test批准运行测试(如果需要)
/cancel取消正在运行的 pipeline

注意:具体命令取决于您的 pipeline 配置。您可以在 PipelineRun 文件中的 on-comment 注解里定义命令。

检查 pipeline 状态

查看 PipelineRuns

检查 pipeline 是否已被触发:

kubectl get pipelineruns -n <namespace>

查看 pipeline 日志

查看特定 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>

警告:这将丢弃本地更改。

后续步骤