You can use the dir step, example:
dir("folder") {
sh "pwd"
}
The folder can be relative or absolute path.
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.
There are lots of ways to do this. Here are two ways I can think of off the top of my head:
steps {
println(WORKSPACE)
}
or
steps {
def foo = sh(script: 'pwd', returnStdout: true)
println(foo)
}
In my MultiBranchPipeline I've achieved the goal using this shared library code:
#!groovy
import jenkins.model.Jenkins
String call() {
String thisMultiBranchProjectName = JOB_NAME.split('/')[0]
def thisMultiBranchProject = Jenkins.getInstance().getItemByFullName(thisMultiBranchProjectName)
def thisBranchProjectFactory = thisMultiBranchProject.getProjectFactory()
String thisStringPath = thisBranchProjectFactory.getScriptPath()
return thisStringPath
}
I do concede that this looks more like a hack…