Call Jenkins job through jira by passing issue key in the URL through custom post function

I am new to Jenkins and jira tools. I am trying to call Jenkins job from jira tool when ticket has been closed using custom post function(using Groovy script) in jira and I want to pass issue key in the Jenkins URL but I am unable to do that. I tried with webhook but I was unable to do it.  Can any one please give me suggestion in this problem.

2 answers

i have a sample groovy script to do the same

 

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.customfields.option.Option;
CustomFieldManager customFieldManager=ComponentManager.getInstance().getCustomFieldManager();

CustomField customField_component=customFieldManager.getCustomFieldObjectByName("component");
CustomField customField_version=customFieldManager.getCustomFieldObjectByName("version");

Issue issue = issue;

def component=issue.getCustomFieldValue(customField_component);

def version=issue.getCustomFieldValue(customField_version);
def Issueid=issue.getKey()

def curl = "C:/Softwares/curl/curl.exe http://Jenkins_job_url/buildWithParameters?Components=$comp&Version=$version&Issueid=$Issueid"
def output = curl.execute().text

 

 

here Component and version are the custom field values.... if you are good in groovy, you can modify the script accordingly as per your requrement

Thanks Arju,
I am new to groovy script and I used the following script to trigger jenkins job
import com.atlassian.jira.issue.Issue
def launch= ["wget", "--auth-no-challenge", "--user=username", "--password=password", "https://onejenkins.verizon.com/vz/job/VZW.MYV.JIRA.test/build?token=jira&delay=0sec&JiraIssue=${issue.key}"].execute().text;
println launch

this will trigger when the issue has been closed. I need status of issue and assignee to pass in jenkins url.
Any suggestions .

Hi Arjun,

 

I'm using below script to call the jenkins build but it's having problem when I use https  protocol for jenkins url.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.customfields.view.CustomFieldParams
import com.atlassian.jira.issue.link.RemoteIssueLinkBuilder
import com.atlassian.jira.issue.link.RemoteIssueLinkManager
import java.net.URL
import java.net.URLEncoder
import java.io.OutputStreamWriter
import groovy.json.JsonSlurper
import com.opensymphony.workflow.InvalidInputException

import org.apache.log4j.Category

def Category log = Category.getInstance("invokeJenkinsBuild.groovy")
log.setLevel(org.apache.log4j.Level.DEBUG)


try {
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def key = issue.getKey()
def domainApplication = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Domain/Application")) as CustomFieldParams
def values = domainApplication.toString().replace("{","").replace("}","").split(", ")
def domain = values[0].toString().split('=')[1]
def application = values[1].toString().split('=')[1]
def svnRevision = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("SVN Revision"))
def svnLocation = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("SVN Directory")).toString()
def releaseVersion = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Release Version"))
def developmentVersion = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Development Version"))

log.debug("Jenkins domain code: $domain")
log.debug("Jenkins application code: $application")

if (svnLocation != "trunk") {
svnLocation = svnLocation + "/" + issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("SVN Sub-Directory"))
}

def performRelease = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Perform Release")).getValue().toString()

if (performRelease == "Yes") {
buildType = "release"
} else {
buildType = "snapshot"
}

def jenkinsJob = "https://xxxxxxx.net/"
def params = "SVN_REVISION=$svnRevision&ISSUE_NUMBER=$key&DOMAIN=$domain&APPLICATION=$application&SVN_LOCATION=$svnLocation&MAVEN_BUILD_TYPE=$buildType&RELEASE_VERSION=$releaseVersion&DEVELOPMENT_VERSION=$developmentVersion"
//def jenkinsBuildUrl = new URL("$jenkinsJob" + "buildByToken/buildWithParameters?job=xxxxxxxx$domain-$application&token=JIRA")
def jenkinsBuildUrl = new URL("$jenkinsJob" + "buildByToken/buildWithParameters?job=xxxxxxxx&token=JIRA")
log.debug("Jenkins build code: $jenkinsBuildUrl")
def connection = jenkinsBuildUrl.openConnection()

log.debug("LCM number is $key")
log.debug("SVN_REVISION=$svnRevision&ISSUE_NUMBER=$key&DOMAIN=$domain&APPLICATION=$application&SVN_LOCATION=$svnLocation&MAVEN_BUILD_TYPE=$buildType&RELEASE_VERSION=$releaseVersion&DEVELOPMENT_VERSION=$developmentVersion")
log.debug("Jenkins connection code: $connection")

connection.setDoOutput(true)
connection.getOutputStream().write(params.getBytes("UTF-8"))

def responseCode = connection.getResponseCode()

log.debug("Jenkins response code: $responseCode")
if (responseCode == 404) {
throw new InvalidInputException("No Jenkins automated build job was found for this Domain/Application. Please contact.")
}

} catch (e) {
throw new InvalidInputException(e.getMessage())
}

Does this work with JIRA 4.3 version? I have written similar script, but it is not executing job in Jenkins

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Monday in Jira Ops

Jira Ops Early Access Program Update #1: Announcing our next feature and a new integration

Thanks for signing up for Jira Ops! I’m Matt Ryall, leader for the Jira Ops product team at Atlassian. Since this is a brand new product, we’ll be delivering improvements quickly and sharing updates...

320 views 0 7
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you