I try to launch a Jenkins build via its API using cURL:
#!/usr/bin/env bash
curl \
-i \
--fail \
--show-error \
-s \
-X POST \
-H 'Content-Type:application/json' \
-H 'Accept:application/json' \
--form json='{"parameter": [{"name":"COMPOSE_FULL_NAME", "value": "/redacted/docker-compose-prod.yml"}, {"name":"BRANCH", "value": "prod"}, {"name":"AD_USER", "value": "redacted"}, {"name":"AD_PASSWORD", "value": "redacted"}}]}' \
-u redactedUser:redactedToken \
-k \
https://jenkins-dck.redacted/job/elr-156344/job/stack_deploy/build \
and this is what I get:
curl: (22) The requested URL returned error: 400 Nothing is submitted
I tried several ways of passing POST data, like using -d
or --data-urlencode 'json={
but with no success so far.
Any idea what's going on ? the message doesn't say much and I can't access the logs of the jenkins backend.
This error means that an HTTPS request reaches to a HTTP (plaintext) listener.
The jenkins-rest library is an object oriented Java project that provides access to the Jenkins REST API programmatically to some remote API Jenkins provides. It is built using the jclouds toolkit and can easily be extended to support more REST endpoints.
ok, found it, you first need to disregard the docs here: https://wiki.jenkins.io/display/JENKINS/Remote+access+API. The proper method is described at https://wiki.jenkins.io/display/JENKINS/Parameterized+Build
use this API endpoint:
https://jenkins-dck.redacted/job/elr-156344/job/stack_deploy/buildWithParameters?param1=urlencode¶m2=urlencoded
Don't forget to quote the url in the CURL quote, since bash will mess with & symbols.
working example:
#!/usr/bin/env bash
curl \
-i \
--fail \
--show-error \
-s \
-X POST \
-H 'Content-Type:application/json' \
-H 'Accept:application/json' \
-u redactedUser:redactedToken \
-k \
"https://jenkins-dck.redacted/job/elr-156344/job/stack_deploy/buildWithParameters?BRANCH=prod&AD_USER=$SERVICE_ACCOUNT"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With