The particulars will depend on each API that you use, but ScriptRunner comes bundled with the HttpBuilder library, which makes making HTTP requests of external APIs pretty straightforward.
Here's a pretty basic example that I used to contact a local jenkins instance.
import groovyx.net.http.ContentType
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.Method
if (repository.name == "myrepo") {
String jenkinsUrl = "http://localhost:8089/"
String userName = 'admin'
String password = 'admin'
String targetJob = 'job1'
def http = new HTTPBuilder(jenkinsUrl)
String uspass = "$userName:$password".bytes.encodeBase64()
http.setHeaders([Authorization: "Basic $uspass"])
def crumb
http.get(path: 'crumbIssuer/api/xml') { response, xml ->
crumb = xml.crumb.toString()
}
log.debug "Jenkins authorization crumb: $crumb"
http.headers += [
".crumb" : crumb, //different versions of Jenkins take different versions of this header
"Jenkins-Crumb": crumb
]
http.request(Method.POST, ContentType.TEXT) {
uri.path = "job/$targetJob/build"
query = [token: "62d07cb3d3571bebf23de309e88f1f3a6f7c93bd", crumb: crumb]
}
}
Naturally, you'll want to change the jenkinsUrl, repository name, target job name, and credentials used to match your use case. I'd also recommend that you store the jenkins credentials somewhere besides hard-coding them into your script (perhaps an environment variable or a credentials store on your server), then use the script to retrieve them.
I also assume that your Jenkins instance has CSRF protection enabled, since recent versions of Jenkins use it by default. If it's not enabled, you won't need the first get request to the crumbIssuer API, and you shouldn't need to set any authentication headers at all; the token should be sufficient to fire the Jenkins job.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.