94

I'm currently doing some evaluation on the Jenkins Pipeline plugin (formerly know as Workflow plugin). Reading the documentation I found out that I currently cannot retriev the workspace path using env.WORKSPACE:

The following variables are currently unavailable inside a workflow script:

NODE_LABELS

WORKSPACE

SCM-specific variables such as SVN_REVISION

Is there any other way how to get the absolute path to the current workspace? I need this running some test which in turn gets some parameter (absolute path to some executable file). I already tried using new File("").absolutePath() inside a @NonCPS section but looks like the non-CPS stuff gets always executed on the master.

Does anybody have a clue how to get this path without running some batch script which stores the path into some file which later on can be read in again?

Community
  • 1
  • 1
Joerg S
  • 4,730
  • 3
  • 24
  • 43

4 Answers4

77

Since version 2.5 of the Pipeline Nodes and Processes Plugin (a component of the Pipeline plugin, installed by default), the WORKSPACE environment variable is available again. This version was released on 2016-09-23, so it should be available on all up-to-date Jenkins instances.

Example

node('label'){
    // now you are on slave labeled with 'label'
    def workspace = WORKSPACE
    // ${workspace} will now contain an absolute path to job workspace on slave

    workspace = env.WORKSPACE
    // ${workspace} will still contain an absolute path to job workspace on slave

    // When using a GString at least later Jenkins versions could only handle the env.WORKSPACE variant:
    echo "Current workspace is ${env.WORKSPACE}"

    // the current Jenkins instances will support the short syntax, too:
    echo "Current workspace is $WORKSPACE"

}
Joerg S
  • 4,730
  • 3
  • 24
  • 43
Jan Fabry
  • 7,221
  • 2
  • 36
  • 41
  • 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. Answer for reference - https://stackoverflow.com/a/57089239/4269078 – Ismail Jul 18 '19 at 07:36
  • 1
    Please see my answer below how to access WORKSPACE as property of the pipeline itself on Jenkins 2.222.3, Build Pipeline Plugin 1.5.8, Pipeline: Nodes and Processes 2.35: (https://stackoverflow.com/a/62216903/1620597) – Tom Fink Jun 05 '20 at 13:51
60

Note: this solution works only if the slaves have the same directory structure as the master. pwd() will return the workspace directory on the master due to JENKINS-33511.

I used to do it using pwd() functionality of pipeline plugin. So, if you need to get a workspace on slave, you may do smth like this:

node('label'){
    //now you are on slave labeled with 'label'
    def workspace = pwd()
    //${workspace} will now contain an absolute path to job workspace on slave 
}
Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
Aleks
  • 1,301
  • 2
  • 12
  • 9
  • 3
    Damn. Probably need some glasses. Totally missed that there's pwd() available. That was exactly what I was looking for. Thanks! – Joerg S Apr 29 '16 at 12:50
  • And, yet, according to the discussion in [this ticket](https://issues.jenkins-ci.org/browse/JENKINS-33511), `pwd()` will return the work directory of the master, not the node you are on. – Daniel C. Sobral Jul 31 '16 at 00:57
  • 2
    A fact I just confirmed by myself. `pwd()` will _not_ work on slaves, unless they happen to have the same directory structure as the master. – Daniel C. Sobral Jul 31 '16 at 01:01
  • 1
    It seems the linked Jenkins ticket has been resolved? – giorgiosironi Dec 03 '18 at 11:38
22

"WORKSPACE" environment variable works for the latest version of Jenkins Pipeline. You can use this in your Jenkins file: "${env.WORKSPACE}"

Sample use below:

def files = findFiles glob: '**/reports/*.json'
for (def i=0; i<files.length; i++) {
jsonFilePath = "${files[i].path}"       
jsonPath = "${env.WORKSPACE}" + "/" + jsonFilePath
echo jsonPath

hope that helps!!

anuj0901
  • 573
  • 6
  • 8
  • 1
    Please note that the accepted answer as well mentions that the `WORKSPACE` variable now is available: https://stackoverflow.com/a/43137597/4279361 – Joerg S Sep 20 '18 at 13:24
  • 2
    I looked into that answer but it did not have a code sample. So I still had to do some research before implementation. I don't mind the down vote but I hope my answer helps someone immediately by just copying the syntax. – anuj0901 Sep 21 '18 at 20:56
  • 3
    @anuj0901 I totally agree, answers with code samples should be the accepted answers, not text only answers. Thank you for the additional content. – Miguel Mesquita Alfaiate Mar 25 '20 at 13:18
1

For me WORKSPACE was a valid property of the pipeline itself. So when I handed over this to a Groovy method as parameter context from the pipeline script itself, I was able to access the correct value using "... ${context.WORKSPACE} ..."

(on Jenkins 2.222.3, Build Pipeline Plugin 1.5.8, Pipeline: Nodes and Processes 2.35)

Tom Fink
  • 532
  • 5
  • 17
  • 1
    Thanks for the note. Would you see a benefit to favor `context.WORKSPACE` over `env.WORKSPACE`? Is there some documentation available about the `context` object? – Joerg S Jun 16 '20 at 14:54