Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 775
rok Avatar asked Oct 28 '25 08:10

rok


1 Answers

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
                }
            }
        }
    }
}
like image 96
Samit Kumar Patel Avatar answered Oct 31 '25 10:10

Samit Kumar Patel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!