在Jenkins的重复流水线的不同阶段重用对象/属性

时间:2017-11-07 10:17:19

标签: maven jenkins groovy jenkins-pipeline artifactory

我们创建了一个新的maven构建:

def rtMaven = Artifactory.newMavenBuild()

现在我们想要在与当前阶段不同的阶段重用这个rtMaven; 比如下面的代码:

pipeline {
agent any

...
stages {

    stage('stage1') {
        steps {
            script {
                def rtMaven = Artifactory.newMavenBuild()
            }
    }

    stage('stage2') {
         steps {
            script {
                //REUSE rtMaven (now it's unknown)
            }
         }

     }
}

是否可以重复使用rtMaven而不在第二阶段重新定义它?

现在我们遇到如下错误:

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

1 个答案:

答案 0 :(得分:3)

在全局范围内定义var

def rtMaven = ''
pipeline {
    agent any
    stages {
        stage('stage1') {
            steps {
                script {
                    rtMaven = Artifactory.newMavenBuild()
                }
            }
        }
    }
    stage('stage2') {
        steps {
            script {
                echo "$rtMaven"
            }
        }
    }
}