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
Gradleto run builds onJenkins - Setting up
Junitto generate a test report - Setting up
Jacocoto generate a code coverage report - Excluding classes from the
Jacococode coverage report - Setting up a
WebhookfromGithubtoJenkins - Setting up a
jarfile as output of a jenkins build - Configuring
Gradleto 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 serverthen choose Server -Cloud compute, Server location -Sydney, Server type -Marketplace Apps > Docker > Ubuntu 20.04 x64 - Head to
Products > Server > Server Detailsand note downip address,usernameandpasswordIt should look something like107.191.57.92rootandv3j4by@341respectively
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/initialAdminPasswordOn your browser
- Open your browser and head to
<ip address>e.g.http://107.191.57.92 - It should ask for your
initial passwordthat you copied in the previous steps ChooseInstall Suggested Plugins(takes around 10mins) Create anewusername and password for web access to your newly created Jenkins instance Share theip addresstogether with yournewusername and password with your team members so they can see faillure and success of builds - Adding support for
JacocoHead toJenkins > Manage Jenkins> Manage Plugins > Available >and Type 'Jacoco', select the checkbox then clickInstall without restartClickRestart Jenkins when installation is complete and no jobs are running - Adding support for
GradleHead toManage Jenkins > Global Tool Configuration > Gradle > Add Gradle >Type any name e.g.mygradle691 - Adding support for
Github EnterpriseHead toManage Jenkins > Configure System > GitHub Enterprise Servers >Set API Endpoint tohttps://github.sydney.edu.au/api/v3Set 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 ManagementChooseGitRepository URL -https://github.sydney.edu.au/FRFU8670/fruitshop.gitSetup your credentials Credentials Click onAdd>Jenkinand fill in your Username : frfu8670 (your unikey) Password : xxxxxxxx (your unikey password) Click onAddClick 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 versionand select the gradle that you configured in step 4 of “On your browser” e.g.mygradle691 - Then under
Taskssection, type inclean build testwhich 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 reportand find the section that says Test report XMLs and enter the following and pressSave
**/test-results/**/*.xmlHow 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 reportand 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 reportand add the following under Exclusions. For example if you had a class calledApp.javaand 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.classHow can I ask Github to notify Jenkins when a new commit has been pushed?
- Head over to
Jenkins>Your project>Configure>Build TriggersChooseGitHub hook trigger for GITScm pollingand pressSave - Head over to
Github>Your repository>Settings>Hooks>Add webhookPayload URL -http://<your server ip>/github-webhook/e.g.http://107.191.57.92/github-webhook/then choose Content type -application/json, SSL Verification -Disablethen 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 TriggersChoose 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
jartask in yourapp/build.gradlefilejar
jar {
manifest {
attributes(
"Main-Class": 'fruitshop.App'
)
}
}
- Then head over to
Jenkins>Your project>Configure>Add post-build actionChooseArchive the artifactsUnderFiles to archivetype inapp/build/libs/app.jaror whatever the name of your app is called
How can I ask Gradle to wait for user input?
- Head over to your
app/build.gradlefile and give some configuration options to theruntask
run {
standardInput = System.in
}- Then every time you would like to run your application instead of
gradle runyou would execute
gradle run --console=plain