Jenkins管道通过管道库获得“无此类属性”

时间:2020-07-15 10:22:55

标签: jenkins jenkins-pipeline jenkins-shared-libraries

在共享库中,我定义:

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

在我的管道中,我定义

#!/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 }\""
                    }
                }
            }
    
        }
    }

现在我收到此错误

groovy.lang.MissingPropertyException:无此类属性:REVISION for 类别:groovy.lang.Binding

如何在管道中获取“版本”?

4 个答案:

答案 0 :(得分:1)

您的REVISION变量$ {REVISION}应为$ {REVISION},在Curley括号中有一个空格

答案 1 :(得分:0)

您可以在groovy库文件中定义REVISION,该文件将从管道过程中调用,然后终止。我不是精通技巧的人,但是调用脚本终止进程的调用脚本的正常行为是子进程(库文件)中的所有值在子进程完成时都会消失。问题似乎是您的管道进程无法访问您的库文件中的变量。

您是否考虑过使用Jenkins环境变量:SVN_REVISION?

$ {SVN_REVISION}应该适合您-您可以尝试:

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

否则,它应该在jenkins文件中,或者,在最坏的情况下,将其从Shell转到命令中:

svn info --show-item revision

答案 2 :(得分:0)

您还可以使用checkoutSvnCode DSL功能返回修订版本,然后:

        stage('getCode') {
            steps {
                script {
                    def REVISION = checkoutSvnCode("${SVN_ADDR}")
                    println "//Revision:\"${REVISION}\""
                }
            }
        }

答案 3 :(得分:0)

最后我解决了这个问题

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
}

在管道中

#!/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 }\""
                    }
                }
            }
    
        }
    }

现在我可以成功获取SVN_REVISION ^ _ ^

//修订:“ 39128”