0

In my shared libraries, I define:

vars/checkoutSvnCode.groovy

#!/usr/bin/env groovy    
//get svn code    
def call(String URL="url") {
    def scmVars = checkout([
        $class: 'SubversionSCM',
        additionalCredentials: [],
        excludedCommitMessages: '',
        excludedRegions: '',
        excludedRevprop: '',
        excludedUsers: '',
        filterChangelog: false,
        ignoreDirPropChanges: false,
        includedRegions: '',
        locations: [[
            cancelProcessOnExternalsFail: true,
            credentialsId: 'svn_auth',
            depthOption: 'infinity',
            ignoreExternalsOption: true,
            local: '.',
            remote: "${URL}"
        ]],
        quietOperation: false,
        workspaceUpdater: [$class: 'UpdateUpdater']
    ])
    def REVISION = scmVars.SVN_REVISION
    println "//Revision:\"${REVISION}\""
}

In my pipeline, I define

#!/usr/bin/env groovy
@Library('jenkins-pipeline-library')_
import com.test.GlobalVars

    pipeline {    
        environment {
            def SVN_ADDR = "svn://code.test.com/myproject"
        }
        
        agent any
        
        stages {
            
            stage('getCode') {
                steps {
                    script {
                        checkoutSvnCode("${SVN_ADDR}")
                        println "//Revision:\"${REVISION }\""
                    }
                }
            }
    
        }
    }

now I get this error

groovy.lang.MissingPropertyException: No such property: REVISION for class: groovy.lang.Binding

how can I get the "REVISION" in my pipeline?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
sam
  • 1
  • 1
  • The `REVISION` variable is not declared or initialized in your Pipeline, so you cannot resolve its value. That is why the error is thrown. – Matthew Schuchard Jul 15 '20 at 12:28

4 Answers4

1

You have a space in the curley braces for your REVISION var ${REVISION } should be ${REVISION}

  • oh,is a syntax error, without this space, I still get the same error "No such property: REVISION" – sam Jul 15 '20 at 11:15
0

You define REVISION in the groovy library file, that gets called from your pipeline process and then terminates. I am no groovy expert, but the normal behaviour of a calling script calling a process that terminates is that all the values in the child process (library file) disappear when the sub-process completes. The problem seems to be that your pipeline process can't access the variable in your library file.

Have you thought of using the Jenkins Environment Variable: SVN_REVISION ?

${SVN_REVISION} should have it for you - can you try :

println "//Revision:\"${SVN_REVISION}\""

Else, it should be in the jenkins file, or, worst case scenario get it from shelling to the command:

svn info --show-item revision
0

You can also have your checkoutSvnCode DSL function RETURN the revision and then:

        stage('getCode') {
            steps {
                script {
                    def REVISION = checkoutSvnCode("${SVN_ADDR}")
                    println "//Revision:\"${REVISION}\""
                }
            }
        }
Patrice M.
  • 4,209
  • 2
  • 27
  • 36
0

Finally I solved this problem

vars/checkoutSvnCode.groovy

#!/usr/bin/env groovy    
//get svn code    
def call(String URL="url") {
    def scmVars = checkout([
        $class: 'SubversionSCM',
        additionalCredentials: [],
        excludedCommitMessages: '',
        excludedRegions: '',
        excludedRevprop: '',
        excludedUsers: '',
        filterChangelog: false,
        ignoreDirPropChanges: false,
        includedRegions: '',
        locations: [[
            cancelProcessOnExternalsFail: true,
            credentialsId: 'svn_auth',
            depthOption: 'infinity',
            ignoreExternalsOption: true,
            local: '.',
            remote: "${URL}"
        ]],
        quietOperation: false,
        workspaceUpdater: [$class: 'UpdateUpdater']
    ])
    return scmVars
}

In pipeline

#!/usr/bin/env groovy
@Library('jenkins-pipeline-library')_
import com.test.GlobalVars

    pipeline {    
        environment {
            def SVN_ADDR = "svn://code.test.com/myproject"
        }
        
        agent any
        
        stages {
            
            stage('getCode') {
                steps {
                    script {
                        def REVISION = checkoutSvnCode("${SVN_ADDR}").SVN_REVISION
                        println "//Revision:\"${REVISION }\""
                    }
                }
            }
    
        }
    }

Now I can get the SVN_REVISION successfully ^_^

//Revision:"39128"

sam
  • 1
  • 1