Skip to content

Instantly share code, notes, and snippets.

@marshyski
Last active September 22, 2023 09:18
Show Gist options
  • Save marshyski/abaa1ccbcee5b15db92c to your computer and use it in GitHub Desktop.
Save marshyski/abaa1ccbcee5b15db92c to your computer and use it in GitHub Desktop.
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'
@marshyski
Copy link
Author

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
Copy link

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="[email protected]">
<properties>envVar1=val1 envVar2=val2 envVar3=val3</properties>
</com.cloudbees.hudson.plugins.folder.properties.EnvVarsFolderProperty>
</properties>

@marshyski
Copy link
Author

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