通过 git commit 阻止 Jenkins 作业启动其他作业

Prevent Jenkins job from launching additional jobs with git commit

我正在尝试创建一个 Jenkins 管道,它在较高级别执行以下步骤:

  1. 发送构建通知
  2. 运行 lint 测试
  3. 运行 CI 测试
  4. 更新几个文件中的版本信息
  5. 使用git提交对文件的更新、创建标签并将更改推送到源
  6. 将项目中的文件上传到其他系统
  7. 发送通知

我希望此管道在特定分支发生提交时执行。我现在有这个工作,但问题是当作业在构建期间提交新更改时(在上面的第 5 步中),它启动一个新构建并基本上进入无限循环。

我知道这是设计使然,但有没有办法阻止新的构建作业执行?我可以在 Jenkins 管道中做一些事情来防止新提交启动新的 Jenkins 作业吗,或者这是否需要整个工作流程的返工?

您可以使用 generic-webhook-plugin,

例如,Jenkins 中的 GitHub webhooks 用于在开发人员向分支提交内容时触发构建,在每个 webhook 中我们都有以下信息

git repository name
branch which was changed
commit id
commit message
commit author
etc ...

避免循环

  • 将 Jenkins 提交 ID 保存到文件并将文件添加到 git 忽略
  • 使用通用 webhook 触发器从推送中读取远程提交 ID
  • 比较本地文件提交 ID 与远程提交 ID
  • 如果相同则表示来自 Jenkins 的提交,如果不相同则表示提交不是来自 Jenkins

这是可能有帮助或相应更改的代码片段,它不会创建外观

#!/bin/bash
webhook_commit_id=$commit
commit_by_jenkins=commit_by_jenkins.txt
if [ ! -f $commit_by_jenkins ]
then
    echo "creating local file name commit_by_jenkins.txt, please add this file to git ignore"
    touch commit_by_jenkins.txt
fi
jenkins_commit=`cat commit_by_jenkins.txt`
if [ "${webhook_commit_id}" == "${jenkins_commit}" ]; then 
    echo "commit by jekins server, ignoring commit"
else
      echo "commiting code from jenkins servver"
      git add -A && git commit -m "commit by jenkins server" && git rev-parse HEAD > commit_by_jenkins.txt
fi