title: How to re-use previously created workspace across stages
body: <p>I am facing an issue where I have two stages defined in my pipeline that both are run on the same node and need to be run in the same workspace.</p>
  
  <p>The first of these stages runs on my master node initially, but towards the end of the steps defined has to unstash some files onto a different node.</p>
  
  <p>The second stage then just needs to continue on my master, and relies on some modules that were installed from the first stage.</p>
  
  <p>Here is my pipeline to better explain:</p>
  
  <pre class="lang-groovy prettyprint-override"><code>#!groovy
  pipeline {
    agent { label 'master' }
    stages {
      stage('Build') { // 1. Running on master in /var/lib/jenkins/workspace/_Pipelines_IACT-Jenkinsfile-UL3RGRZZQD3LOPY2FUEKN5XCY4ZZ6AGJVM24PLTO3OPL54KTJCEQ
        steps {
          sh '''
            npm install
            bower install
            gulp set-staging-node-env
            gulp prepare-staging-files
            gulp webpack
          '''
          stash includes: 'dist/**/*', name: 'builtSources'
          stash includes: 'config/**/*', name: 'appConfig'
          node('Protractor') { // 2. Running on vnccentos7 in /var/jenkins/workspace/_Pipelines_IACT-Jenkinsfile-UL3RGRZZQD3LOPY2FUEKN5XCY4ZZ6AGJVM24PLTO3OPL54KTJCEQ
            dir('/opt/foo/deploy/') {
              unstash 'builtSources'
              unstash 'appConfig'
            }
          }        
        }
      }
      stage('Unit Tests') {
        agent { label 'master' } // 3. Running on master in /var/lib/jenkins/workspace/_Pipelines_IACT-Jenkinsfile-UL3RGRZZQD3LOPY2FUEKN5XCY4ZZ6AGJVM24PLTO3OPL54KTJCEQ@2
        steps {
          parallel (
            "Jasmine": {
              sh 'gulp karma-tests-ci'
            },
             "Mocha": {
              sh 'gulp mocha-tests'
            }
          )
        }
      }
    }
  }
  </code></pre>
  
  <p>As you can see, I have added comments at the start of each stage\node used to show the jenkins output I see for what workspaces get allocated.</p>
  
  <p>The problem I am facing is that the Unit Tests stage fails as it tries to use some node modules it cannot find. These are present in the first workspace that gets created which is where I want this stage to continue using, so not using a new '@2' suffixed workspace.</p>
  
  <p>Is there a way to tell the Jenkins to preserve previously created workspaces in the pipeline?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I am guessing as I have specified agent <code>{label:'master'}</code> again in my next stage, this is what cases a new workspace to get created? Should I be using the <code>node</code> approach instead? Would that allow the same workspace to get used?</p>
  
  <p>I actually have tried using <code>node('master'){...}</code> around each of the parallel steps in my Unit Tests stage, but these still use the @2 suffixed workspace rather than the original.</p>
  
  <p>I have seen other threads talking about how you should not re-use the same workspace as you could run into issues with file locks. They suggest instead to archive\unarchive the workspace between steps.</p>
  
  <p>I have also seen approaches where you can store the workspace path in a variable and use this later on which sounds good for my case, but I havent found any declarative syntax samples, only groovy ones.</p>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>I have now tried a few approaches involving saving the allocated workspace from the first stage into a variable and using in a <code>ws(...)</code> directive in the later stages:</p>
  
  <pre class="lang-groovy prettyprint-override"><code>pipeline {
    agent { label 'master' }
    stages {
      stage('Build') {
        steps {
          script {
            def workspace = pwd()
          }
          sh '''
            npm install
            bower install
            gulp set-staging-node-env
            gulp prepare-staging-files
            gulp webpack
          '''
          stash includes: 'dist/**/*', name: 'builtSources'
          stash includes: 'config/**/*', name: 'appConfig'
          node('Protractor') {
            dir('/opt/foo/deploy/') {
              unstash 'builtSources'
              unstash 'appConfig'
            }
          }        
        }
      }
      stage('Unit Tests') {
        steps {
          parallel (
            "Jasmine": {
              node('master') {
                ws("${workspace}"){
                  sh 'gulp karma-tests-ci'
                }
              }
            },
            "Mocha": {
              node('master') {
                ws("${workspace}"){
                  sh 'gulp mocha-tests'
                }
              }
            }
          )
        }
        post {
          success {
            sh 'gulp combine-coverage-reports'
            sh 'gulp clean-lcov'
            publishHTML(target: [
              allowMissing: false,
              alwaysLinkToLastBuild: false,
              keepAll: false,
              reportDir: 'test/coverage',
              reportFiles: 'index.html',
              reportName: 'Test Coverage Report'
            ])
          }
        }
      }
      }
  }
  </code></pre>
  
  <p>I did try just removing the second agent declaration from the Unit Tests stage but the stage remained on my Protractor node which I didnt want it to do. So following the answers\comments here I then used node blocks around each of my parallel steps and used the ws blocks as you can see,</p>
  
  <p>The stage fails, and I can see from the logs that its not using the workspace allocated from the first stage (without the @ suffixes on):</p>
  
  <pre><code>[Jasmine] Running on master in /var/lib/jenkins/workspace/_Pipelines_IACT-Jenkinsfile-UL3RGRZZQD3LOPY2FUEKN5XCY4ZZ6AGJVM24PLTO3OPL54KTJCEQ@2
  [Pipeline] [Jasmine] {
  [Pipeline] [Jasmine] ws
  [Jasmine] Running in /var/lib/jenkins/workspace/_Pipelines_IACT-Jenkinsfile-UL3RGRZZQD3LOPY2FUEKN5XCY4ZZ6AGJVM24PLTO3OPL54KTJCEQ@2@2
  [Pipeline] [Jasmine] {
  [Pipeline] [Jasmine] sh
  [Jasmine] [_Pipelines_IACT-Jenkinsfile-UL3RGRZZQD3LOPY2FUEKN5XCY4ZZ6AGJVM24PLTO3OPL54KTJCEQ@2@2] Running shell script
  [Jasmine] + gulp karma-tests-ci
  [Jasmine] [08:27:01] No gulpfile found
  </code></pre>
  
  <p>Its even double suffixing with @2, so I'm unsure what its doing now.</p>