简体   繁体   English

通过 git 提交防止 Jenkins 作业启动其他作业

[英]Prevent Jenkins job from launching additional jobs with git commit

I'm attempting to create a Jenkins pipeline which does the following steps at a high level:我正在尝试创建一个 Jenkins 管道,它在高级别执行以下步骤:

  1. Send build notifications发送构建通知
  2. Run lint tests运行 lint 测试
  3. Run CI tests运行 CI 测试
  4. Update version information in a couple of files更新几个文件中的版本信息
  5. Use git to commit the updates to the files, create tags, and push changes to origin使用 git 提交对文件的更新,创建标签,并将更改推送到源
  6. Upload files in project to another system将项目中的文件上传到另一个系统
  7. Send notifications发送通知

I want this pipeline to execute when a commit happens to specific branches.我希望在特定分支发生提交时执行此管道。 I have this working now, but the issue is when the job commits the new changes during the build (in step 5 above), it launches a new build and essentially enters an infinite loop.我现在有这个工作,但问题是当作业在构建过程中提交新的更改(在上面的步骤 5 中)时,它会启动一个新的构建并基本上进入一个无限循环。

I know this is working by design right now, but is there anyway way to prevent a new build job from executing?我知道这现在是按设计工作的,但是有没有办法阻止新的构建作业执行? Can I do something within the Jenkins pipeline to prevent the new commit from launching a new Jenkins job, or would this require a whole rework of the workflow?我可以在 Jenkins 管道中做一些事情,以防止新的提交启动新的 Jenkins 作业,还是需要对工作流程进行整个返工?

You can use generic-webhook-plugin ,您可以使用generic-webhook-plugin

For instance, GitHub webhooks in Jenkins are used to trigger the build whenever a developer commits something to the branch, in each webhook we have following info例如,Jenkins 中的 GitHub webhook 用于在开发人员向分支提交某些内容时触发构建,在每个 webhook 中我们都有以下信息

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

To avoid loop避免循环

  • save Jenkins commit ID to file and add the file to git ignore将 Jenkins 提交 ID 保存到文件并将文件添加到 git 忽略
  • read remote commit ID from push using generic webhook trigger使用通用 webhook 触发器从推送中读取远程提交 ID
  • compare local file commit ID with Remote commit ID比较本地文件提交 ID 和远程提交 ID
  • If same it's mean commit from Jenkins, if not it's mean commit is not from Jenkins如果相同,则表示来自 Jenkins 的平均提交,如果不是,则表示平均提交不是来自 Jenkins

Here is the snippet that may help or change accordingly, it will not create look这是可能会有所帮助或相应更改的片段,它不会创建外观

#!/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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM