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 onJenkins
- 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
fromGithub
toJenkins
- 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
- Create an account on Vultr
- 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
- Head to
Products > Server > Server Details
and note downip address
,username
andpassword
It should look something like107.191.57.92
root
andv3j4by@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
- Open your browser and head to
<ip address>
e.g.http://107.191.57.92
- It should ask for your
initial password
that you copied in the previous steps ChooseInstall Suggested Plugins
(takes around 10mins) Create anew
username and password for web access to your newly created Jenkins instance Share theip address
together with yournew
username and password with your team members so they can see faillure and success of builds - Adding support for
Jacoco
Head toJenkins > Manage Jenkins> Manage Plugins > Available >
and Type 'Jacoco', select the checkbox then clickInstall without restart
ClickRestart Jenkins when installation is complete and no jobs are running
- Adding support for
Gradle
Head toManage Jenkins > Global Tool Configuration > Gradle > Add Gradle >
Type any name e.g.mygradle691
- Adding support for
Github Enterprise
Head toManage Jenkins > Configure System > GitHub Enterprise Servers >
Set API Endpoint tohttps://github.sydney.edu.au/api/v3
Set Name toGithub USYD
Setting up the webhook from Github to Jenkins
- Repo https://github.sydney.edu.au/FRFU8670/fruitshop.git
- Payload URL - http://<MY IP ADDRESS>/github-webhook/
- Content type : application/json
- Disable SSL verification
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
ChooseGit
Repository URL -https://github.sydney.edu.au/FRFU8670/fruitshop.git
Setup your credentials Credentials Click onAdd
>Jenkin
and fill in your Username : frfu8670 (your unikey) Password : xxxxxxxx (your unikey password) Click onAdd
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 inclean 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 pressSave
**/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 pressSave
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 calledApp.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 pressSave
**/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
ChooseGitHub hook trigger for GITScm polling
and pressSave
- 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 onAdd 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 inH/1 * * * *
underneathSchedule
- 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 yourapp/build.gradle
filejar
jar {
manifest {
attributes(
"Main-Class": 'fruitshop.App'
)
}
}
- Then head over to
Jenkins
>Your project
>Configure
>Add post-build action
ChooseArchive the artifacts
UnderFiles to archive
type inapp/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 therun
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