Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve current workspace using Jenkins Pipeline Groovy script?

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 
like image 636
N.Reddy Avatar asked Jun 15 '16 21:06

N.Reddy


People also ask

How do I find the workspace URL in Jenkins pipeline?

If you have this plugin pipeline-stage-view-plugin, you can try opening: https://<Your Jenkins hostLport>/job/<job-name>/<build number>/execution/node/3/wfapi/ . You should see "Allocate node : Start" as the step name.


2 Answers

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

like image 133
Litty Philip Avatar answered Sep 26 '22 00:09

Litty Philip


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() 
like image 45
Krzysztof Krasoń Avatar answered Sep 23 '22 00:09

Krzysztof Krasoń