Created
January 27, 2015 19:42
-
-
Save marshyski/c58053d4ef79f5e1e9a6 to your computer and use it in GitHub Desktop.
Creating Jenkins Folder via web request with Grails / REST
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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