How to add comment to Jira issue from Jenkins pipeline?
Asked Answered
K

1

6

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?

Kiele answered 16/5, 2017 at 12:7 Comment(0)
A
3

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!'
            }
        }
    }
}
Altamira answered 8/1, 2018 at 16:24 Comment(1)
se docs jenkinsci.github.io/jira-steps-plugin/steps/comment/…Patriarchate

© 2022 - 2024 — McMap. All rights reserved.