Modifying Jira tasks from Jenkins pipeline script

Hey everyone! I’m trying to figure out how to change Jira issues directly from my Jenkins pipeline script. I know there’s a Jira Issue Updater plugin, but I’m not sure if I can use it as a step in my pipeline.

I could use the Jira REST API, but I’d rather use the plugin if possible. It would be great if I could do something like this:

pipeline {
    agent any
    stages {
        stage('Update Jira') {
            steps {
                script {
                    def projectKey = 'PROJ'
                    def issueKey = 'PROJ-123'
                    def newStatus = 'In Progress'

                    updateJiraIssue(projectKey: projectKey, issueKey: issueKey, status: newStatus)
                }
            }
        }
    }
}

Has anyone done this before? Any tips or tricks would be super helpful. Thanks!

I’ve implemented a similar solution in our CI/CD pipeline. The Jira Issue Updater plugin is indeed a viable option for your use case. Here’s a more robust example that might help:

pipeline {
    agent any
    stages {
        stage('Update Jira') {
            steps {
                script {
                    def issueKey = 'PROJ-123'
                    def newStatus = 'In Progress'
                    def comment = 'Updated by Jenkins pipeline'

                    jiraEditIssue site: 'DEFAULT', idOrKey: issueKey, fields: [
                        [fieldId: 'status', fieldType: 'option', fieldValue: newStatus]
                    ]
                    jiraAddComment site: 'DEFAULT', idOrKey: issueKey, comment: comment
                }
            }
        }
    }
}

This approach allows for more flexible issue updates, including adding comments. Remember to configure the Jira site in Jenkins global configuration. Also, ensure your Jenkins has the necessary permissions in Jira to make these changes.

I’ve actually tackled this issue in my previous job. While the Jira Issue Updater plugin is great, I found using the Jira REST API gave me more flexibility and control. Here’s a snippet that worked well for me:

def updateJiraIssue(issueKey, newStatus) {
    def jiraUrl = 'https://your-jira-instance.com'
    def jiraToken = 'your-jira-api-token'

    def response = httpRequest(
        url: "${jiraUrl}/rest/api/2/issue/${issueKey}",
        httpMode: 'PUT',
        contentType: 'APPLICATION_JSON',
        customHeaders: [[name: 'Authorization', value: "Basic ${jiraToken.bytes.encodeBase64().toString()}"]],
        requestBody: "{'fields': {'status': {'name': '${newStatus}'}}}",
        validResponseCodes: '204'
    )

    if (response.status == 204) {
        echo "Successfully updated ${issueKey} to ${newStatus}"
    } else {
        error "Failed to update Jira issue"
    }
}

It’s a bit more code, but it gives you full control over the API calls. Just remember to secure your Jira token properly in Jenkins credentials.

hey mandy, i’ve used the jira plugin in jenkins before. it’s pretty handy! you can def use it in your pipeline. try something like this:

jiraEditIssue site: ‘YOUR_JIRA_SITE’, idOrKey: issueKey, field: ‘status’, fieldValue: newStatus

just make sure you’ve got the plugin installed and configured in jenkins first. good luck!