I’ve tried to use different agent for different environments (dev/prod) using if-else inside agent directive. But I’m getting errors if I use the below pipeline script. Any help is much appreciated!!
pipeline {
agent {
if (env.ENVIRONMENT == 'prod') {
label {
label "EC2-1"
customWorkspace "/home/ubuntu/eks-prod-backend/"
}
}
else if (env.ENVIRONMENT == 'dev') {
label {
label "EC2-2"
customWorkspace "/home/ubuntu/eks-dev-backend/"
}
}
}
}
2
Answers
Maybe something like this could help you in case you have only two environments ?
Otherwise, you can check this thread, this will perhaps help you.
This is the approach I would suggest. Define a variable before the "pipeline" block, for example:
Then, just use it like this:
}
I am not sure if label inside label is needed, but you hopefully get the point. Use variables specified before the pipeline execution.