Updating Jira tickets from Jenkins workflow (jenkinsfile)
Asked Answered
G

5

6

How can I update a jira issue from within a Jenkinsfile (jenkins-worflow/pipeline)? Is there a way I could use the Jira Issue Updater plugin as a step in the Jenkinsfile?

I know I could use the Jira RestAPI, but I'm trying to figure out if I can re-use the functionality provided by the jira-updater-issue.

What I'm looking for is a something similar to the example below calling Junit archiver, and atifact archiver, but calling jira updater.

    node {
      git url: 'https://github.com/jglick/simple-maven-project-with-tests.git'
      def mvnHome = tool 'M3'
      sh "${mvnHome}/bin/mvn -B -Dmaven.test.failure.ignore verify"
      step([$class: 'ArtifactArchiver', artifacts: '**/target/*.jar', fingerprint: true])
      step([$class: 'JUnitResultArchiver', testResults: '**/target/surefire-reports/TEST-*.xml'])
    }
Geriatrics answered 4/2, 2016 at 22:55 Comment(0)
K
11

The Jira Plugin is compatible with Pipeline.

This should work:

step([$class: 'hudson.plugins.jira.JiraIssueUpdater', 
    issueSelector: [$class: 'hudson.plugins.jira.selector.DefaultIssueSelector'], 
    scm: [$class: 'GitSCM', branches: [[name: '*/master']], 
        userRemoteConfigs: [[url: 'https://github.com/jglick/simple-maven-project-with-tests.git']]]]) 

You can get a full reference in the built-in Pipeline Snippet Generator.

Kanazawa answered 27/2, 2016 at 14:35 Comment(0)
D
2

As this was way harder for me than it should be, here is a working example. This will update a custom field of a ticket with a specific value:

step([$class: 'IssueFieldUpdateStep',
        issueSelector: [$class: 'hudson.plugins.jira.selector.ExplicitIssueSelector', issueKeys: ticket],
        fieldId: field,
        fieldValue: value
    ])

The snippet generator did not work for me. The variables ticket, field and value are all strings. Starting from this you can look for options here: https://www.jenkins.io/doc/pipeline/steps/jira/

Dessertspoon answered 24/3, 2021 at 6:51 Comment(1)
Additionally, if you want to tag multiple tickets, the ticket String needs to be a comma-separated string of tickets, without ANY whitespace between the commas and the tickets.Hunchbacked
O
0

The JIRA Steps Plugin provides a more declarative way to update an existing Jira Ticket:

node {
  stage('JIRA') {
    # Look at IssueInput class for more information.
    def testIssue = [fields: [ // id or key must present for project.
                               project: [id: '10000'],
                               summary: 'New JIRA Created from Jenkins.',
                               description: 'New JIRA Created from Jenkins.',
                               customfield_1000: 'customValue',
                               // id or name must present for issuetype.
                               issuetype: [id: '3']]]

    response = jiraEditIssue idOrKey: 'TEST-01', issue: testIssue

    echo response.successful.toString()
    echo response.data.toString()
  }
}

Since you would like to use the Jenkinsfile to define your pipeline, that should be the preferred way for you to go...

Oraliaoralie answered 19/6, 2017 at 12:55 Comment(0)
I
0

Building on amuniz's answer, if you're running in a pipeline that has already done a checkout, you can simplify this further:

step([$class: 'JiraIssueUpdater',
    issueSelector: [$class: 'hudson.plugins.jira.selector.DefaultIssueSelector'],
    scm: scm])
Incunabulum answered 14/12, 2023 at 22:7 Comment(0)
T
-1

Yes, seems like this page answers your question:

https://wiki.jenkins-ci.org/display/JENKINS/Jira+Issue+Updater+Plugin

After you install the plugin, add a build step, or pre/post build step to call this plugin

There you can give it the REST URL to your Jira server, the creds and the JQL to find the issues

Transpadane answered 5/2, 2016 at 9:20 Comment(2)
Hi thanks, but I'm looking for an example using the jenkins-workflow dsl. I think the docs you're pointing to refer to using the jira-updater from a regular (non-workflow) project, right? I mean, it tells you how to use the jenkins ui. What I'm looking for is the way to use it from a workflow jenkinsfile.Geriatrics
Agreed. This is not an answer.Spermatogonium

© 2022 - 2024 — McMap. All rights reserved.