Trigger a Jenkins Build with UI from Jira

beril kurt September 6, 2018

Hello everybody,

I am trying to trigger a Jenkins build from Jira. I have a UI element (Trigger Jenkins Build button) in my Test Plan in Jira. I followed each step in the following link Script Runner. When I click the button, nothing happens but when I used the link 

https://${jenkinsHostPort}/job/${jobName}/buildWithParameters?token=${token}&TESTPLAN=$issueId" then I can see a new Jenkins build. I expected the same behavior when I click the button.

In groovy Script (trigger_jenkins_build_restapi_endpoint.groovy), I changed https to http, rest of the code is same. 

Advance thanks for your help.

3 answers

0 votes
Connor White June 4, 2021

Hey all, (my first ever community contribution here :p) 

I searched and searched for a solution for this and only ever found bits and pieces I could use. 

I ended up coming up with the following http Builder solution. The goal was to define some current issue parameters, and then take those parameters and pass them into the jenkins build. I'm now successfully triggering builds with parameters in jenkins by this http builder script using scriptrunner for JIRA.

Really hope this helps, I've thoroughly enjoyed this solution now that it's finished. 

 

Note ** if you do not have explicit networking connectivity in place from your jira instance to your jenkins instance you will be banging your head for wasted hours attempting to get a http post to hit jenkins. I didn't realize that both servers behind our firewall still needed some explicit routes defined at the networking level to allow these machines to communicate. If you're getting time outs / or host's "not resolving" errors it's either the DNS isn't able to resolve (devops / infra team can help) or it's a networking routing issue (devops /infra team can help)

Also Note** I've removed my jenkins auth key & login from, and the endpoints for privacy. 

 

(Pretty liberal imports, I don't even recall if I used them all) 

import groovyx.net.http.HTTPBuilder
import groovyx.net.http.Method
import javax.ws.rs.core.Response
import groovyx.net.http.ContentType
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.component.ComponentAccessor


IssueManager im = ComponentAccessor.getIssueManager()
MutableIssue issue = im.getIssueObject("SD-4672")

if(issue){
//get the value
log.info("Found Issue, retrieving custom fields..")

//Input 1
def im_first_name_cf_val = issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_15201"))

//input 2
def im_last_name_cf_val = issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_15202"))

//input 3
def im_username_cf_val = issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_15203"))

//input 4
def im_email_cf_val = issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_15204"))

//input 5
def preferred_client_name_cf_val = issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_14302"))

//input 6
def client_backend_institution_name_cf_val_list = issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_13709"))
def client_backend_institution_name_cf_val = client_backend_institution_name_cf_val_list.find{true}.toString()

//input 7
def client_user_admin_cf_val = issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_14304"))

//selection 1
def infrastructure_location_cf_val = issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_15205"))

//selection 2
def uploader_type_cf_val = issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_15206"))

// Input 8
def zip_code_cf_val = issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_15700"))


//Store your jenkin parameter here as a map
def data = [IM_FirstName: "$im_first_name_cf_val", IM_LastName: "$im_last_name_cf_val", User_Name: "$im_username_cf_val", IM_Email: "$im_email_cf_val", Institution_Label: "$preferred_client_name_cf_val", Institution_Name: "$client_backend_institution_name_cf_val", Client_Email: "$preferred_client_name_cf_val", Infrastructure_Location: "$infrastructure_location_cf_val", Uploader: "$uploader_type_cf_val", Zip_Code: "$zip_code_cf_val"]

def httpBuilder = new HTTPBuilder("http://<yourJenkinsEndpointHere>/job/<yourJobPath>/buildWithParameters")
//Set Username and password here as authentication

def username='yourJenkinsEmailHere'
//Go to jenkins profile, create auth token, said user you make token for needs access to the build
def password='11122712371237139929s9s9see78467acacdd8099cc4'

String userPassBase64 = "$username:$password".toString().bytes.encodeBase64()

httpBuilder.setHeaders(["Authorization": "Basic $userPassBase64"])
httpBuilder.request(Method.POST, ContentType.JSON) {
uri.query = data
response.success = {resp, json ->
log.warn "request succeed with status ${resp.status}"
//log.warn "request succeed with status ${resp.status}, response body was [${resp.entity.content.text}]"
}
response.failure = { resp, json ->
log.warn "request failed with status ${resp.status}"
//log.warn "request failed with status ${resp.status} response body was [${resp.entity.content.text}]"
}
}
}

0 votes
nihcet June 1, 2021

Hi Beril,

I want to trigger a job on Jenkins from post function on Jira. Did you can solution your problem?

Thanks

Connor White June 4, 2021

@nihcet  Please check out my reply below. I think it could help you. 

0 votes
beril kurt September 7, 2018

Any idea?

beril kurt September 7, 2018

UPDATE: When I right click the Trigger Jenkins Build button and copy the link I saw the following Error

{"message":"https","stack-trace":"java.net.UnknownHostException: https\n\tat Script138$_run_closure1.doCall(Script138.groovy:43)\n\tat com.onresolve.scriptrunner.runner.rest.common.UserCustomScriptEndpoint.doEndpoint(UserCustomScriptEndpoint.groovy:379)\n\tat com.onresolve.scriptrunner.runner.rest.common.UserCustomScriptEndpoint.getUserEndpoint(UserCustomScriptEndpoint.groovy:267)\n","status-code":"INTERNAL_SERVER_ERROR"}

Suggest an answer

Log in or Sign up to answer