Your first Jenkins job - part 3

Step 2 - Let’s build automatically with Jenkins

In step we trigger the build each time a commit is pushed to our git repository and request Jenkins to send us an email with the results of the build.

First , Go to Manage Jenkins > Manage Plugins and install Build Authorization Token Root Plugin which allows to trigger build even when anonymous users cannot see Jenkins.

Go To Configure > Build Triggers and select Trigger builds remotely (e.g., from scripts). In Authentication Token enter your token. This token should be keeped private as any one who knows it will be able to trigger a build. In this tutorial, we will enter SECRET. Now, we apply the changes by clicking the Save button in the bottom of the page.

Now, You can trigger a build for the current job abc by accessing the url http://my-jenkins.com/buildByToken/build?job=abc&token=SECRET.

We will use this triggering method to build the software every time a commit is pushed to our git repository. git allows running scripts located at hooks when certain events occur. The post-update will be invoked after a commit is pushed to our git repository. So, we trigger a build by accessing the above URL (using curl).

Add git post trigger method
 user@abc-01:~# ssh my-git.com
 root@my-git:~# nano /projects/abc.git/hooks/post-update

Now, enter the following commands and save the file:

Inform Jenkines about the new push
 #!/bin/sh
 echo "Trigger a build"
 curl -s 'http://my-jenkins.com/buildByToken/build?job=abc&token=SECRET'

Now, we make it executable:

Make the script executable
 root@my-git:~# chmod 555 /projects/abc.git/hooks/post-update

Jenkins can notify us about the build results via email. Go to Configure > Post-build Actions and click Add post-build action and select E-mail notification

By default, email will be sent for every build failure or for every successful build after a failed one. Unchecking Send e-mail for every unstable build, will change this default behavior - Now, email will be sent for every build failure after successful one.

The Recipients of the mail notifications can be set in the Recipients field. You can also send notifications of build failures to users which change files at the current build, by checking Send separate e-mails to individuals who broke the build checkbox.

Click the Save button in the bottom of the page

Testing our automatic build

Congratulations, you automate the build of your software.

Now, Let’s test it

# Build become unstable

Change the build.sh to failure by exiting with code 1

Mimic a build failure
 echo 'Hello from build script :-)'
 exit 1
Push changes
 user@abc-01:~/abc # git commit -a -m "Build Failed"
 user@abc-01:~/abc # env GIT_SSH_COMMAND='ssh -i abc.key' git push

A new build should be triggered. when the build is done - you should receive an email about the failed build.

# Build become stable again

Now, Change the build.sh to success again by exiting with code 0

Mimic a build success
 echo 'Hello from build script :-)'
 exit 0
push changes
 user@abc-01:~/abc # git commit -a -m "Build Ok"
 user@abc-01:~/abc # env GIT_SSH_COMMAND='ssh -i abc.key' git push

A new build should be triggered. When the build is done - you should recieve an email about the successful build.