3

Is there a way to use Jenkins WORKSPACE environment variable in Jenkins declarative pipeline parameters?

Below attempt failed.

pipeline {
    parameters {
        extendedChoice description: 'Template in project',
                        multiSelectDelimiter: ',', name: 'TEMPLATE',
                        propertyFile:  env.WORKSPACE + '/templates.properties', 
                        quoteValue: false, saveJSONParameterToFile: false, type: 'PT_MULTI_LEVEL_SINGLE_SELECT',
                        value: 'Project,Template', visibleItemCount: 6
   ...
   } 
   stages { 
   ...
   }

propertyFile: '${WORKSPACE}/templates.properties' didn't work either.

rok
  • 9,403
  • 17
  • 70
  • 126

2 Answers2

7

The environment variable can be accessed in various place in Jenkinsfile like:

def workspace
node {
    workspace = env.WORKSPACE
}
pipeline {
    agent any;
    parameters {
        string(name: 'JENKINS_WORKSPACE', defaultValue: workspace, description: 'Jenkins WORKSPACE')
    }
    stages {
        stage('access env variable') {
            steps {
                // in groovy
                echo "${env.WORKSPACE}"
                
                //in shell
                sh 'echo $WORKSPACE'
                
                // in groovy script 
                script {
                    print env.WORKSPACE
                }
            }
        }
    }
}
Samit Kumar Patel
  • 1,932
  • 1
  • 13
  • 20
0

The only way that worked is putting absolute path to Jenkins master workspace where properties file is located.

pipeline {
    parameters {
        extendedChoice description: 'Template in project',
                        multiSelectDelimiter: ',', name: 'TEMPLATE',
                        propertyFile:  'absolute_path_to_master_workspace/templates.properties', 
                        quoteValue: false, saveJSONParameterToFile: false, type: 'PT_MULTI_LEVEL_SINGLE_SELECT',
                        value: 'Project,Template', visibleItemCount: 6
   ...
   } 
   stages { 
   ...
   }

It seems that environment variables are not available during pipeline parameters definition before the pipeline actually triggered.

rok
  • 9,403
  • 17
  • 70
  • 126