8

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.

StephenKing
  • 36,187
  • 11
  • 83
  • 112
sweetfeet
  • 121
  • 1
  • 1
  • 7

6 Answers6

8

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
tchambers
  • 456
  • 3
  • 5
  • 1
    Can I get the SVN revision number without getting / download the code? – Jbisgood9999999 Nov 16 '18 at 01:20
  • 1
    Does this assignment also work in Declarative syntax (under a `pipeline`, and not `node`)? I kept getting parsing errors on `def` – OzgurH Apr 25 '19 at 20:56
  • Never mind! I needed to put things in a `script { .. }` block to be able to "escape" to scripted pipeline syntax ([doc](https://jenkins.io/doc/book/pipeline/syntax/#script))... Also, note that as @Mikhail 's answer below elaborated, if you're checking out from multiple URLs, the revision number accessor will be number suffixed. – OzgurH Apr 25 '19 at 21:23
  • 1
    Is this still working? My console displays one revision number and the SVN_REVISION don't match. def checkoutResults = checkout([$class: 'SubversionSCM', ... ) echo "Full checkoutResults: " + checkoutResults def versionNumber = checkoutResults.SVN_REVISION echo manager.build.getDisplayName() + " " + versionNumber checkout log ex: Updating https://myrepo/path at revision '2019-04-30T11:06:10.962 -0400' Using sole credentials ... in realm ‘ VisualSVN Server’ At revision 25043 – Sean Apr 30 '19 at 15:19
  • 1
    @AlexWeitz I did eventually get this to work for me by changing the syntax to this... def checkoutResults = checkout([$class: 'SubversionSCM' ...snip...) manager.build.setDisplayName(manager.build.getDisplayName() + " " + checkoutResults['SVN_REVISION']) – Sean Dec 17 '19 at 15:33
  • Please note that this information is at present (June 2020) not 100% reliable. In our setup we see cases where the SVN_REVISION reported is not the actual revision that was checked out. This is in combination with svn:externals and shared libraries. It might be related to https://issues.jenkins-ci.org/browse/JENKINS-45489. – Bram Jun 23 '20 at 16:31
5

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']
Mikhail
  • 1,223
  • 14
  • 25
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.

sweetfeet
  • 121
  • 1
  • 1
  • 7
2

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

Lionel Orellana
  • 490
  • 1
  • 6
  • 12
-1

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

Community
  • 1
  • 1
ivoruJavaBoy
  • 1,307
  • 2
  • 19
  • 39
  • 1
    @ivoryJavaBoy unfortunately your first link provided is faulty. Searched on google but similar links are not resolvable. The second link I have seen but attempting to use that variable did not provide a solution. It always resolves to null – sweetfeet Aug 25 '16 at 23:26
  • sweetfeet i corrected the first link... about the variable i did not try it but knowing there are a lot of unknown Jenkins variable, i supposed it worked. By the way as i told you.. the groovy one it's just an example, i would do a groovy simple script the perform and svn info on the repo to get the repository...quick and easy... – ivoruJavaBoy Aug 26 '16 at 08:26
  • Thanks @ivoruJavaBoy – sweetfeet Aug 26 '16 at 08:40
  • Unfortunately one falls foul of security restrictions. Unable to use "".execute() as this is a static method. Cant use the ProcessBuilder constructor to get a process because your are not allowed to use the constructor with the list argument. Will have no hair left soon. – sweetfeet Aug 29 '16 at 03:38