Search code examples
jenkinsenvironment-variablesjenkins-pipelinejenkins-pluginsextended-choice-parameter

Use Jenkins WORKSPACE environment variable in Jenkins declarative pipeline parameter


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.


Solution

  • 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
                    }
                }
            }
        }
    }