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.
Any idea?
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"}
Hi Beril,
I want to trigger a job on Jenkins from post function on Jira. Did you can solution your problem?
Thanks
Hey all, (my first ever community contribution here )
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.HTTPBuilderimport groovyx.net.http.Methodimport javax.ws.rs.core.Responseimport groovyx.net.http.ContentTypeimport com.atlassian.jira.issue.IssueManagerimport com.atlassian.jira.issue.MutableIssueimport com.atlassian.jira.component.ComponentAccessorimport com.atlassian.jira.issue.util.DefaultIssueChangeHolderimport com.atlassian.jira.issue.ModifiedValueimport com.atlassian.jira.component.ComponentAccessor
IssueManager im = ComponentAccessor.getIssueManager()MutableIssue issue = im.getIssueObject("SD-4672")
if(issue){//get the valuelog.info("Found Issue, retrieving custom fields..")//Input 1def im_first_name_cf_val = issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_15201"))//input 2def im_last_name_cf_val = issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_15202"))
//input 3def im_username_cf_val = issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_15203"))
//input 4def im_email_cf_val = issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_15204"))
//input 5def preferred_client_name_cf_val = issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_14302"))//input 6def 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 7def client_user_admin_cf_val = issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_14304"))
//selection 1def infrastructure_location_cf_val = issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_15205"))//selection 2def uploader_type_cf_val = issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_15206"))
// Input 8def zip_code_cf_val = issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_15700"))
//Store your jenkin parameter here as a mapdef 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 builddef password='11122712371237139929s9s9see78467acacdd8099cc4'
String userPassBase64 = "$username:$password".toString().bytes.encodeBase64()
httpBuilder.setHeaders(["Authorization": "Basic $userPassBase64"])httpBuilder.request(Method.POST, ContentType.JSON) {uri.query = dataresponse.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}]"}}}
@nihcet Please check out my reply below. I think it could help you.
It looks like you're new here. Sign in or register to get started.