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
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ jenkins โ€บ jenkins pipeline โ€“ change to another folder
Jenkins Pipeline โ€“ Change to Another Folder | Baeldung on Ops
November 6, 2023 - We defined a Build stage in our pipeline that changes the working directory. 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.
Discussions

Is there an alternative to the Jenkins dir() to change to a directory on a Docker container? - DevOps Stack Exchange
Here's my current set-up: Jenkins Master running in Docker on an Ubuntu 18.04 host. A Node running Ubuntu 18.04 with Docker installed. Desired job spins up docker containers on the node which build More on devops.stackexchange.com
๐ŸŒ devops.stackexchange.com
Is there any way to change the working directory of a Jenkins Maven build? - Stack Overflow
Is there any way to change the working directory of a Jenkins Maven build? Use Case There is a Maven build that will only work if the current working directory is the directory with pom.xml in it... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to change the jenkins home directory location? - Stack Overflow
Simple question: How to change the jenkins home directory location? By default it points to /var/lib/jenkins whereas I want it to point to /mnt/home/jenkins. I have changed my $JENKINS_HOME to /mnt... More on stackoverflow.com
๐ŸŒ stackoverflow.com
continuous integration - How to change home directory of Jenkins? - Stack Overflow
Simple question: How to change the jenkins home directory location? By default it points to /var/lib/jenkins whereas I want it to point to /home/jenkins. I have changed my $JENKINS_HOME to /home/je... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
TheServerSide
theserverside.com โ€บ blog โ€บ Coffee-Talk-Java-News-Stories-and-Opinions โ€บ Jenkins-Home-Directory-Location-Change-JENKINS_HOME-windows-linux-ubuntu
Find Jenkins Home and change the JENKINS_HOME directory location by example
The default Jenkins Home locations are used only if a JENKINS_HOME environment variable is not pre-configured. To change the location of Jenkins Home on Windows, simply add or update the JENKINS_HOME system variable and restart the CI/CD tool.
๐ŸŒ
PhoenixNAP
phoenixnap.com โ€บ home โ€บ kb โ€บ sysadmin โ€บ how to change jenkins home directory
How to Change Jenkins Home Directory {Linux or Windows}
October 18, 2022 - To change the Jenkins Home directory on Linux, create a new Home directory, copy the contents of the old Home directory to the new one and edit the Jenkins configuration file.
๐ŸŒ
DZone
dzone.com โ€บ testing, deployment, and maintenance โ€บ deployment โ€บ jenkins: changing the jenkins home directory
Steps to Changing Jenkins Home Directory
October 15, 2019 - 7. Create a new directory where ever you want to move the Jenkins home to. In this article, I creating a new directory in the /homepath. ... 8. We need to change the ownership of a created directory by using the chown command to change the user and group ownership of the created directory.
๐ŸŒ
Super User
superuser.com โ€บ questions โ€บ 1470676 โ€บ how-to-change-my-working-directory-using-execute-shell-script-on-remote-host-us
linux - How to change my working directory using "Execute shell script on remote host using ssh" in Jenkins? - Super User
In my case there is no need for me to put any double quote or single quote thing in my ssh as Jenkins already have this option in build tab called "Execute shell script on remote host using ssh" where I just need to filled in the "ssh site" for the ssh and "command" for the commands that I wanted to execute.. And for the very long hour of thinking to what's really wrong, I have figured it out already. I just have to leave the checkbox "Execute each line" UNCHECK - and it does change the directory already .
๐ŸŒ
YouTube
youtube.com โ€บ watch
How To Change Directory in Jenkins Pipeline - YouTube
Need help with your Jenkins questions?Visit https://community.jenkins.io/c/using-jenkins/support/8Timecodes โฑ:00:00 Introduction00:05 Starting point00:13 Rev...
Published ย  February 28, 2023
Find elsewhere
๐ŸŒ
Jenkins
jenkins.io โ€บ doc โ€บ book โ€บ managing โ€บ system-configuration
Configuring the System
You can change this location after youโ€™ve used Jenkins for a while, too. To do this: Stop Jenkins completely. Move the contents from the old JENKINS_HOME to the new location. Set the JENKINS_HOME variable to the new location.
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
issues.jenkins.io โ€บ browse โ€บ JENKINS-46636
Loading...
pipeline { agent none stages { stage('Change working directory...') { agent { docker 'my-image' } steps { dir('new-dir') { sh 'pwd' } } } } } What it does is simply create a new folder called "new-dir" in the current working directory and then the pwd prints out the current working directory ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-change-jenkins-home-directory
How to Change Jenkins Home Directory?
It's recommended to create this directory outside of user-specific directories to avoid permission issues later. sudo mkdir -p /opt/jenkins_home_new sudo chown jenkins:jenkins /opt/jenkins_home_new ยท First, stop the Jenkins service to prevent file corruption during the move: ... Copy all contents from the current Jenkins home to the new location. Using rsync ensures all file permissions and timestamps are preserved: sudo rsync -av /var/lib/jenkins/ /opt/jenkins_home_new/ ... Changing the Jenkins Home Directory is a straightforward process when done systematically.
Top answer
1 of 2
4

If you need to change the Jenkins home directory in Ubuntu, follow the instructions below:

Stop Jenkins 
/etc/init.d/jenkins stop

Create folder for new Jenkins home directory
mkdir home

Change access rights for this folder and all folders and files inside
sudo chown -R jenkins:jenkins home

Find current Jenkins folder and copy all directories and files into new one. If you don't know current Jenkins directory you can find it in /etc/default/jenkins file.
sudo -u jenkins cp -r /var/lib/jenkins/* home/

Change JENNIKS_HOME inside /etc/default/jenkins
sudo nano /etc/default/jenkins

find JENKINS_HOME and change path to new Jenkins home folder

Start Jenkins 
/etc/init.d/jenkins start 

In case if you have the error message 
Unable to create the home directory โ€˜/path/to/jenkins/homeโ€™. This is most likely a permission problem.
run the command
sudo chown -R jenkins:jenkins root_folder/

To get the root_folder you need go to Jenkins home folder and run pwd. This will work in case if you want use other one hdd (not system) for Jenkins home .
$pwd
/media/path/to/jenkins/home

In my case the root_folder is media

That's all. Enjoy!

Source: http://www.sofment.com/?q=node/25

2 of 2
2

By default, Jenkins home is set to ~/.jenkins, but you can change this in one of the following ways:

  • Set "JENKINS_HOME" environment variable to the new home directory before launching the servlet container.
  • Set "JENKINS_HOME" system property to the servlet container.
  • Set JNDI environment entry "JENKINS_HOME" to the new directory.

Refer the below URL from jenkins https://wiki.jenkins-ci.org/display/JENKINS/Administering+Jenkins

๐ŸŒ
Jenkins
jenkins.io โ€บ doc โ€บ pipeline โ€บ steps โ€บ workflow-basic-steps
Pipeline: Basic Steps
Use a comma separated list to add more than one expression. If blank, treated like **: all files. 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.
๐ŸŒ
Jenkins
community.jenkins.io โ€บ using jenkins
How do i change default build directory (history) - Using Jenkins - Jenkins
November 19, 2022 - Hi all, I am having issue setting up build directory properly on my jenkins installation on kubernetes. It defaults to /var/log${ITEM_ROOTDIR}/builds. How do i update this. I want to put builds in our share mount cifโ€ฆ
๐ŸŒ
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
}
๐ŸŒ
Blogger
ingorichter.blogspot.com โ€บ 2012 โ€บ 02 โ€บ jenkins-change-workspaces-and-build.html
today is tomorrow's past: Jenkins: Change Workspaces and Build Directory Locations
Navigate to Jenkins->Manage Jenkins->Configure System and click on the Advanced Button on the right hand side. Voilร ! This opens up the following options section ยท Now you can change your workspace and build directory to any other location ...