curl - Jenkins Pipeline - 使用 sh 和curl 获取 java.io.NotSerializedException

标签 curl jenkins groovy jira jenkins-pipeline

我正在创建一个 Jenkins 管道,它在某个时刻创建一个 Jira Ticket。 不幸的是我得到: java.io.NotSerializedException: org.codehaus.groovy.runtime.EncodingGroovyMethods$1

代码:

    token = createEncodedToken()

    def curlCommand = "curl -H 'Authorization: Basic ${token}' -X POST -d \'${payload}\' -H 'Content-Type: application/json' ${jira_url} > JIRA_TICKET"

    try {
        println "executing: ${curlCommand}"
        sh "${curlCommand}"
    } catch (NotSerializableException err) {
        println "${err}"
    }

创建EncodedToken:

def createEncodedToken() {
   def auth = "${jira_user}:${jira_password}"
   return auth.bytes.encodeBase64()
}

生成的curlCommand 有效!我可以通过curl 或示例管道发送它。但一旦我在 jira.groovy 脚本中使用它,我就会遇到异常。

[Pipeline] echo
executing: curl -H "Authorization: Basic MYTOKEN=" -X POST -d "{"fields":{"project":{"key":"KEY"},"summary":"Pipeline Test","description":"Creating of an issue using project keys and issue type names using the REST API","issuetype":{"name":"Story"}}}" -H "Content-Type: application/json" https://MYJIRA/rest/api/2/issue/ > JIRA_TICKET
[Pipeline] sh
[plinetest] Running shell script
[Pipeline] echo
java.io.NotSerializableException: org.codehaus.groovy.runtime.EncodingGroovyMethods$1

输出文件 JIRA_TICKET 用于读取生成的问题 key 。

有趣的事实:虽然我遇到异常,但票证已创建...但我无法读取 JIRA_TICKET 文件。

我目前正在沙箱管道中测试它:

def jira = fileLoader.fromGit('jira.groovy', 'MYBITBUCKET-pipeline.git', 'master', 'CREDS', '')
echo "create ticket"
def payload = '{"fields":{"project":{"key":"KEY"},"summary":"Pipeline  Test","description":"Creating of an issue using project keys and issue type names using the REST API","issuetype":{"name":"Story"}}}'
jira.createTicket(payload)

该通话经过测试,工作正常。也许 jira lib 导致了这个问题? 我知道 Jenkins 管道的可序列化概念,但我不明白我错过了什么。 我现在很无能为力......感谢任何帮助。

最佳答案

encodeBase64() 不返回字符串。它返回一个实现 groovy.lang.Writeable 的类,该类具有 toString() 方法。

您需要显式调用 toString()。

关于curl - Jenkins Pipeline - 使用 sh 和curl 获取 java.io.NotSerializedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39296330/

相关文章:

php - 通过 PHP CURL 添加 Google 联系人

Jenkins:成功构建后如何执行powershell脚本

maven - SonarQube "The main branch has no lines of code."问题,而 Jenkins 说分析成功

security - 登录后更新用户的lastLogin属性

java - 限制每个请求映射处理的请求数量

java - 无法从 groovy 中的 json 数组中过滤对象

windows - Bash 函数自动执行未按预期执行的 curl POST 身份验证

java - 所需的字符串参数 'surname' 不存在

django - 使用 json 使用 djangorestframework 将图像上传到 imagefield 并使用 CURL 进行测试

适用于本地和云应用程序的 Azure DevOps CI/CD