Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-41446

ws step does not update WORKSPACE environment variable

    • Icon: Improvement Improvement
    • Resolution: Fixed
    • Icon: Trivial Trivial
    • None
    • Jenkins LTS ver. 2.32.1
      Nodes and Processes Plugin 2.8

      I consider this a bug, but feel free to reclassify.

      node {
          ws ('workspace/customDir') {
              println "workspace: ${env.WORKSPACE}"
          }
      }
      
      [Pipeline] node
      Running on <node> in E:\JenkinsRoot\workspace\Experiments\Test_Workspace
      [Pipeline] {
      [Pipeline] ws
      Running in E:\JenkinsRoot\workspace\customDir
      [Pipeline] {
      [Pipeline] echo
      workspace: E:\JenkinsRoot\workspace\Experiments\Test_Workspace
      [Pipeline] }
      [Pipeline] // ws
      [Pipeline] }
      [Pipeline] // node
      [Pipeline] End of Pipeline
      Finished: SUCCESS
      

      I can't find a way, in code, to get the path to the workspace after using the ws step. At the moment the only way I've found is to run a batch or shell script and get the current working directory from there.

      Please tell me if I'm missing something

          [JENKINS-41446] ws step does not update WORKSPACE environment variable

          Bertrand Latinville added a comment - - edited

          I was about to open this issue, I faced it yesterday.

          I had a look in the code of WorkspaceStepExecution.java and tried to inject the new workspace path value in the environment, with
          this commit:

          https://github.com/blatinville/workflow-durable-task-step-plugin/commit/80886255fb283f272a1726001a37989185f7a9e4

          Using this pipeline:

          stage ("Test Workspace") {
              node ('linux') {
                  sh "echo node=\${NODE_NAME},  WORKSPACE=\${WORKSPACE}"
                  ws('foo')
                  {
                      sh "echo node=\${NODE_NAME},  WORKSPACE=\${WORKSPACE}"
                  
                  }
              }
          }
          

          WORKSPACE is correct after, BUT, NODE_NAME is set to null in the ws() step.
          Need to find something better.

          Bertrand Latinville added a comment - - edited I was about to open this issue, I faced it yesterday. I had a look in the code of WorkspaceStepExecution.java and tried to inject the new workspace path value in the environment, with this commit: https://github.com/blatinville/workflow-durable-task-step-plugin/commit/80886255fb283f272a1726001a37989185f7a9e4 Using this pipeline: stage ( "Test Workspace" ) { node ( 'linux' ) { sh "echo node=\${NODE_NAME}, WORKSPACE=\${WORKSPACE}" ws( 'foo' ) { sh "echo node=\${NODE_NAME}, WORKSPACE=\${WORKSPACE}" } } } WORKSPACE is correct after, BUT, NODE_NAME is set to null in the ws() step. Need to find something better.

          Jesse Glick added a comment -

          Would make sense to do it. blatinville I think you meant to use EnvironmentExpander.

          Anyway $WORKSPACE is just there for conceptual compatibility with freestyle projects. Definitely would not want to set it inside the dir step. Use pwd() if you want to know the current working directory.

          Jesse Glick added a comment - Would make sense to do it. blatinville I think you meant to use EnvironmentExpander . Anyway $WORKSPACE is just there for conceptual compatibility with freestyle projects. Definitely would not want to set it inside the dir step. Use pwd() if you want to know the current working directory.

          Our shell scripts, use the $WORKSPACE variable, so it is better so have it set properly by default, to use pipeline without modifications.

          Associated PR : https://github.com/jenkinsci/workflow-durable-task-step-plugin/pull/29

          Bertrand Latinville added a comment - Our shell scripts, use the $WORKSPACE variable, so it is better so have it set properly by default, to use pipeline without modifications. Associated PR : https://github.com/jenkinsci/workflow-durable-task-step-plugin/pull/29

            blatinville Bertrand Latinville
            philmcardlecg Phil McArdle
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: