You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Groovy脚本将JIRA附件传递至Jenkins作业的文件参数

通过Groovy+JIRA API实现JIRA附件到Jenkins文件参数的传递

以下是具体实现方案,包含JIRA API细节和Groovy脚本逻辑:

一、JIRA REST API 获取附件信息

要拉取指定Issue的附件,使用以下API端点:

GET /rest/api/2/issue/{issueIdOrKey}/attachments
  • 认证方式:采用Basic Auth,将JIRA用户名和密码拼接为username:password格式后做Base64编码,放入请求头的Authorization字段
  • 权限要求:发起请求的用户必须拥有该Issue的浏览权限,以及查看附件的权限
  • 响应核心字段:
    • content:附件的直接下载URL,可直接用于下载文件
    • filename:附件的原始文件名
    • id:附件的唯一标识(可选)

简化版响应示例:

[
  {
    "content": "https://your-jira-domain/secure/attachment/10000/test.zip",
    "filename": "test.zip",
    "size": 12345
  }
]

二、Groovy脚本实现传递逻辑

假设你在JIRA工作流的后置函数中执行Groovy脚本,核心逻辑为:获取附件→下载到临时文件→调用Jenkins API触发作业并上传文件参数。

完整示例脚本

import groovy.json.JsonSlurper
import groovyx.net.http.RESTClient
import java.io.File

// 配置项,根据实际环境修改
def jiraDomain = "https://your-jira-domain"
def jenkinsDomain = "https://your-jenkins-domain"
def targetJenkinsJob = "your-job-name"
def jiraIssueKey = issue.getKey() // 当前触发工作流的Issue Key
// 认证信息:替换为实际用户名和密码/API Token
def jiraAuth = "Basic " + Base64.getEncoder().encodeToString("jira-user:jira-pass".getBytes())
def jenkinsAuth = "Basic " + Base64.getEncoder().encodeToString("jenkins-user:jenkins-api-token".getBytes())
// Jenkins作业中定义的文件参数名
def jenkinsFileParamName = "project-attachment"

// 1. 调用JIRA API获取附件列表
def jiraClient = new RESTClient(jiraDomain)
jiraClient.defaultRequestHeaders.Authorization = jiraAuth
def attachResp = jiraClient.get(path: "/rest/api/2/issue/${jiraIssueKey}/attachments")
def attachments = new JsonSlurper().parseText(attachResp.data.toString())

if (attachments.size() == 0) {
    println "No attachments found for issue ${jiraIssueKey}"
    return
}

// 2. 下载第一个附件(如需处理多个,改为遍历数组即可)
def targetAttach = attachments[0]
def tempFile = new File(System.getProperty("java.io.tmpdir") + File.separator + targetAttach.filename)

// 下载附件到临时文件
def downloadClient = new RESTClient(targetAttach.content)
downloadClient.defaultRequestHeaders.Authorization = jiraAuth
def fileData = downloadClient.get(contentType: "application/octet-stream").data
tempFile.withOutputStream { out ->
    out << fileData
}

// 3. 调用Jenkins API触发作业并上传文件
def jenkinsClient = new RESTClient(jenkinsDomain)
jenkinsClient.defaultRequestHeaders.Authorization = jenkinsAuth
jenkinsClient.contentType = "multipart/form-data"

def postBody = [
    parameter: [
        [
            name: jenkinsFileParamName,
            file: tempFile
        ]
    ],
    json: '{"parameter": [{"name": "' + jenkinsFileParamName + '", "file": ""}]}'
]

jenkinsClient.post(
    path: "/job/${targetJenkinsJob}/build",
    body: postBody,
    requestContentType: "multipart/form-data"
)

// 4. 清理临时文件
tempFile.delete()

三、关键注意点

  • 权限校验:JIRA用户需能访问目标Issue的附件,Jenkins用户需拥有触发目标作业、上传文件参数的权限
  • 多附件处理:若Issue存在多个附件,可修改脚本遍历attachments数组,将所有附件传递给Jenkins(需Jenkins作业支持多文件参数)
  • 临时文件清理:脚本执行完成后务必删除临时文件,避免磁盘空间占用
  • Jenkins配置:确保Jenkins开启远程API访问(全局安全配置中勾选“允许使用远程API”并配置对应权限)

内容的提问来源于stack exchange,提问作者sipreeti

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 01:52:09