Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add comment to Jira issue from Jenkins pipeline?

I have a Jenkins job that is configured via a declarative pipeline script.

I would like to add a comment to the related Jira issue when a build passes / fails.

The plugins that are available don't give very good documentation with regards to using them with a pipeline. I have tried to use the "Jira Plugin" as it is explained in this answer:

Updating Jira tickets from Jenkins workflow (jenkinsfile)

step([$class: 'hudson.plugins.jira.JiraIssueUpdater', 
    issueSelector: [$class: 'hudson.plugins.jira.selector.DefaultIssueSelector'], 
    scm: [$class: 'GitSCM', branches: [[name: '*/develop']], 
        userRemoteConfigs: [[url: 'https://github.com/something.git']]]])

But I get this error:

java.lang.IllegalArgumentException: Unsupported run type org.jenkinsci.plugins.workflow.job.WorkflowRun
at hudson.plugins.jira.JiraIssueUpdater.perform(JiraIssueUpdater.java:69)
at org.jenkinsci.plugins.workflow.steps.CoreStep$Execution.run(CoreStep.java:78)
at org.jenkinsci.plugins.workflow.steps.CoreStep$Execution.run(CoreStep.java:65)
at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution$1$1.call(SynchronousNonBlockingStepExecution.java:49)
at hudson.security.ACL.impersonate(ACL.java:260)
at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution$1.run(SynchronousNonBlockingStepExecution.java:46)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

Has anyone successfully done this via the pipeline?

like image 523
simbro Avatar asked May 16 '17 12:05

simbro


1 Answers

Using jira-steps-plugin:

pipeline {
    stages {
        stage('jira') {
            steps {
                comment_issues()
            }
        }
    }
}

void comment_issues() {
    def issue_pattern = "TEST-\\d+"

    // Find all relevant commit ids
    currentBuild.changeSets.each {changeSet ->
        changeSet.each { commit ->
            String msg = commit.getMsg()
            msg.findAll(issue_pattern).each {
                // Actually post a comment
                id -> jiraAddComment idOrKey: id, comment: 'Hi there!'
            }
        }
    }
}
like image 178
Ulbe Avatar answered Sep 21 '22 03:09

Ulbe