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-durable-task-step
Pipeline: Nodes and Processes
The relative path of the directory in the workspace to use as a new working directory.
Download and deploy
Packages marked third party may not be updated as frequently as packages supported by the Jenkins project directly.
Pipeline
Schedules the steps contained within the block to run by adding an item to the Jenkins queue. As soon as an executor is free on a node, the steps will run. Creates a workspace (a directory specific to that particular Pipeline) where work can be done on files checked out from source control.
Pipeline Syntax
Sections in Declarative Pipeline typically contain one or more Directives or Steps. The agent section specifies where the entire Pipeline, or a specific stage, will execute in the Jenkins environment depending on where the agent section is placed.
Using a Jenkinsfile
The Declarative Pipeline example above contains the minimum necessary structure to implement a continuous delivery pipeline. The agent directive, which is required, instructs Jenkins to allocate an executor and workspace for the Pipeline. Without an agent directive, not only is the Declarative ...
🌐
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 ...
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 › workflow-basic-steps
Pipeline: Basic Steps
Reply-To email address. Defaults to the admin address globally configured for the Jenkins instance. ... To email address list. Comma separated list of email addresses. Returns the current directory path as a string.
🌐
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 - - command: cd /workspace/source/charts/preview && ls -a - command: ls -a dir: /workspace/source/charts/preview
Author   jenkins-x
🌐
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 - dir('reports/test_output') { ... } This step tells Jenkins to change the current working directory to reports/test_output inside the workspace.
Find elsewhere
🌐
Jenkins
issues.jenkins.io › browse › JENKINS-46636
`dir` does not change working directory for contained steps
pipeline { agent none stages { stage('Change working directory...') { agent { docker 'my-image' } steps { dir('new-dir') { sh 'pwd' } } } } }
🌐
Jenkins
jenkins.io › doc › pipeline › examples
Pipeline Examples
// This shows a simple example of how to archive the build output artifacts. node { stage "Create build output" // Make the output directory. sh "mkdir -p output" // Write an useful file, which is needed to be archived. writeFile file: "output/usefulfile.txt", text: "This file is useful, need to archive it."
🌐
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.
🌐
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 you don't need to know if you are inside a dir step, just that there is a FilePath to use. https://github.com/jenkinsci/workflow-basic-steps-plugin/blob/master/src/main/java/org/jenkinsci/plugins/workflow/steps/PushdStep.java#L90-L93
🌐
Jenkins
jenkins.io › doc › pipeline › steps › pipeline-utility-steps
Pipeline Utility Steps
Reads a file in the current working directory or a String as a plain text JSON file.
🌐
University of Tampa
ut.edu › home
Directory
Building: Jenkins Technology Building · Room: TECH 353F · Gina Almerico · Director, Educator Preparation Programs; Professor, Education · Phone: (813) 257-3685 · Email: galmerico@ut.edu · Address: 401 W. Kennedy Blvd.
🌐
DevCoops
devcoops.com › changing-workspace-directory-in-jenkins
Changing workspace directory in Jenkins | DevCoops
June 24, 2023 - In Jenkins, each stage in a pipeline is executed in a separate workspace directory. The workspace is a local directory on the Jenkins agent where the pipeline is running. Each stage can have its own workspace, and the workspace is typically isolated from other stages.
🌐
Lakeland Regional Health
mylrh.org › home
Lakeland Regional Health | Hospital in Lakeland FL
July 5, 2016 - Advance Directives and Living Wills · Baby Photos & Delivery Gifts · Dining Services · Emergency Services · Extended Stay · Frequently Asked Questions · Gift Shop · Medical Center · Patient Education · Parking · Pastoral Care · Patient Safety and Quality · Pharmacy · Referral Transfer Center · Safety and Security · Centers of Excellence · Bannasch Institute for Advanced Rehabilitation Medicine · Brain and Spine Institute · Carol Jenkins Barnett Pavilion for Women and Children ·
🌐
HackerRank
hackerrank.com › skills-directory
Skills Directory | HackerRank
Join over 28 million developers in solving code challenges on HackerRank, one of the best ways to prepare for programming interviews.
🌐
Jenkins
jenkins.io › doc › book › managing › system-configuration
Configuring the System
The JENKINS_HOME directory is the root of the disk directory structure that Jenkins uses to perform builds and store archives.
🌐
Jenkins
jenkins.io › doc › pipeline › steps › file-operations
File Operations Plugin
Destination folder location to copy the files. Base directory is workspace.