Skip to content

Instantly share code, notes, and snippets.

@marshyski
Created January 27, 2015 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marshyski/c58053d4ef79f5e1e9a6 to your computer and use it in GitHub Desktop.
Save marshyski/c58053d4ef79f5e1e9a6 to your computer and use it in GitHub Desktop.
Creating Jenkins Folder via web request with Grails / REST
import grails.plugins.rest.client.RestBuilder
import grails.transaction.Transactional
import sun.misc.BASE64Encoder
@Transactional
class JenkinsService {
def grailsApplication
def createJob(String folderName) {
String url = grailsApplication.config.jenkins.url
def rest = new RestBuilder(connectTimeout: 10000);
def base1 = 'createItem?name='
def base2 = '&mode=com.cloudbees.hudson.plugins.folder.Folder&from=&json=%7B%22name%22%3A%22'
def base3 = '%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'
def jobUrl = "${url}${base1}${folderName}${base2}${folderName}${base3}"
def resp = rest.post(jobUrl) {
auth getAuth()
contentType "application/x-www-form-urlencoded"
}
println "Jenkins Job Creation response ${resp.text}"
}
private String getAuth() {
String userName = grailsApplication.config.jenkinsrequest.jenkinsuser.username
String password = grailsApplication.config.jenkinsrequest.jenkinsuser.password
String userPassword = userName + ":" + password
return "Basic " + (new BASE64Encoder().encode(userPassword.getBytes()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment