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

会有一股神秘感。 提交于 2019-11-30 04:40:05

问题


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.


回答1:


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



回答2:


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']



回答3:


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.




回答4:


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.




回答5:


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



回答6:


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




回答7:


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

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