Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,293,800
Community Members
 
Community Events
165
Community Groups

Trigger a Jenkins Build with UI from Jira

Edited

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

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}]"
}
}
}

Hi Beril,

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

Thanks

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

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"}

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you