title: Different working directories in jenkins pipeline
body: <p>Here's a simple Jenkins pipeline job that highlights the different working directories seen by <code>sh</code> vs <code>script</code>.</p>
  
  <pre><code>pipeline {
    agent any
  
    stages {
      stage('Stage1') {
        steps {
          sh """
          pwd
          """
          script {
            echo "cwd--pwd: "+ "pwd".execute().text
          }
        }
      }
    }
  }
  </code></pre>
  
  <p>Here's how the Jenkins instance was launched</p>
  
  <pre><code>/Users/MyJenkinsUser/dirJenkinsLaunched$ java -jar /Applications/Jenkins/jenkins.war --httpPort=8080
  </code></pre>
  
  <p>Here's the console output of the job...</p>
  
  <pre><code>Started by user MyJenkinsUser
  Running in Durability level: MAX_SURVIVABILITY
  [Pipeline] node
  Running on Jenkins in /Users/MyJenkinsUser/.jenkins/jobs/TestPipeline/workspace
  [Pipeline] {
  [Pipeline] withEnv
  [Pipeline] {
  [Pipeline] stage
  [Pipeline] { (Stage1)
  [Pipeline] sh
  [workspace] Running shell script
  + pwd
  /Users/MyJenkinsUser/.jenkins/jobs/TestPipeline/workspace
  [Pipeline] script
  [Pipeline] {
  [Pipeline] echo
  cwd--pwd: /Users/MyJenkinsUser/dirJenkinsLaunched
  [Pipeline] }
  [Pipeline] // script
  [Pipeline] }
  [Pipeline] // stage
  [Pipeline] }
  [Pipeline] // withEnv
  [Pipeline] }
  [Pipeline] // node
  [Pipeline] End of Pipeline
  Finished: SUCCESS
  </code></pre>
  
  <p>I find it curious that they would be different working directories, the shell command <code>sh</code> step uses the workspace as the working directory while the groovy <code>script</code> step uses the directory the Jenkins process was launched.</p>
  
  <p><strong>Question</strong>: how can I make my Jenkins scripted pipeline steps (<code>script</code>) use the workspace as the working directory by default?</p>
  
  <hr>
  
  <p>I guess it makes sense after realizing this most clearly, that groovy is a java thing and we launch the Jenkins war file from java, and that launching imposes a certain working directory. I wonder the origins of this design for the Jenkins behavior. It made me go a bit wonky with a bunch of file not found errors as I ported some <code>sh</code> commands into the more substantive groovy syntax because I wanted to avoid all the double nesting escaping craziness that one can fall into in the shell, especially when spaces are invoked in paths and what not.</p>