Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get absolute path to workspace directory in Jenkins Pipeline plugin

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?

like image 483
Joerg S Avatar asked Apr 29 '16 09:04

Joerg S


People also ask

What is workspace folder in Jenkins?

The workspace directory is where Jenkins builds your project: it contains the source code Jenkins checks out, plus any files generated by the build itself. This workspace is reused for each successive build.


2 Answers

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"

}
like image 146
Jan Fabry Avatar answered Oct 06 '22 22:10

Jan Fabry


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 
}
like image 60
Aleks Avatar answered Oct 06 '22 22:10

Aleks