I'm facing an error while trying to return multiple parameters from a function using if else in Jenkins pipeline script. Below is my test pipeline.
def envi, host
def build_func(envi, host){
if (env.ENVIRONMENT == 'dev') {
envi = 'dev'
host = 'devhost'
}
else if(env.ENVIRONMENT == 'prod') {
envi = 'prod'
host = 'prodhost'
}
return [envi, host]
}
pipeline {
agent any
environment {
def (envir,hostname) = build_func(env,host)
}
stages {
stage('Hello') {
steps {
echo "Hello World\n${envir}\n${env.ENVIRONMENT}"
}
}
}
}
When I build this, getting the below error,
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: WorkflowScript: 18: Expected string literal @ line 18, column 7. def (envir,hostname) = build_func(env,host)
CodePudding user response:
I don't think you can return multiple values and create Environment variables like that within an environment
block. As a workaround, maybe you can do something like this.
environment {
envir = "${build_func(env,host)[0]}"
hostname = "${build_func(env,host)[1]}"
}