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
Answer from Jon S on Stack OverflowWhat 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')])
jenkins pipeline example for creating a folder with a description - Stack Overflow
Jenkins - Create Folder and Pipeline in Startup
How to use a directory across stages?
Create a temporary directory for specific steps in Jenkins pipeline - Stack Overflow
Videos
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
}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…
Not exactly. There's the deleteDir step, which deletes the current directory, so you could do:
dir('/tmp/jobDir') {
// your steps here
deleteDir()
}
If this comes up often enough, you could also just make your own function:
def tempDir(path, closure) {
dir(path) {
closure()
deleteDir()
}
}
And use it like this:
tempDir('/tmp/jobDir') {
// your steps here
}
Edit: If you only want to delete the directory if it was newly created, you can use fileExists:
def tempDir(path, closure) {
def dirExisted = fileExists(path)
dir(path) {
closure()
if(!dirExisted) {
deleteDir()
}
}
}
My favorite solution so far:
withTempDir {
// Do something in the dir,e.g. print the path
echo pwd()
}
void withTempDir(Closure body) {
// dir( pwd(tmp: true) ) { // Error: process apparently never started i
dir( "${System.currentTimeMillis()}" ) {
try {
body()
} finally {
deleteDir()
}
}
}
Hey all, does anyone have any experience with Jenkins creating new project directory when the build is run, when I run my Jenkins build instead of the dir being foobar its foobar@1, I cant seems to find a way to stop this, the original project directory still exists but Jenkins does not use it, I've ended up with several projects directory like this, foobar@1, @2, @3 ect.
Any help would be appreciated.
On further inspection and some googling, I noticed that even with concurrent builds disabled, if a process linking the old Dir is still running from an abort when the new build runs it will spawn a new dir appended with @n -> does anyone have any suggestion to combat this?