52

I am trying to get the current workspace of my Jenkins build using a Groovy pipeline script:

node('master') { // PULL IN ENVIRONMENT VARIABLES // Jenkins makes these variables available for each job it runs def buildNumber = env.BUILD_NUMBER def workspace = env.WORKSPACE def buildUrl = env.BUILD_URL // PRINT ENVIRONMENT TO JOB echo "workspace directory is ${workspace}" echo "build URL is ${env.BUILD_URL}" } 

It returns:

[Pipeline] Allocate node : Start Running on master in /Users/Shared/Jenkins/Home/jobs/test/workspace [Pipeline] node { [Pipeline] echo workspace directory is null [Pipeline] echo build URL is http://localhost:8080/job/test/5/ [Pipeline] } //node [Pipeline] Allocate node : End [Pipeline] End of Pipeline Finished: SUCCESS 
2

8 Answers 8

53

For me just ${WORKSPACE} worked without even initializing the variable workspace.

Sign up to request clarification or add additional context in comments.

2 Comments

me too but only with uppercase ${WORKSPACE} works, ${workspace} does NOT
${WORKSPACE} works for me too and not ${workspace}. ${workspace} is taken as class workspace
7

There is no variable included for that yet, so you have to use shell-out-read-file method:

sh 'pwd > workspace' workspace = readFile('workspace').trim() 

Or (if running on master node):

workspace = pwd() 

1 Comment

Don't forget to trim() your readFile() result, otherwise you will end up with a newline out of nowhere.
5

I think you can also execute the pwd() function on the particular node:

node { def PWD = pwd(); ... } 

Comments

5

A quick note for anyone who is using bat in the job and needs to access Workspace:

It won't work.

$WORKSPACE https://issues.jenkins-ci.org/browse/JENKINS-33511 as mentioned here only works with PowerShell. So your code should have powershell for execution

 stage('Verifying Workspace') { powershell label: '', script: 'dir $WORKSPACE' } 

1 Comment

this sounds as its only working that way on Windows systems.
4

I have successfully used as shown below in Jenkinsfile:

steps { script { def reportPath = "${WORKSPACE}/target/report" ... } } 

Comments

2

The key is that, this works if used within double quotes instead of single quotes, below is my code and this worked!

script { echo 'Entering Stage - Nexus Upload' def artefactPath = "${WORKSPACE}/build/deploy/identityiq.war" echo "printing the path ${artefactPath}" } 

1 Comment

Groovy does not interpolate strings in single quotes '${WORKSPACE}', only in double quotes: "${WORKSPACE}". Similarly it will interpolate strings in triple double quotes """${WORKSPACE}""", but not in triple single quotes '''${WORKSPACE}'''. See groovy-lang.org/syntax.html#_string_interpolation for more details.
0

This is where you can find the answer in the job-dsl-plugin code.

Basically you can do something like this:

readFileFromWorkspace('src/main/groovy/com/groovy/jenkins/scripts/enable_safehtml.groovy') 

Comments

0

In Jenkins pipeline script, I am using

targetDir = workspace 

Works perfect for me. No need to use ${WORKSPACE}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.