I created a pipeline job and would like to get the svn version number to enable further downstream processing in a call to a shell script. I am using a pipeline script similar to the following:
node {
// Mark the code checkout 'stage'....
stage 'Checkout'
// Get some code from a SVM repository
checkout(
[
$class: 'SubversionSCM',
additionalCredentials: [],
excludedCommitMessages: '',
excludedRegions: '',
excludedRevprop: '',
excludedUsers: '',
filterChangelog: false,
ignoreDirPropChanges: false,
includedRegions: '',
locations: [
[
...
]
],
workspaceUpdater: [$class: 'UpdateUpdater']
]
)
def svnversionnumber=${SVN_VERSION}
sh "/.../someshellscript ${svnversionnumber};"
}
Is there documentation on the checkout function available? Is it possible to get hold of the svn revision number? I can see that the revision is output to the log.
I had the same issue, but you can solve it by using the map that is returned from calling SCM checkout. It contains a value for SVN_REVISION.
// Get some code from a SVM repository
def scmVars = checkout(
...
)
def svnversionnumber = scmVars.SVN_REVISION
I ended up invoking a shell to get the svn revision number as follows
def svnVersionNumber = sh(
script: "svn info --show-item last-changed-revision $url",
returnStdout: true
)
This was the only way I could get it to work correctly.
In Groovy pipeline script it's possible to get results of checkout scm
command into TreeMap variable and then get what you need:
def checkoutResults = checkout([
poll: false,
scm: [
$class: 'SubversionSCM',
...
]
])
echo 'checkout results' + checkoutResults.toString()
echo 'checkout revision' + checkoutResults['SVN_REVISION']
echo 'checkout revision' + checkoutResults['SVN_REVISION_1']
echo 'checkout revision' + checkoutResults['SVN_REVISION_2']
There is a file called revision.txt in the build dir. The SubversionSCM provides methods to read this file.
//Here remote returns url@revision but the revision part is across the entire repo
//We will use the url part to get the revision for our branch
def remote = scm.locations.first().remote
def url = remote.split('@').first()
//The revision file has the revision for our branch. Parse returns a map.
def revmap = scm.parseRevisionFile(currentBuild.rawBuild)
revmap[url]
The scm variable is available on Jenkinsfiles. If you are not using a Jenkinsfile you should be able to create the scm object and pass it into the checkout method.
this code work for me in jenkins pipeline:
String url = 'svn+ssh:...'
SVN_REVISION_IN = sh returnStdout: true, script: 'svn info --show-item last-changed-revision ' + url
currentBuild.displayName = "Rev: ${SVN_REVISION_IN}"
Jenkins environment variable SVN_REVISION provides that straight away
来源:https://stackoverflow.com/questions/39136459/how-to-get-svn-version-number-from-checkout-for-use-in-dsl