Setting up Jenkins on Cloud with Junit and Jacoco integrated via Github

Summary

The notes below are complimentary to the youtube video showing students how to setup Jenkins on the cloud along with

  • Setting up Gradle to run builds on Jenkins
  • Setting up Junit to generate a test report
  • Setting up Jacoco to generate a code coverage report
  • Excluding classes from the Jacoco code coverage report
  • Setting up a Webhook from Github to Jenkins
  • Setting up a jar file as output of a jenkins build
  • Configuring Gradle to wait for user input

The Video

https://www.youtube.com/watch?v=fCnmcsc7VEc

On your browser

  1. Create an account on Vultr
  2. Head to Products > Plus button > Deploy new server  then choose Server - Cloud compute , Server location - Sydney, Server type - Marketplace  Apps > Docker > Ubuntu 20.04 x64  
  3. Head to Products > Server > Server Details and note down ip address,  username and password It should look something like 107.191.57.92 root and v3j4by@341 respectively

On your terminal

  • Remote into your newly created instance via ssh
ssh <username>@<ip address>  
  • Create a docker container with Jenkins official image and give it a custom name myjenkins  , do this by executing the command
docker run -d --restart always -p 80:8080 -p 50000:50000 -v jenkins_home:/var/jenkins_home --name myjenkins jenkins/jenkins:lts-jdk11
  • Find your initial password by executing the command below, it should look something like a1234b32b32412asdf123vbdfgasfqwer
docker exec myjenkins cat /var/jenkins_home/secrets/initialAdminPassword

On your browser

  1. Open your browser and head to <ip address> e.g. http://107.191.57.92
  2. It should ask for your initial password that you copied in the previous steps  Choose Install Suggested Plugins (takes around 10mins) Create a new username and password for web access to your newly created Jenkins instance Share the ip address together with your new username and password with your team members so they can see faillure and success of builds  
  3. Adding support for Jacoco  Head to Jenkins > Manage Jenkins> Manage Plugins > Available > and Type 'Jacoco', select the checkbox then click Install without restart Click Restart Jenkins when installation is complete and no jobs are running  
  4. Adding support for Gradle  Head to Manage Jenkins > Global Tool Configuration > Gradle > Add Gradle > Type any name e.g. mygradle691  
  5. Adding support for Github Enterprise  Head to Manage Jenkins > Configure System > GitHub Enterprise Servers > Set API Endpoint to  https://github.sydney.edu.au/api/v3 Set Name to Github USYD  

Setting up the webhook from Github to Jenkins

FAQ

How can I ask Jenkins to pull down my source code from University of Sydney’s Github?

  • Ensure that you have configured Github Enterprise in step 5 of “on your browser
  • Jenkins > Your project > Configure > Source Code Management  Choose Git Repository URL - https://github.sydney.edu.au/FRFU8670/fruitshop.git Setup your credentials Credentials  Click on Add > Jenkin and fill in your  Username : frfu8670 (your unikey) Password  : xxxxxxxx (your unikey password) Click on Add   Click on the dropdown menu and choose your newly added credentials e.g. frfu8670/*****    

How can I ask Jenkins to run the tasks to build and test my project?

  • Ensure you have gradle configured properly for Jenkins - see step 4  in “On your browser”
  • Head over to Jenkins > Your project > Configure > Build > Invoke Gradle script > Invoke Gradle > Gradle version and select the gradle that you configured in step 4 of “On your browser” e.g. mygradle691
  • Then under Tasks section, type in clean build test which will run 3 separate tasks that will clean your project, build your project and run unit tests on said project

How can I add a report to show my unit test results?

  • Head over to Jenkins > Your project > Configure > Post-build Actions > Publish Junit test result report  and find the section that says Test report XMLs and enter the following and press Save
**/test-results/**/*.xml

How can I add a report to show my unit test coverage?

  • Ensure you have the Jacoco plugin installed for Jenkins - see step 3 in  “On your browser”
  • Head over to Jenkins > Your project > Configure > Post-build Actions > Record JaCoCo coverage report and press Save

How can I exclude a class from being included in the Test Coverage Report?

  • Head over to Jenkins > Your project > Configure > Post-build Actions > Record JaCoCo coverage report and add the following under Exclusions. For example if you had a class called App.java and which had non backend business logic such as asking for command line user input and cleaning and you wish to exclude in your test coverage report  you can enter the following in the input field undernearth the Exclusions and press Save
**/App.class

How can I ask Github to notify Jenkins when a new commit has been pushed?

  • Head over to Jenkins > Your project > Configure > Build Triggers  Choose GitHub hook trigger for GITScm polling and press Save  
  • Head over to Github > Your repository > Settings > Hooks > Add webhook  Payload URL - http://<your server ip>/github-webhook/ e.g. http://107.191.57.92/github-webhook/ then choose Content type - application/json , SSL Verification - Disable then click on Add webhook  

How can I ask Jenkins to find out if a new commit has been pushed without Github notifying it?

  • Head over to Jenkins > Your project > Configure > Build Triggers  Choose Poll SCM Type in H/1 * * * * underneath Schedule  
  • This will ask Jenkins to poll your repository every 1 minute for changes such as new  commit pushed and merged pull requests etc.

How can I ask Jenkins to build a jar file that contains my command line app?

  • Head over to project code and add  a jar task in your app/build.gradle file jar
jar {
    manifest {
        attributes(
            "Main-Class": 'fruitshop.App'
        )
    }
}
  • Then head over to Jenkins > Your project > Configure > Add post-build action  Choose Archive the artifacts Under Files to archive type in app/build/libs/app.jar or whatever the name of your app is called  

How can I ask Gradle to wait for user input?

  • Head over to your app/build.gradle file and give some configuration options to the run task
run {
   standardInput = System.in
}
  • Then every time you would like to run your application instead of gradle run you would execute
gradle run --console=plain