Issue porting scripted to declarative withEnv{} to environment{}

9 views
Skip to first unread message

Bob TheBuilder

unread,
May 14, 2019, 3:27:53 PM5/14/19
to Jenkins Users
Hi,

I'm porting a scripted pipeline to a declarative one.

I used to have something like this:
//Scripted
def myEnv = [:]

stage ('Prepare my env') {
[...]
myEnv = ...
}

stage ('Fancy stuff') {
    node() {
        withEnv(myEnv} {
        // here use what is defined in myEnv
        }
    }    
    
stage ('Fancy stuff2') {
    node() {
        withEnv(myEnv} {
        // here use what is defined in myEnv
        }
}    

}

and now in declarative I would like to have

//Declarative
def myEnv = [:]
    
pipeline {

    agent none

    stage('Prepare my env') {
        steps {
            script {
                [...]
                myEnv = ...
            }
        }
    }

    stages {
        environment { myEnv }
        stage('Fancy stuff') {
            [...]
        }
        stage('Fancy stuff2') {
            [...]
        }
    }
}

when I try to run this, it fails withEnv
"org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: xx: "myEnv" is not a valid environment expression. Use "key = value" pairs with valid Java/shell keys.
"

Fair enough. 
What should I do to be able to use declarative environment { } to avoid using the declarative one in every further steps?

Thank you,

Bob.
Reply all
Reply to author
Forward
0 new messages