You can use the dir step, example:
dir("folder") {
sh "pwd"
}
The folder can be relative or absolute path.
groovy - Pass variable path in dir() jenkins pipeline - Stack Overflow
Jenkins pipeline create directory - Stack Overflow
How to use a directory across stages?
Is there an alternative to the Jenkins dir() to change to a directory on a Docker container? - DevOps Stack Exchange
Videos
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…
Use double quotes
dir ("RELEASE/abc/${abc_version}")
in groovy Single quotes are a standard Java String while Double quotes are a templatable String
e.g
a = 10
b = "RELEASE/abc/${a}"
c = 'RELEASE/abc/${a}'
print(b)
print('\n')
print(c)
output will be
RELEASE/abc/10
RELEASE/abc/${a}
You can try it here
We need to use double quotes to resolve variable in groovy script.
use the following sample code
def dirpath = "RELEASE/abc/${abc-version}"
dir(dirpath){
//logic
}
What you can do is use the dir step, if the directory doesn't exist, then the dir step will create the folders needed once you write a file or similar:
node {
sh 'ls -l'
dir ('foo') {
writeFile file:'dummy', text:''
}
sh 'ls -l'
}
The sh steps is just there to show that the folder is created. The downside is that you will have a dummy file in the folder (the dummy write is not necessary if you're going to write other files). If I run this I get the following output:
Started by user jon
[Pipeline] node
Running on master in /var/lib/jenkins/workspace/pl
[Pipeline] {
[Pipeline] sh
[pl] Running shell script
+ ls -l
total 0
[Pipeline] dir
Running in /var/lib/jenkins/workspace/pl/foo
[Pipeline] {
[Pipeline] writeFile
[Pipeline] }
[Pipeline] // dir
[Pipeline] sh
[pl] Running shell script
+ ls -l
total 4
drwxr-xr-x 2 jenkins jenkins 4096 Mar 7 22:06 foo
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
Just use a file operations plugin.
fileOperations([folderCreateOperation('directoryname')])
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
}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.
You shall not use execute() in Jenkins pipelines. Use the pipeline DSL's steps instead of arbitrary Groovy code.
As you noticed, this such "native" code is executed on the Jenkins master and without any relation to the current job.
Unfortunately this may not be a possible operation. I'll have to redesign the script code to explicitly use the workspace variable instead of relying on the current working directory Java uses.
Changing the current working directory in Java?