Issue
I am trying to retrieve a username/password combination from Jenkins's credential store and pass it into a cURL. This works completely fine until there's a special character in the password like "!" that will cause it to error out.
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)
}
}
I've tried to put the HTTP PUT through Jenkins's Http Plugin but it wants to post as an application/x-www-form-urlencoded and not as a normal form. Any thoughts on how to escape the string so it doesn't error? Thanks!
Solution
Okay... I figured this out and it's something dumb. In the process of parameterizing the username/password, I dropped the "$". Once I updated the code to the following below, everything worked fine.
final String curl = 'curl -X PUT -u $' + credUsername + ':$' + credPassword + ' -F content=@\"' +
filename +'\" -F message="Revised document" -F branch=' +
branchName + ' -F sourceCommitId=' + srcCommitId + ' ' + url
Answered By - ChrisS