简体   繁体   English

如何在 jenkins 多分支脚本管道中获取最新的 git 提交作者姓名或消息并在分支条件下使用

[英]How to get latest git commit author name or message in jenkins multibranch scripted pipeline and use under branch condition

We use github organisation scan job in jenkins to auto scan all the related repos in the org based on certain name filters and auto triggers builds for specified branch regex.我们使用 jenkins 中的 github 组织扫描作业,根据某些名称过滤器和自动触发指定分支正则表达式的构建,自动扫描组织中的所有相关存储库。 The pipeline works great and saves a ton of time having to manually configure jobs for each repo.该管道运行良好,节省了大量时间,不得不为每个 repo 手动配置作业。 All of these repos utilize maven build to built the snapshot and release artifacts.所有这些存储库都使用 maven 构建来构建快照和发布工件。 Since we are trying to automate everything including the release process, what we have now done is to have a single externalized jenkinsfile (scripted) that these repos utilize and that file has all conditions for different branches like dev, uat, release and master.由于我们正在尝试自动化包括发布过程在内的所有内容,因此我们现在所做的是拥有一个外部化的 jenkinsfile(脚本),这些 repos 使用并且该文件具有不同分支(如 dev、uat、release 和 master)的所有条件。 The approach is code development begins in dev with a snapshot version and continues to deploy that until the release day is near and that point of time merge dev to release through PR, etc and that kicks off an automated release build under for the release branch for that repo job in jenkins.方法是代码开发从开发人员开始,使用快照版本,并继续部署,直到发布日临近,并且该时间点将开发人员合并到通过 PR 等发布,并为发布分支启动自动发布构建jenkins 中的那个回购工作。 Release branch build basically uses maven release plugin to first edit the pom in the remote repository by removing -SNAPSHOT for the artifact version, then doing a deployment to artifactory and then again committing back to the github repository with the increment version in pom.xml in the same release branch. Release branch build basically uses maven release plugin to first edit the pom in the remote repository by removing -SNAPSHOT for the artifact version, then doing a deployment to artifactory and then again committing back to the github repository with the increment version in pom.xml in相同的发布分支。 I have added an extra step in the jenkinsfile that pulls that latest release, merges to dev and then push back to remote dev so that dev has the correct increment SNAPSHOT version in pom.xml.我在 jenkinsfile 中添加了一个额外的步骤,用于提取最新版本,合并到 dev,然后推送回远程 dev,以便 dev 在 pom.xml 中具有正确的增量 SNAPSHOT 版本。 All of this works fine and does the job.所有这些都可以正常工作并且可以完成工作。 The issue is that when the release build kicks off and submits back two commits to the remote repo release branch (truncating -SNAPSHOT first and later adding incremented SNAPSHOT version in pom.xml), that in turns triggers another build which you have to manually stop else the loop will go endlessly.问题是,当发布构建开始并将两个提交提交回远程 repo 发布分支(首先截断 -SNAPSHOT,然后在 pom.xml 中添加递增的 SNAPSHOT 版本)时,这反过来会触发另一个构建,您必须手动停止否则循环将无休止地 go 。 Our single jenkinsfile has all branch checks like:-我们的单个 jenkinsfile 具有所有分支检查,例如:-

 if (env.BRANCH_NAME == 'release')

Now is there a way to add an and condition in that if statement that does something like:- if latest checked out commit not has author "xyz" or its message,~ "[maven-release-plugin]*" That would mean if those auto commits are made by maven release plugin then the moment the next build is triggered.现在有一种方法可以在 if 语句中添加一个和条件,它执行以下操作:- 如果最新签出的提交没有作者“xyz”或其消息,~“[maven-release-plugin]*”这意味着如果这些自动提交是由 maven 发布插件进行的,然后在触发下一个构建的那一刻。 it wouldn't do anything since the if condition for the branch and the above extra condition would invalidate it.它不会做任何事情,因为分支的 if 条件和上述额外条件会使它无效。 The question is how to achieve the above solution or what could be some similar alternatives here as.问题是如何实现上述解决方案,或者这里有什么类似的替代方案。

I was able to achieve this using the below approach:-我能够使用以下方法实现这一点: -

 def author = sh(returnStdout: true, script: "git log -1 --pretty=format:'%an'").trim() echo "$author" if (env.BRANCH_NAME == 'release' && author != 'xyz')

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

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