Search code examples
if-statementjenkins-pipeline

Jenkins Pipeline script environment if condition


I'm trying to add if condition for environment variables in Jenkins Pipeline script

This is the code I wrote

            environment {
                if (BRANCH == 'production' && $REGION == "us-east-1" ) {
                    CRED = "username"
                    }
                else if (BRANCH == 'production'){
                    CRED = "Username"
                    }
                else {
                    CRED = "user"
                      }
            } 
steps 
            {   
                Credentials([
                    usernamePassword(credentialsId: env.CRED
}````


I'm getting following error. Not sure what needs to be changed


No variables specified for environment 

Solution

  • You can't use script in environment clause, and if clause is script.

    But you can use this:

    environment {
        CRED = (BRANCH == 'production') ? (REGION == "us-east-1" ? "Username" : "username") : "User"
    }