title: Jenkins Pipeline: no permission to create directories inside script block with .execute()
body: <p>I want to run an external shell command (for example, <code>git clone</code>) inside a Jenkins pipeline.</p>
  
  <p>I have found 2 ways of doing this.</p>
  
  <hr>
  
  <p>This one works:</p>
  
  <pre><code>steps {
      sh "git clone --branch $BRANCH --depth 1 --no-single-branch $REMOTE $LOCAL
  }
  </code></pre>
  
  <p>Downsides:</p>
  
  <ul>
  <li>I only see the output when the complete command is finished. Which is annoying if the command takes a long time.</li>
  <li>I need to do some Groovy scripting to look up values in a <code>Map</code>, based on parameters chosen by the user who starts the build. Haven't found a way to do that without a <code>script {}</code> block.</li>
  <li>A variation is to run a Bash script that runs the <code>git clone</code> command, that also works. Which will get me into trouble when running on Windows nodes. </li>
  </ul>
  
  <hr>
  
  <p>The next one errors on</p>
  
  <blockquote>
    <p>fatal: could not create work tree dir 'localFolder'.: Permission denied</p>
  </blockquote>
  
  <pre><code>steps {
      script {
          def localFolder = new File(products[params.PRODUCT].local)
          if (!localFolder.exists()) {
              def gitCommand = 'git clone --branch ' + params.BRANCH + ' --depth 1 --no-single-branch ' + products[params.PRODUCT].remote + ' ' + localFolder
              runCommand(gitCommand)
          }
      }
  }
  </code></pre>
  
  <hr>
  
  <p>This is the <code>runCommand()</code> wrapper:</p>
  
  <pre><code>def runCommand = { strList -&gt;
      assert ( strList instanceof String ||
              ( strList instanceof List &amp;&amp; strList.each{ it instanceof String } ) \
  )
      def proc = strList.execute()
      proc.in.eachLine { line -&gt; println line }
      proc.out.close()
      proc.waitFor()
  
      print "[INFO] ( "
      if(strList instanceof List) {
          strList.each { print "${it} " }
      } else {
          print strList
      }
      println " )"
  
      if (proc.exitValue()) {
          println "gave the following error: "
          println "[ERROR] ${proc.getErrorStream()}"
      }
      assert !proc.exitValue()
  }
  </code></pre>
  
  <hr>
  
  <p>My question is: how come I have permission to create directories when running a <code>sh</code> command, and how come I don't have that permission when I do the same thing inside a <code>script {}</code> block with <code>.execute()</code>?</p>
  
  <p>I'm intentionally using the example of the <code>git clone</code> command to avoid getting answers that don't read the question, like using a <code>dir {}</code> block. If I can't create the git directory, then I can also not create the files inside that directory.</p>