How to fetch attachments for JIRA issues in Jenkins pipeline?

I’m having trouble with the JIRA Pipeline Steps plugin in Jenkins. I want to grab all the attachments for a JIRA issue and save them in my Jenkins workspace. But I’m stuck.

The plugin has a jiraDownloadAttachment function, but it needs the attachment ID. Problem is, I can’t find any way to get that ID from the issue number. Even jiraGetIssue doesn’t seem to give me this info.

Am I missing something? Do I need to add extra parameters to jiraGetIssue to see the attachment details? Or is there another way to get all the attachments for an issue?

Here’s a bit of what I’ve tried:

def issueKey = 'PROJ-123'
def issue = jiraGetIssue idOrKey: issueKey

// This doesn't work - no attachment info
println issue.fields.attachment

// What I want to do, but can't
issue.fields.attachment.each { attachment ->
    jiraDownloadAttachment id: attachment.id, file: attachment.filename
}

Any help would be awesome. I’m really scratching my head over this one!

I’ve actually tackled this problem before in one of my projects. The JIRA Pipeline Steps plugin can be a bit tricky when it comes to attachments. What worked for me was using the Groovy HTTP Builder library. It’s more flexible and gives you direct access to JIRA’s REST API.

Here’s a rough outline of how I did it:

First, add the HTTP Builder dependency to your Jenkins pipeline.
Then, create an HTTP client with your JIRA credentials.
Make a GET request to the JIRA issue endpoint, including the ‘attachment’ field.
Parse the JSON response and extract the attachment info.
Loop through the attachments and download each one.

It takes a bit more setup, but it’s way more reliable than wrestling with the plugin limitations. Plus, you get full control over the API response, which is always nice.

Just remember to handle your JIRA credentials securely - don’t hardcode them in your pipeline script!

I’ve encountered this issue before, and found a workaround using the REST API Step plugin. Here’s what worked for me:

  1. Install the REST API Step plugin in Jenkins.
  2. Use the ‘httpRequest’ step to call the JIRA REST API directly.
  3. Parse the JSON response to extract attachment details.
  4. Iterate through attachments and download each.

Here’s a snippet to illustrate:

def issueKey = 'PROJ-123'
def response = httpRequest authentication: 'jira-creds', url: "${JIRA_URL}/rest/api/2/issue/${issueKey}?fields=attachment"

def attachments = readJSON text: response.content

attachments.fields.attachment.each { attachment ->
    jiraDownloadAttachment id: attachment.id, file: attachment.filename
}

This approach bypasses the limitations of the JIRA Pipeline Steps plugin for fetching attachments.

hey tom, i had similar issue. try using jira rest api directly. u can make http request to /rest/api/2/issue/{issueKey}?fields=attachment and parse json response. it’ll give u attachment ids n filenames. then use those wit jiraDownloadAttachment. hope this helps!