Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Jenkins trigger, create and remove jobs and folders
# check if job exists
curl -XGET 'http://jenkins/checkJobName?value=yourJobFolderName' --user user.name:YourAPIToken
# with folder plugin
curl -s -XPOST 'http://jenkins/job/FolderName/createItem?name=yourJobName' --data-binary @config.xml -H "Content-Type:text/xml" --user user.name:YourAPIToken
# without folder plugin
curl -s -XPOST 'http://jenkins/createItem?name=yourJobName' --data-binary @config.xml -H "Content-Type:text/xml" --user user.name:YourAPIToken
# create folder
curl -XPOST 'http://jenkins/createItem?name=FolderName&mode=com.cloudbees.hudson.plugins.folder.Folder&from=&json=%7B%22name%22%3A%22FolderName%22%2C%22mode%22%3A%22com.cloudbees.hudson.plugins.folder.Folder%22%2C%22from%22%3A%22%22%2C%22Submit%22%3A%22OK%22%7D&Submit=OK' --user 'user.name:YourAPIToken' -H "Content-Type:application/x-www-form-urlencoded"
# remove folder / job
curl -XPOST 'http://jenkins/job/FolderName/doDelete' --user 'user.name:YourAPIToken'
# trigger remote job
curl 'http://jenkins/job/yourJobName/build?delay=0sec' --user 'user.name:YourAPIToken'
@josesa-xx

This comment has been minimized.

Copy link

@josesa-xx josesa-xx commented Jul 1, 2018

To create folder it is not needed to pass the "from" and "json" inputs. The following I've tested to work creating a sub-folder (I used -n to read server specific credentials from .netrc file):

curl -s -Lkgf -o out.html -n -X POST "${JENKINS_ROOT_URL}/${JOB_FOLDER_URL}/createItem?name=${JOB_NAME}&mode=com.cloudbees.hudson.plugins.folder.Folder&Submit=OK" -H "Content-Type:application/x-www-form-urlencoded"

@jonfast565

This comment has been minimized.

Copy link

@jonfast565 jonfast565 commented Jan 2, 2019

@josesa-xx is correct. I was also able to do this without the from or json form parameters.

@SebastianHerzog

This comment has been minimized.

Copy link

@SebastianHerzog SebastianHerzog commented Mar 20, 2019

Hi, i am using the create Folder API within a groovy skript. this is the code i use:

import groovyx.net.http.*

def post = new URL("JenkinsURL:Port/createItem?name=FolderName&mode=com.cloudbees.hudson.plugins.folder.Folder").openConnection();
post.setRequestMethod("POST")
post.setDoOutput(true)
post.setRequestProperty("Content-Type", "application/x-www-form-urlencoded")
def postRC = post.getResponseCode();
println(postRC);
if(postRC.equals(200)) {
    println(post.getInputStream().getText());
}

my Problem is now that i dont know where to put the --user user.name;YourAPIToken part. With my Code i am getting 403 response
which is perfectly normal given the code i use. Where exactly do i have to put the user information ? is it in the Body or in the Header or somewhere else ?

@adelwin

This comment has been minimized.

Copy link

@adelwin adelwin commented Jun 12, 2019

if i'm reading my stuffs correctly, seems like the newer jenkins doesn't support remoting anymore, so with that you can't run groovy scripts remotely anymore.
I've also been trying to find a way to create folders using jenkins-cli, now i have to resort to using cURL

To create folder it is not needed to pass the "from" and "json" inputs. The following I've tested to work creating a sub-folder (I used -n to read server specific credentials from .netrc file):

curl -s -Lkgf -o out.html -n -X POST "${JENKINS_ROOT_URL}/${JOB_FOLDER_URL}/createItem?name=${JOB_NAME}&mode=com.cloudbees.hudson.plugins.folder.Folder&Submit=OK" -H "Content-Type:application/x-www-form-urlencoded"

@josesa-xx can you tell me how that JOB_FOLDER_URL is configured? I'm particularly interested in creating folders in folders

@aarthy4f

This comment has been minimized.

Copy link

@aarthy4f aarthy4f commented Oct 30, 2020

Hi All,
how do we create a job under the list view ,Tried with this API https://jenkins url/view/ViewName/addJobToView?name=jobname
Its seems not working. Can anyone help

@saketraman

This comment has been minimized.

Copy link

@saketraman saketraman commented Nov 30, 2020

Hi All,

I need to add parameters while creating the folder. Usually in UI, I do it using the Folders plus plugin which given the option to add environmental variables in the list of parameters.

"com-cloudbees-udson-plugins-folder-properties-EnvVarsFolderProperty": {"properties": "envvar1=test1"}

How do I pass on this while creating the Folder ??

@marshyski

This comment has been minimized.

Copy link
Owner Author

@marshyski marshyski commented Nov 30, 2020

Hi everyone, this gist is pretty old. I would be surprised if these web requests still work. I can take a look if you put the curl -v output for both request and response here (please remove any secrets/tokens from req/resp).

@saketraman

This comment has been minimized.

Copy link

@saketraman saketraman commented Dec 1, 2020

Hi ,

Basically I need to setup a property as below while creating the Folder. I was able to create the folder as per the above shared commands. But clueless how to pass that in curl to setup the properties as well.

Below snippet tells how my properties section look in the config.xml for this Folder.

<properties>
<com.cloudbees.hudson.plugins.folder.properties.EnvVarsFolderProperty plugin="cloudbees-folders-plus@3.9">
<properties>envVar1=val1 envVar2=val2 envVar3=val3</properties>
</com.cloudbees.hudson.plugins.folder.properties.EnvVarsFolderProperty>
</properties>
@marshyski

This comment has been minimized.

Copy link
Owner Author

@marshyski marshyski commented Dec 1, 2020

Once you create an item (job) you should be able to overwrite the config.xml file aftwards:

I believe this call will work:

curl -s -XPOST 'http://jenkins/job/FolderName/ProjectName/config.xml' --data-binary @config.xml -H "Content-Type:text/xml" --user user.name:YourAPIToken

or

curl -s -XPUT 'http://jenkins/job/FolderName/ProjectName/config.xml' --data-binary @config.xml -H "Content-Type:text/xml" --user user.name:YourAPIToken
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment