【问题标题】:Passing text with escape characters from Jenkins into Groovy script将带有转义字符的文本从 Jenkins 传递到 Groovy 脚本
【发布时间】:2020-09-22 22:19:19
【问题描述】:

我正在尝试从 Jenkins 的凭据存储中检索用户名/密码组合并将其传递到 cURL。这完全可以正常工作,直到密码中有一个特殊字符,如“!”这将导致它出错。

String url = ["https://stash.myorg.com/stash/rest/api/1.0/projects", projectKey,
              "repos", repoSlug, "browse", repoDirPath, filename].join("/")
url = url.replace(" ", "%20")

// call to helper function
final String srcCommitId = getPreviousCommitId(projectKey, repoSlug, branchName)

withCredentials([usernamePassword(credentialsId: credId, usernameVariable: credUsername,
        passwordVariable: credPassword)]) {

    final String curl = 'curl -X PUT -u ' + credUsername + ':' + credPassword + ' -F content=@\"' +
            filename +'\" -F message="Revised document" -F branch=' +
            branchName + ' -F sourceCommitId=' + srcCommitId + ' ' + url


    dir (‘localpath/docs’) {
        
        def response
        response = sh(script: curl, returnStdout: true)
        println(response)
        
    }
}

我试图通过 Jenkins 的 Http 插件将 HTTP PUT 放入,但它想以 application/x-www-form-urlencoded 的形式发布,而不是普通的形式。关于如何转义字符串以使其不会出错的任何想法?谢谢!

【问题讨论】:

  • 我们应该猜测错误信息还是它发生的位置?
  • 道歉......正在发生的错误是它没有正确解释密码并且身份验证失败。如果我的密码没有“!”,它可以正常工作。 {"errors":[{"context":null,"message":"身份验证失败。请检查您的凭据并重试。","exceptionName":"com.atlassian.bitbucket.auth.IncorrectPasswordAuthenticationException"}]}。
  • groovy 不需要对! 进行转义。但是在外壳中它确实如此。尝试将user:pass 转义为单引号'uesr:pass'

标签: linux jenkins groovy jenkins-pipeline jenkins-groovy


【解决方案1】:

好的...我想通了,这很愚蠢。在参数化用户名/密码的过程中,我删除了“$”。将代码更新为以下代码后,一切正常。

final String curl = 'curl -X PUT -u $' + credUsername + ':$' + credPassword + ' -F content=@\"' +
        filename +'\" -F message="Revised document" -F branch=' +
        branchName + ' -F sourceCommitId=' + srcCommitId + ' ' + url

【讨论】:

    猜你喜欢
    • 2021-09-09
    • 1970-01-01
    • 1970-01-01
    • 2019-08-22
    • 1970-01-01
    • 2022-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多