How to get svn version number from checkout for use in dsl

只谈情不闲聊 提交于 2019-11-30 22:06:04

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}"
ivoruJavaBoy

I think one of the best choice can be use a simple little "groovy console script" to get the revision number then put into a Jenkins variable..

Something like this to give you an idea: Link

Take also a look at this question: Link

Jenkins environment variable SVN_REVISION provides that straight away

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!