def generateVersion() {
def commitCount = sh(script: "git rev-list --count HEAD", returnStdout: true).trim() as Integer
echo "this is commitcount------------->>>>>>>>>>>>>>>> ${commitCount}";
def metadata = readJSON file: 'package.json'
def (major, minor) = metadata.version.tokenize('.')
def patch = commitCount
def prerelease = env.BRANCH_NAME == 'master' ? '' : "-${env.BRANCH_NAME}"
return "${major}.${minor}.${patch}${prerelease}"
}
This is a groovy code that I have written in my Jenkinsfile. It is supposed to return me a unique version of the build. This function gets called in a stage Publish Libraries.
....
stage('Publish Libraries') {
dir('External') {
libVersion = generateVersion()
...
...
I am not able to get the correct value of commitCount and therefore wrong value of patch. It stays consistent at value 5 no matter how many commits I make in my branch. I have created a branch off of another feature branch that initally had 56 commits. So when i created a branch it initally had those 56 commits. I added 11 commits of my own in the newly created branch so a total of 67 commits are there in the branch but it shows the count as only 5. What should I do?
I even tried:
def commitCount = sh(script: "git rev-list --count ${env.BRANCH_NAME}", returnStdout: true).trim() as Integer
thinking that maybe my HEAD gets set to some other branch without my knowledge. But still commitCount is 5.