I recently learned about withCredentials
DSL thanks to an answer to this question.
Having attempted to use @RamKamath's answer, i.e. the following Jenkinsfile:
pipeline {
agent any
stages {
stage( "1" ) {
steps {
script {
def credId = "cred_id_stored_in_jenkins"
withCredentials([usernamePassword(credentialsId: credId,
passwordVariable: 'password',
usernameVariable: 'username')]) {
String url = "https://bitbucket.company.com/rest/build-status/1.0/commits"
String commit = '0000000000000000000000000000000000000001'
Map dict = [:]
dict.state = "INPROGRESS"
dict.key = "foo_002"
dict.url = "http://server:8080/blue/organizations/jenkins/job/detail/job/002/pipeline"
List command = []
command.add("curl -f -L")
command.add('-u ${username}:${password}')
command.add("-H \"Content-Type: application/json\"")
command.add("-X POST ${url}/${commit}")
command.add("-d \''${JsonOutput.toJson(dict)}'\'")
sh(script: command.join(' '))
}
}
}
}
}
}
...the curl
command itself fails because of a reported "Bad request" error. This is the snippet from the Jenkins console output:
+ curl -f -L -u ****:**** -H "Content-Type:application/json" -X POST https://bitbucket.company.com/rest/build-status/1.0/commits/0000000000000000000000000000000000000001 -d '{"state":"INPROGRESS","key":"foo_002","url":"http://server:8080/blue/organizations/jenkins/job/detail/job/002/pipeline"}'
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
100 153 0 0 100 153 0 4983 --:--:-- --:--:-- --:--:-- 5100
curl: (22) The requested URL returned error: 400 Bad request
I understand that -u ****:****
is the masked username:password argument to -u
.
If I copy/paste that exact string into a shell, and replace the masked values with the real values, the curl
command works:
$ curl -f -L -u super_user:super_password -H "Content-Type:application/json" -X POST https://bitbucket.company.com/rest/build-status/1.0/commits/0000000000000000000000000000000000000001 -d '{"state":"INPROGRESS","key":"foo_002","url":"http://server:8080/blue/organizations/jenkins/job/detail/job/002/pipeline"}'
$
What is going wrong? Why does the curl
command result in error 400
/"Bad request" when Jenkins executes it, but the same command runs fine when executed manually?
Please note: as was recommended, I enclosed the -u ${username}:${password}
in single-quotes, not double-quotes.
Update:
I feel as though something is wrong with the string interpolation, because if I modify the Jenkinsfile to add a hardcoded username/password, i.e.
command.add('-u super_user:super_password')
...instead of
command.add('-u ${username}:${password}')
...then the curl
command still fails exactly as before, i.e. because of the error: 400 Bad request
Can someone please help me identify what is wrong, presumably with the command assembly, and/or the sh()
invocation?
Update
I've simplified the problem by removing the withCredentials()
. Even this simplified curl
invocation fails:
pipeline {
agent any
stages {
stage( "1" ) {
steps {
script {
def credId = "cred_id_stored_in_jenkins"
String url = "https://bitbucket.company.com/rest/build-status/1.0/commits"
String commit = '0000000000000000000000000000000000000001'
Map dict = [:]
dict.state = "INPROGRESS"
dict.key = "foo_002"
dict.url = "http://server:8080/blue/organizations/jenkins/job/detail/job/002/pipeline"
List command = []
command.add("curl -f -L")
command.add('-u super_user:super_password')
command.add("-H \"Content-Type: application/json\"")
command.add("-X POST ${url}/${commit}")
command.add("-d \''${JsonOutput.toJson(dict)}'\'")
sh(script: command.join(' '))
}
}
}
}
}
See Question&Answers more detail:
os