Sunday, December 25, 2016

Test Drive Jenkins using Docker - Part II

Instructions Summary

  1. Part I: Install Docker
  2. Part I: Clone repository from GitHub
  3. Part I: Download and start up Jenkins 
  4. Part II: Add new project "PULLCODE" to connect to GitHub repository
  5. Part II: Add new project "BUILD" to build project with Maven
  6. Part II: Run MariaDB and test BUILD
  7. Part III: Install and create Build Pipeline
  8. Part III: Install JaCoCo to check for test coverage
  9. Part III: Install Build Monitor View and schedule run interval

4. Add new project "PULLCODE"

Create new item.

Name your project "PULLCODE" and select Freestyle project. Click OK to create.

Now, we will connect this project to our GitHub repository. But before you can do that you need to install the Git plugin first.

Click Back to Dashboard

Click Manage Jenkins

Click Manage Plugins

Go to tab Available and search for GitHub Plugin. Select it and Click Install without restart.

Wait until the all processes are completed.

Restart Jenkins by going to http://localhost:8080/restart and click Yes.

Wait until you get back to the first screen.

Go back to project PULLCODE's config.

In the section Source Code Management, select Git and input your repository URL. In the field Credential, click Add --> Jenkins.

Input Username and Password of your GitHub account and name your credential in the Description field as your reminder. Click Add.

Select Credential with the one you've just added and then click Save.

Test your project by clicking Build Now.

A new item appears in the Build History. Click on small triangle icon to expand the menu.

Select Console Output

You should see the build is finished successfully. Please note the PULLCODE's workspace location.

5. Add new project "BUILD"

Add another Freestyle project "BUILD". 

In the project config, click Advance... in the Advanced Project Options.

Choose Use custom workspace and input PULLCODE's workspace location. Click Save.

Go to Manage Jenkins --> Configure System.

In JDK section, click Add JDK.

Uncheck Install automatically, name your JDK installation, and input /usr/lib/jvm/java-8-openjdk-amd64 in JAVA_HOME field. (In case you don't use Docker, put your local JDK directory here e.g. C:\Program Files\Java\jdk1.8.0_111)

Move down to  Maven section, click Add Maven...

Select Install automatically and Name your Maven installation. Click Save.

Go back to the project BUILD's config and go down to the Build section, click Add build step and select Invoke top-level Maven targets.

Select Marven Version with the one you've just created and input clean install in the Goals field. Click Save.

Build the project and wait until it finishes. Your build should end in failure.

If you go up and analyze your build log, you will see the failure because it could not connect to the database MariaDB. 

6. Install MariaDB and test BUILD

Note: If you use this alternative repository, you don't need MariaDB.
In your windows command line, put docker run -d --name mariadb -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -e MYSQL_DATABASE=for_test mariadb:10.1.17 and press enter.
This will pull MariaDB version 10.1.17 and run it automatically. Use command docker ps to display all containers. You should see 2 containers are running now including the mariadb.

Check if the database is created properly by using command docker exec -it mariadb mysql -u root -p"123456" and then followed by show databases; and you will see something like this:
PS: If you want to install MariaDB locally, you can download it from here. Then you may have to manually create the database named for_test.

If you use Docker, you need to perform the following steps to point JDBC connection to the Maria DB container you've created.

  1. Check Maria DB container you've just created by using command docker inspect mariadb and note the IP address in the line like: "IPAddress": "",
  2. Go to your working directory and edit the file in folder /model/src/test/resources/.
  3. Replace localhost with the Maria DB's IP Adrress from step 1 and Save
  4. Repeat the same steps with the same file name in folder /model/src/main/resources/.
  5. Use these commands to push your changes to your remote Git repository.
    git add .
    git commit -m "Updated MariaDB host"
    git push
  6. Rebuild the project PULLCODE

Now, you are ready to go. Rebuild the project BUILD and you should get a successful build.

Continue in Part III...


  1. Updates section 6 to make changes on local repository and then push to remote repository instead of changing right on remote repository.

  2. Replaced console outputs with Gist