【发布时间】: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