You can use the dir step, example:
dir("folder") {
sh "pwd"
}
The folder can be relative or absolute path.
Is there an alternative to the Jenkins dir() to change to a directory on a Docker container? - DevOps Stack Exchange
Is there any way to change the working directory of a Jenkins Maven build? - Stack Overflow
How to change the jenkins home directory location? - Stack Overflow
How do i change default build directory (history)
Videos
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:
- 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 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!
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.
Create a target to checkout the parent.
- Checkout the parent dir of your build target
- Set Build -> Goal = clean
- Run build and ascertain the destination workspace directory (second line of console output, in my sample /var/lib/jenkins/workspace/my_project)
Create a target to build your path
- DO NOT check "Delete workspace before build starts"
- In Build -> Advanced - Check "Change folder of workspace" and paste your /yourtargetdirectory
It worked for me.
Not sure when this was added, but for the Jenkins in front of me (Jenkins ver. 1.627 per the bottom of the page), there's now an option for defining where the root pom.xml is (help message says:
If your workspace has the top-level pom.xml in somewhere other than the 1st module's root directory, specify the path (relative to the module root) here, such as parent/pom.xml. If left empty, defaults to pom.xml
). I was facing the same issue but then realized the answer was staring me in the face (note that I did not have to hit advanced as Hua2308 suggested, probably because they changed the UI)
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
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
For me on Jenkins 2.7.2 on RHEL 7.2 after already starting jenkins and configuring a build, I needed to:
1) Change the jenkins user's home directory
sudo su -
service jenkins stop
vi /etc/passwd
# change the /var/lib/jenkins to /home/jenkins or whatever
2) Change the setting in the start script to also find it
vi /etc/sysconfig/jenkins
# change the JENKINS_HOME to /home/jenkins or what ever
3) Copy all the data to the new location (eg: /home/jenkins)
cd /home
cp -Rf /var/lib/jenkins .
chown -R jenkins:jenkins *
4) Start it back up
service jenkins start
And that seems to have done it for me.
To change the Jenkins home directory you just need to setup the "JENKINS_HOME" environment variable to point to the new location. You can also set the JENKINS_HOME as a system property or a JNDI environment entry as explained in the documentation.
Jenkins Documentation
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
}You can modify the path on the config.xml file in the default directory
<projectNamingStrategy class="jenkins.model.ProjectNamingStrategy$DefaultProjectNamingStrategy"/>
<workspaceDir>D:/Workspace/${ITEM_FULL_NAME}</workspaceDir>
<buildsDir>D:/Logs/${ITEM_ROOTDIR}/Build</buildsDir>
I figured it out. In order to save your Jenkins data on other drive you'll need to do the following:
Workspace Root Directory: E:\Jenkins\${ITEM_FULL_NAME}\workspace
Build Record Root Directory: E:\Jenkins\${ITEM_FULL_NAME}\builds

This question was already asked and answered on Stack Overflow. I'll copy the top answer here, but there are several other answers over there that you may find useful:
For me on Jenkins 2.7.2 on RHEL 7.2 after already starting jenkins and configuring a build, I needed to:
Change the jenkins user's home directory
sudo su - service jenkins stop vi /etc/passwd # change the /var/lib/jenkins to /home/jenkins or whateverChange the setting in the start script to also find it
vi /etc/sysconfig/jenkins # change the JENKINS_HOME to /home/jenkins or what everCopy all the data to the new location (eg: /home/jenkins)
cd /home cp -Rf /var/lib/jenkins . chown -R jenkins:jenkins *Start it back up
service jenkins startAnd that seems to have done it for me.
I was having the same problem on a Centos 7.9 system using Jenkins 2.346.2
Sometime around 2.332.1 Jenkins migrated from init.d to system.d, so the previous method of updating the /etc/sysconfig/jenkins (or /etc/default/jenkins) became deprecated.
You now use overrides through: systemctl edit jenkins
Jenkins systemd reference: https://www.jenkins.io/doc/book/system-administration/systemd-services/