I am working currently on a project which required automatic build triggering from remote to run my ansible-playbook which will configure our Instance launching under Auto Scaling.
We can do it using many ways but the easiest way to achieve it which I have used, I just want to share with you all.
Let’s start by configuring our Jenkins server first.
- Jenkins Server
- Jenkins Authentication Keys
Why Jenkins Authentication Keys?
Jenkins Authentication Keys required to authenticate yourself while requesting to trigger the build through Jenkins API from remote.
Go to Manage Jenkins then click on the label shown below.
Click on the user setting as shown below.
Now click on Add new Token.
Once you will click there It will generate the token for you, make sure to keep it safe else you need to generate it again as soon as you will leave the page.
Now let’s Create the test job “Auto-trigger-test” with the below configuration.
My requirements are of creating a parameterized build job. If your’s too then follow the below steps.
Now don’t get confused in Authorization token, put whatever you want instead of “build”, but make sure to remember, you will require this in the below step.
Save and apply the configuration.
Now we will use the curl command to call the API of Jenkins which will trigger our build.
curl -X POST "https://<username>:<token>@<jenkins_domain_url>/job/Auto-trigger-test/buildWithParameters?token=build&IP_ADDRESS=`hostname -I`"
- <username> This will be replaced by the username you use to login to the Jenkins server.
- <token> The token you have generated above.
- <jenkins_domain_url> this will be replaced by your Jenkins domain name or URL
- ** build: This can be replaced by whatever you have given above.
Now for securing our API token and calling it as an ENV Parameter is a pending task on which I am working, didn’t actually get time.