Your first Jenkins job - part 2

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:

Clone our git repository
 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 ... ).

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

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

Add build script to our 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:

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

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

  • 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.

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:

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

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.

Clicking the build, will show more details about it.