Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins pipeline create directory

I want to know if there is a function or pipeline plugin that allows to create directory under the workspace instead of using sh "mkdir directory"?

I've tried to use a groovy instruction new File("directory").mkdirs() but it always return an exception.

org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use new java.lang.RuntimeException java.lang.String

like image 276
black4bird Avatar asked Mar 07 '17 17:03

black4bird


People also ask

How do I create a directory in Jenkins pipeline?

Just use a file operations plugin. Just for the directory creation it is easier to use the pipeline's dir { }. However mentioning of this plugin is also interesting, because it can perform many other file operations on a node.

How do I change the workspace directory in Jenkins pipeline?

So if you wish to change the Jenkins workspace, all you've do is change the path of your JENKINS_HOME. For slave nodes, specify the default workspace on the slave machine in the slave configuration under Manage Jenkins > Manage Nodes > > Configure > Remote FS root.

How do I create a subfolder in Jenkins?

Goto manage Jenkins > manage plugins > search for cloudbees folder plugin > install without restart. this will add an option called move that will move your jobs from one folder to another.

What is .Jenkins folder?

The Jenkins home directory contains all the details of your Jenkins server configuration, details that you configure in the Manage Jenkins screen. These configuration details are stored in the form of a set of XML files. Much of the core configuration, for example, is stored in the config. xml file.


2 Answers

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 
like image 152
Jon S Avatar answered Sep 20 '22 20:09

Jon S


Just use a file operations plugin.

fileOperations([folderCreateOperation('directoryname')]) 
like image 29
gek Avatar answered Sep 20 '22 20:09

gek