Tuesday 9 February 2016

jenkins_with_api

How we can use jenkins with the api.

Lets say we have a job called "test_job" and your username is "amund" and your token is "my_long_token_string" and your "test_job" is parameter driven, then:

consider your patameter name is: "name".

command example:

curl -X POST http://amund:my_long_token_string@jenkins_server:8080/job/test_job/build --data-urlencode json='{"parameter":[{"name":"name","value":"your value"}]}'



NOTE:

http://username:token@IP:port/job/job_name/build --data-urlencode json='{"parameter":[{"name":"name","value":"your value"}]}'


You can also use your username and password, but its not recommended:


curl -X POST --user <username>:<password> http://jenkins_server/job/your_job/build --form json='{"parameter":[{"name":"given_name","value":"your_value"}]}'

OR

curl -X POST --user <username>:<password> http://jenkins_server/job//your_job/build --form json='{"parameter":[{"name":"given_name","value":"your_value"}]}'



If you want to create a script on the top of the curl call, you can do that, note that within the json if you are using a variable you need to use as example:

example:
IPADDRESS=$@

in general we can use as ${IPADDRESS} or $IPADDRESS

inside json data, we need to use as:

"'"$IPADDRESS"'"

which is: " then ' then " and close the same.

example:

curl -X POST http://${USER}:${TOKEN}@${JENKINS_SERVER}/${JOB} --data-urlencode json='{"parameter":[{"name":"IPs","value":"'"$IPADDRESS"'"}]}'