0

I would like to set the variable that will be available to all stages. Variable that depends on chosen parameter, something like this:

parameters {
      choice(name: 'Environment', choices: ['Dev', 'Stage'], description: 'Deploy to chosen environment')
  }

environment { 
       //set the config file which depends on params.Environment e.g.
       //case params.Environment of
       //   Dev -> CONFIG_FILE="deploy/file_1.conf"
       //   Stage -> CONFIG_FILE="deploy/other_file.conf"
  }

stages {
   stage('check-params') {   
     steps {
        sh "echo \"config file: ${CONFIG_FILE}\""
        }
     }
    stage('build-frontend') {   
     steps {
        sh "build-fronted.sh ${CONFIG_FILE}"
        }
     }
    stage('deploy-backend') {   
     steps {
        sh "deploy-backend.sh ${CONFIG_FILE}"
        }
     }

but according to the Pipeline Syntax it is not allowed (I get ERROR: Expected name=value pairs).

Does anyone know how can I achieve this without using scripts { ... } in every stage->step as described in this post?

alxndr
  • 117
  • 1
  • 5
  • If you set a variable in one stage, it will be available in subsequent stages. – Matthew Schuchard Sep 24 '19 at 17:37
  • Thanks @MattSchuchard. I fooled myself after reading docs, which says: `The environment directive specifies a sequence of key-value pairs which will be defined as environment variables for the all steps, or stage-specific steps, depending on where the environment directive is located within the Pipeline.` – alxndr Sep 25 '19 at 08:22

1 Answers1

3

You can have a init stage which will set correct variable depending on parameter, something like this:

parameters {
    choice(name: 'Environment', choices: ['Dev', 'Stage'], description: 'Deploy to chosen environment')
}

environment { 
       //set the config file which depends on params.Environment e.g.
       //case params.Environment of
       //   Dev -> CONFIG_FILE="deploy/file_1.conf"
       //   Stage -> CONFIG_FILE="deploy/other_file.conf"
}

stages {

    // ----------------------------
    stage('init-env-variables') {
        steps {
            script {
                switch(params.Environment) {
                    case "Dev":
                        env.setProperty('CONFIG_FILE', 'deploy/file_1.conf')
                        break;
                    case "Stage":
                        env.setProperty('CONFIG_FILE', 'deploy/other_file.conf')
                        break;
                }
            }
        }
    }
    // -----------------------

    stage('check-params') {   
        steps {
            sh "echo \"config file: ${CONFIG_FILE}\""
        }
    }
    stage('build-frontend') {   
        steps {
            sh "build-fronted.sh ${CONFIG_FILE}"
        }
    }
    stage('deploy-backend') {   
        steps {
            sh "deploy-backend.sh ${CONFIG_FILE}"
        }
    }

  • This is exactly what I was looking for. Thanks [Dumitru](https://stackoverflow.com/users/5539331/dumitru-gurjui)! – alxndr Sep 25 '19 at 08:27