title: Jenkins Multibranch job with declarative pipeline cloning repo for every stage
body: <p>Trying to create a workflow in Jenkins using Declarative Pipeline to do something like this:</p>
  
  <ol>
  <li>Checkout the code on 'master'</li>
  <li>Build solution on 'master' (I know this is not a secure way to do it, but Jenkins is in the intranet so it should be fine for us)</li>
  <li>Stash artifacts (.dll, .exe, .pdb, etc) => 1st stage</li>
  <li>Unstash artifacts on nodes depending on what it's needed (Unit tests on a slave, Integration tests on another one and Selenium tests on a another one) => 2nd stage</li>
  <li>Run tests depending on the slave => 3rd stage running in parallel</li>
  </ol>
  
  <p>The problem that I'm facing is that the git checkout (GitSCM) is executed for every stage.</p>
  
  <p>My pipeline looks like this:</p>
  
  <pre><code>pipeline {
      agent {
          label {
              label "master"
              customWorkspace "C:\\Jenkins\\workspace\\CustomWorkspace"
          }
      }
  
      options {
          timestamps()
      }
  
      stages {
          stage("Build") {
              agent {
                  label {
                      label "master"
                      customWorkspace "C:\\Jenkins\\workspace\\CustomWorkspace"
                  }
              }
              steps {
                  /*
                      steps to build the solution here
                  */
  
                  //Sleep because stashing fails otherwise
                  script {
                      sleep(1)
                  }
  
                  dir("${env.WORKSPACE}\\UnitTests\\bin\\Release") {
                      stash name: 'unit-tests'
                  }
  
                  dir("${env.WORKSPACE}\\WebUnitTests\\bin\\x64\\Release") {
                      stash name: 'web-unit-tests'
  
              }
          }
  
          stage('Export artefacts') {
              agent {
                  label {
                      label "UnitTest"
                      customWorkspace "C:\\Jenkins\\workspace\\CustomWorkspace"
                  }
              }
              steps {
                  echo "Copying dlls from master to ${env.NODE_NAME}"
                  dir("${env.WORKSPACE}\\UnitTests\\bin\\Release") {
                      unstash 'unit-tests'
                  }
              }
          }
  
          stage('Run tests') {
              parallel {
                  stage("Run tests #1") {
                      agent {
                          label {
                              label "UnitTest"
                              customWorkspace "C:\\Jenkins\\workspace\\CustomWorkspace"
                          }
                      }
                      steps {
                          /*
                              run tests here
                          */
                      }
                      post {
                          //post results here
                      }
                  }
                  //other parallel stages
              }
          }
      }
  }
  </code></pre>
  
  <p>So, as mentioned earlier, the GitSCM (code checkout) is a part of and performed for every stage:
  Build stage
  <a href="https://i.stack.imgur.com/XD6av.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XD6av.png" alt="enter image description here"></a></p>
  
  <p>Export stage
  <a href="https://i.stack.imgur.com/bmjfe.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bmjfe.png" alt="enter image description here"></a></p>