How to retrieve current workspace using Jenkins Pipeline Groovy script?

后端 未结 7 1113
独厮守ぢ
独厮守ぢ 2020-12-29 20:03

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

node(\'master\') {
    // PULL IN ENVIRONMENT VARIABLES
    // J         


        
相关标签:
7条回答
  • 2020-12-29 20:39

    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')
    
    0 讨论(0)
  • 2020-12-29 20:46

    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()
    
    0 讨论(0)
  • 2020-12-29 20:47

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

    node {
        def PWD = pwd();
        ...
    }
    
    0 讨论(0)
  • 2020-12-29 20:49

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

    0 讨论(0)
  • 2020-12-29 20:52

    I have successfully used as shown below in Jenkinsfile:

    steps {
      script {
        def reportPath = "${WORKSPACE}/target/report"
        ...
      }
    }
    
    0 讨论(0)
  • 2020-12-29 20:57

    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'
    }
    
    0 讨论(0)
提交回复
热议问题