Jenkins JSON Result Build Request
I was looking for a way to request a build request through calling a url that would return JSON so that I would be able to setup a hook with git to validate unit tests after each push to the remote repository. I found url that looked like what I want so I tried:
http://www.doamin.com:8082/job/bare-bone-test/build/api/json
however when I try to do there in my browser I get the
Firefox has detected that the server is redirecting the request for this address in a way that will never complete
even though it does trigger a build of that job. Is this the correct way 开发者_高级运维to request a build that will return a JSON response?
According to the documentation:
Submitting jobs
For a job with no parameters, you need merely go an HTTP GET on
JENKINS_URL/job/JOBNAME/build?token=TOKEN
where TOKEN is set up in the job configuration.
If you have parameters, you need to send JSON. Here's a snipped of shell, with a few extra newlines to be more readable.
json="{\"parameter\": [{\"name\": \"taskfile\", \"value\": \"$taskfile\"}, {\"name\": \"task\", \"value\": \"$task\"}, {\"name\": \"jobParameters\", \"value\": \"$jobargs\"}], \"\": \"\"}" url=http://hudson.basistech.net/job/benson-segmentation-training/build curl -X POST $url -d token=zorn --data-urlencode json="$json"
I'm not quite sure if the TOKEN
field is really necessary. I was able to kick off a build using a simple get to the job//build URL:
curl -v http://localhost:8080/job/testjob/build
* About to connect() to localhost port 8080 (#0)
* Trying 127.0.0.1... connected
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /job/testjob/build HTTP/1.1
> User-Agent: curl/7.21.3 (x86_64-pc-linux-gnu) libcurl/7.21.3 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18
> Host: localhost:8080
> Accept: */*
>
< HTTP/1.1 302 Found
< Server: Winstone Servlet Engine v0.9.10
< Location: http://localhost:8080/job/testjob/
< Content-Length: 0
< Connection: Keep-Alive
< Date: Mon, 08 Aug 2011 20:48:37 GMT
< X-Powered-By: Servlet/2.5 (Winstone/0.9.10)
<
* Connection #0 to host localhost left intact
* Closing connection #0
For a git post-commit hook, this should be enough.
Redirect, wtf?
The reason you're seeing the infinite redirect issue, is that the build/api/json
URL redirects to the build/api
endpoint (I think the handler for the build
url tries to redirect back to the job status page by chopping off the last part of the URL). For some strange reason, build/api
redirects back to itself. I don't think that the build supports the api/json
suffix, but as shown above, it isn't needed.
精彩评论