Using GitVersion in a Jenkins pipeline

After several hours of pain trying to update the build displayName from the GitVersion output I came up with this:


node('windows') {
    stage 'Checkout'
        checkout scm

    stage 'Run GitVersion'
        bat 'git remote remove origin1' // For some reason the pipeline is adding duplicate remotes that GitVersion moans about
        currentBuild.displayName = (bat (script: '@echo off && GitVersion.exe /showvariable FullSemVer', returnStdout: true))

    stage 'Build'
        bat '.\\Build.cmd'
}

Let me know if you think there is a better way! 🙂

EDIT:

So after having a PR get it’s knickers in a twist after I force-pushed a rebased history to a fork of mine (I know, I’m naughty! :)) I came up with this instead. It removes the need to remove the origin1 remote as it pulls directly from the source repo instead of using the local clone:

stage('Run GitVersion') {
	  withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'GitHubCredentials', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD']]) {
	  	def git_url = bat (script: '@echo off && git remote get-url origin', returnStdout: true)
		currentBuild.displayName = bat (script: "@echo off && GitVersion.exe /showvariable FullSemVer /url ${git_url.trim()} /b ${env.BRANCH_NAME}  /u ${env.GIT_USERNAME} /p ${env.GIT_PASSWORD}", returnStdout: true)
	  }
	}

The credentialsId “GitHubCredentials” is a credential setup in Jenkins, just change to whatever yours is (which may be a guid if you let Jenkins auto-generate one for you).