防止 Jenkins 作业使用 git 提交启动其他作业



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

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

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

我知道这现在正在设计上工作,但是有没有办法防止执行新的构建作业?我可以在 Jenkins 管道中做一些事情来阻止新提交启动新的 Jenkins 作业,还是需要对工作流进行整个返工?

你可以使用 generic-webhook-plugin,

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

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

避免循环

  • 将 Jenkins 提交 ID 保存到文件并将文件添加到 git 忽略
  • 使用通用 Webhook 触发器从推送中读取远程提交 ID
  • 将本地文件提交 ID 与远程提交 ID 进行比较
  • 如果相同,则表示来自 Jenkins 的 mean 提交,如果不是,则表示 commit 不是来自 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

最新更新