使用 Git 操作触发

面向普通用户

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

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

PAC 触发工作原理

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

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

有关 pipeline 定义和事件 annotations 的更多详情,请参见 维护 Pipeline 代码

测试 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

推送更多提交以再次触发 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 annotation 里定义命令。

检查 Pipeline 状态

查看 PipelineRun

检查 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>

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

后续步骤