It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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.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", "${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 groovy.json.JsonSlurper
import com.opensymphony.workflow.InvalidInputException

import org.apache.log4j.Category

def Category log = Category.getInstance("invokeJenkinsBuild.groovy")

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 = ""
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("Jenkins connection code: $connection")


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
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Posted Oct 09, 2018 in Jira Core

How to manage many similar workflows?

I have multiple projects that use variations of the same base workflow. The variations depend on the requirements of the project or issue type. The variations mostly come in the form of new statuses ...

567 views 6 0
Join discussion

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