The Jenkins automation server allows to automate the building and testing of software project. In this tutorial, we will see how can we automate the building and testing of a fictitious software project.

In the beginning of times

Suppose, you are developing a learning software named abc. The source code of your abc project is already located in git repository in my-git.com machine and can be accessed via ssh protocol - ssh://root@my-git.com/projects/abc.git.

Your workflow for building your software is simple. First, you clone the git repository to your local machine, then manually invoke the build tools.

Jenkins - the old workflow
user@abc-01:~ # env GIT_SSH_COMMAND='ssh -i abc.key' git clone ssh://root@my-git.com/projects/abc.git
Cloning into 'abc'...
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 6 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (6/6), done.
Checking connectivity... done.
user@abc-01:~ # cd abc
user@abc-01:~/abc # echo 'Invoke commands to build the software'
...

After several builds, you find that this workflow is error prone and time consuming. You decide to automate the build process. In the following steps - we will see how Jenkins can help you with this task.

Step 1 - Let’s build manually with Jenkins

First, we need to write a script which will be used by Jenkins to build and test our software. In the following steps, we will tell Jenkins to run this script every time there is a change in our source tree and to report us the results. In other words, when we commit or push changeset to our git repository, Jenkins will invoke this script and when the script is done , It will notify us via email whether the build process was successful.

From Jenkins viewpoint, we can invoke any command or build tool to build and test our software. Jenkins will consider the build process as successful if the exit code of this command or build tool is 0. Any other exit code, will be is a sign that the build has failed.

First, Clone our git repository to our development machine:

Build manually with Jenkins
user@abc-01:~ # env GIT_SSH_COMMAND='ssh -i abc.key' git clone ssh://root@my-git.com/projects/abc.git
Cloning into 'abc'...
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 6 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (6/6), done.
Checking connectivity... done.
user@abc-01:~ # cd abc
user@abc-01:~/abc #

Now, Create a new build.sh. In this tutorial, this script will simply print that it was invoked and exit. In real life, this script will call our build tools (For example make, ant, rake … ).

build.sh - The build script
echo 'Hello from build script :-)'
exit 0

Now, add, commit this file and push the changes:

Add build.sh to the git repository
user@abc-01:~/abc # git add build.sh
user@abc-01:~/abc # git commit -m "adding build script"
[master 67e9a97] adding build script
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 build.sh
user@abc-01:~/abc # env GIT_SSH_COMMAND='ssh -i abc.key' git push
...
To ssh://root@my-git.com/projects/abc.git
 8be7ded..1935cca  master -> master

Now, Open Jenkins on your web browser. Go to New Item. The following screen should appear:

Jenkins - Creating Freestyle project

  • In item name enter abc
  • select Freestyle project and click OK

Now , Go to Source Code Management section and select git :

Jenkins - Setting git as source code management system

  • Fill Repository URL - in this tutorial, The repository URL is ssh://my-git.com/projects/abc.git
  • Click add button to the left of Credentials field and select Jenkins and add your credentials. In this tutorial we use user with private key method.

Jenkins - Add credentials

After clicking ok, a new abc key credential will appear in credentials list. select it and wait - If Jenkins can connect to the repository, you will not see any red error message.


Go to Build section, click in Add build step, select Execute shell and enter the following script which call our build.sh and return its exit code:

Jenkins - Add build step
bash -ex build.sh
exit $?

Jenkins - Add build step , execute shell


Click the Save button in the bottom of the page to apply the changes. We are now ready to test our build.

Testing build process

Click Build now, You should see a new build in job list display.

Jenkins - job list display

Clicking the build, will show more details about it.

Step 2 - Let’s build automatically with Jenkins

In step we trigger the build each time a commit is push to our git repository and request Jenkins to send us 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.

Jenkins - Trigger builds remotely

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 trigger method to build the software every time a commit is pushed to our git repository. git allows to run a scripts located at hooks when certain events occurs. 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).

The post-update GIT hook
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:

Jenkins - The post-update GIT hook - Initiate the build
#!/bin/sh
echo "Trigger a build"
curl -s 'http://my-jenkins.com/buildByToken/build?job=abc&token=SECRET'

Now, we make it 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

Jenkins - Please notify me via email about build results

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

build fails
echo 'Hello from build script :-)'
exit 1
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.

Jenkins - Build become unstable

Build become stable again

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

Build successes
echo 'Hello from build script :-)'
exit 0
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.

Jenkins - Build become stable again