You can use the dir step, example:

dir("folder") {
    sh "pwd"
}

The folder can be relative or absolute path.

Answer from tsl0922 on Stack Overflow
🌐
Jenkins
jenkins.io › doc › pipeline › steps › workflow-basic-steps
Pipeline: Basic Steps
The current working directory is the base directory for the saved files, which will later be restored in the same relative locations, so if you want to use a subdirectory wrap this in dir. ... If selected, use the default excludes from Ant - see here for the list. Defaults to true. This is a special step that allows to call builders or post-build actions (as in freestyle or similar projects), in general "build steps". Just select the build step to call from the dropdown list and configure it as needed. Note that only Pipeline-compatible steps will be shown in the list.
Discussions

groovy - Pass variable path in dir() jenkins pipeline - Stack Overflow
I have created a jenkinsfile in which I need to go into an already created directory. But the problem is that the name of the directory can be variable, eg: dir ('RELEASE/abc/${abc-version}'). The ... More on stackoverflow.com
🌐 stackoverflow.com
Jenkins pipeline create directory - Stack Overflow
The exception is thrown because you need to explicitly whitelist that script/module: jenkins.io/doc/book/managing/script-approval ... also, if running on a node, that directory will be created on the master. Groovy CPS means that groovy code executes on master and is transported over to the node. More on stackoverflow.com
🌐 stackoverflow.com
How to use a directory across stages?
If the stages were on different nodes or workspaces... for non-large files and directories you could stash () ... unstash() More on reddit.com
🌐 r/jenkinsci
5
5
October 26, 2020
Is there an alternative to the Jenkins dir() to change to a directory on a Docker container? - DevOps Stack Exchange
The above is a complete Pipeline script. inside will: Automatically grab an agent and a workspace (no extra node block is required). Pull the requested image to the Docker server (if not already cached). Start a container running that image. Mount the Jenkins workspace as a "volume" inside ... More on devops.stackexchange.com
🌐 devops.stackexchange.com
🌐
Jenkins
jenkins.io › doc › book › pipeline › syntax
Pipeline Syntax
In order to use this option, the Jenkinsfile must be loaded from either a Multibranch Pipeline or a Pipeline from SCM. Conventionally this is the Dockerfile in the root of the source repository: agent { dockerfile true }. If building a Dockerfile in another directory, use the dir option: agent { dockerfile { dir 'someSubDir' } }. If your Dockerfile has another name, you can specify the file name with the filename option.
🌐
Baeldung
baeldung.com › home › jenkins › jenkins pipeline – change to another folder
Jenkins Pipeline – Change to Another Folder | Baeldung on Ops
November 6, 2023 - Using the dir function, the script switches to the directory “scripts”, then runs the build steps. In addition, we used “any” agent in the above script, which means that any available Jenkins agent can execute this pipeline job.
🌐
Medium
medium.com › @dipali26N › day-39-creating-directories-in-jenkins-pipelines-278953b74f48
DAY:39 Creating Directories in Jenkins Pipelines | by Dipali Naygaonkar | Medium
July 23, 2025 - If the directory does not exist, Jenkins will create it automatically. writeFile file: 'dummy.txt', text: 'created' Creates a file named dummy.txt inside reports/test_output with the content "created". \n for new line. echo and sh steps These print confirmation and list the files inside the directory for visibility during the build. ... pipeline { agent any stages { stage('Temp Workspace') { steps { script { ws("/tmp/${env.BUILD_NUMBER}") { sh ''' mkdir -p my-temp-dir echo "This is a test file."
🌐
Jenkins
jenkins.io › doc › pipeline › steps › workflow-durable-task-step
Pipeline: Nodes and Processes
Any step inside the dir block will use this directory as current and any relative path will use it as base path.
Find elsewhere
🌐
Jenkins
jenkins.io › doc › pipeline › examples
Pipeline Examples
Demonstrate how to retrieve the changeset associated with a git commit to a Pipeline job. // This should be performed at the point where you've // checked out your sources on the agent. A 'git' executable // must be available. // Most typical, if you're not cloning into a sub directory // and invoke this in the context of a directory with .git/ // Along with SHA-1 id of the commit, it will be useful to retrieve changeset associated with that commit // This command results in output indicating several one of these and the affected files: // Added (A), Copied (C), Deleted (D), Modified (M), Renamed (R) commitChangeset = sh(returnStdout: true, script: 'git diff-tree --no-commit-id --name-status -r HEAD').trim()
🌐
Jenkins
issues.jenkins.io › browse › JENKINS-46636
Loading...
Any step inside the dir block will use this directory as current and any relative path will use it as base path. However, I can't seem to get it work. I use declarative syntax with a very basic Jenkinsfile · pipeline { agent none stages { stage('Change working directory...') { agent { docker 'my-image' } steps { dir('new-dir') { sh 'pwd' } } } } }
🌐
Reddit
reddit.com › r/jenkinsci › how to use a directory across stages?
r/jenkinsci on Reddit: How to use a directory across stages?
October 26, 2020 -

Is there any way to create a directory in jenkins declarative pipeline and use it across stages?

i.e

node(){
checkout scm
sh "mkdir ${WORKSPACE/conan}" def String dockerImage = 'development-tools'
stage('Testing') {

parallel (
'Release Build': { docker.image(dockerImage).inside(dockerArgs) { 

sh "touch file.txt && mv file.txt  ${WORKSPACE/conan"
 }
 }
stage('Deploy')
 // use file.txt here again
}
Top answer
1 of 2
2

I could not achieve changing the working directory using the suggestion by Vishwas, although I did try creating a Jenkins user inside the container with the same uid as the host. It just didn't work for me. I did however, realize that I didn't actually need to change the directory on the container because the workspace was already in the container.

I'm using the Docker Plugin from CloudBees. After reading their documentation, I realized the workstation folder I thought was on the host machine, was actually in the container. Part of the documentation stated:

The above is a complete Pipeline script. inside will:

  1. Automatically grab an agent and a workspace (no extra node block is required).
  2. Pull the requested image to the Docker server (if not already cached).
  3. Start a container running that image.
  4. Mount the Jenkins workspace as a "volume" inside the container, using the same file path.

After seeing item number four, I ran docker container inspect <containerID> and I saw the workspace /home/jenkins/workspace was actually a volume mount that was mapped to the /home/jenkins/workspace on the Docker host machine as well. Using this as my workspace, I was able to build the various applications within the container and then moved the results to a second volume used for application collection later in the pipeline. It's not an exact answer to my original question, but it works!

2 of 2
1

If your master is running as Jenkins user, you should make a user in the docker image which you use as to create your node and do the key exchange. For example jenkinsuser with same UID needs to exist on both master and node docker container.

🌐
Jenkins
jenkins.io › doc › pipeline › steps › pipeline-utility-steps
Pipeline Utility Steps
Ant style pattern of file paths that should match. If this property is set all descendants of the current working directory will be searched for a match and returned, if it is omitted only the direct descendants of the directory will be returned.
🌐
Google Groups
groups.google.com › g › jenkinsci-dev › c › EIcrX6H6yUo › m › G9Fkg-TdCgAJ
When using the dir step in a Jenkins pipeline is there any way to get that value from a step within?
So if your step is called inside a dir step then the FilePath will be that directory otherwise it will be the workspace, or null if the step gets called outside of a node step.
🌐
Jenkins
jenkins.io › doc › book › pipeline › jenkinsfile
Using a Jenkinsfile
Not all Pipelines will have these ... creation and execution of a simple Pipeline in a test installation of Jenkins. Using a text editor, ideally one which supports Groovy syntax highlighting, create a new Jenkinsfile in the root directory of the project....
🌐
GitHub
github.com › jenkins-x › jx › issues › 5584
dir pipeline option fails if directory does not exist in master · Issue #5584 · jenkins-x/jx
September 24, 2019 - The build errors with: sh: 0: getcwd() failed: No such file or directory make: getcwd: No such file or directory make: *** No rule to make target 'preview'. Stop. The output of jx version is: jx 2.0.758 Kubernetes cluster v1.13.7-gke.19 kubectl v1.15.0 helm client Client: v2.14.2+ga8b13cc git 2.22.0 Operating System Mac OS X 10.14.6 build 18G95 · Serverless Jenkins X Pipelines (Tekton + Prow) Classic Jenkins ·
Author   jenkins-x
🌐
CloudBees
docs.cloudbees.com › documentation › use declarative pipeline syntax
Use Declarative Pipeline syntax
February 1, 2022 - When applied at the top-level of the pipeline block no global agent will be allocated for the entire Pipeline run and each stage directive will need to contain its own agent directive. For example: agent none ... Execute the Pipeline, or stage, on an agent available in the Jenkins environment with the provided label.
🌐
Jenkins
jenkins.io › doc › book › pipeline › getting-started
Getting started with Pipeline
As mentioned above, unlike Jenkinsfiles you define in source control (below), Jenkinsfiles entered into the Script text area of Pipeline projects are stored by Jenkins itself, within the Jenkins home directory.