Jenkins has information about commits included in a particular build. I know this because Jenkins displays this (see the screenshot below).
How can I pull the information about commits included in the build during the CI execution?
This information is required to check what folders have files modified and, as a result, to determine what actions Jenkins should execute.
Thanks!
P.S. To achieve this in CI jobs for PRs, I use git command:
git rev-parse github/master
It returns the last commit made in master before current branch was branched off (to make it work I add 'github' remote). Obviously, it can't work in the 'master' branch itself. So I'm thinking, maybe Jenkins has some built-in information that I can use? Or some Jenkins plugins can help?
CodePudding user response:
Apparently, there is a bunch of links, though I could not find any until I figured the keyword to google for it.
1st of all, there is a DevOps version of StackOverflow: https://devops.stackexchange.com/ It might have more people active that can help with Jenkins related questions
2nd, the idea is to use jenkins variable currentBuild and further iterated through:
- currentBuild.previousBuild
- build.changeSets
- items in the build
- commit hashes are in the item.commitId
Links that helped me to figure out the solution:
- What commits are pulled by Jenkins in the build?
- https://devops.stackexchange.com/questions/2310/get-all-change-logs-of-since-last-successful-build-in-jenkins-pipeline?newreg=52d68c7ced584340988071cc72440ca1
- https://blog.csdn.net/liurizhou/article/details/88236397
- NotSerializableException thrown when accessing currentBuild.changeSets in Jenkins pipeline
- https://gitter.im/jenkinsci/jenkins - public chat with jenkins community, not super active, but still helpful.
IMPORTANT NOTES:
- Because of how Groovy works in Jenkins you need to put this logic into a separate function and add "@NonCPS" attribute to it
- All variables inside need to be prefixed by 'def' (even though my prettier told me this is not required), without this you will have "java.io.NotSerializableException: hudson.plugins.git.GitChangeSetList" errors.
Hope this will help someone as well!