title | layout | sidebar | permalink | folder | comments |
---|---|---|---|---|---|
Maven Package Management with VSTS and Jenkins |
page |
java |
/labs/java/mavenpmjenkins/ |
/labs/java/mavenpmjenkins/ |
true |
In this exercise, you will configure a Jenkins build to get the source code from the VSTS repo, compile and then publish the MyShuttleCalc package to a VSTS Maven Package feed so that it can be consumed by MyShuttle2 and any other applications that require the calculation code.
This exercise assumes you have completed the exercises to create a Team Project, have set up the Docker private VSTS agent, and imported the MyShuttleCalc and MyShuttle2 GitHub repos into your VSTS team project. This exercise also assumes that you have cloned the repos in either IntelliJ or Eclipse. This exercise uses a team project named jdev-labs, though your team project name may differ.
{% include note.html content="This exercise shows how to do a Maven build using Jenkins. To see how to perform a Maven build using just VSTS Team Build that integrates into the VSTS Package Feed, please refer to Maven Package Management with VSTS.
Port 8080 is not open on the Azure VM for security purposes. However, since a local agent is running in Docker on the VM, it will be able to build and interact with VSTS." %}
Before getting to the build, you will need to create the Maven Package Feed and then update the Maven settings file in the project to specify where to publish the package to. All these instructions are in the Maven Package Management with VSTS Team Build lab. Please complete the following exercises from that lab:
- Create a Maven Package Feed
- Create a Maven Settings File with the Feed Credentials
In this task you will create an SSH key to authenticate to the VSTS repo. You need to create an SSH key if you do not already have one and then upload the public key to VSTS. The Jenkins job will use the key to authenticate to the repo.
-
On your VM, open a terminal by clicking on the Terminal Emulator icon in the toolbar.
-
Enter the following command:
ssh-keygen -C "jdev.com"
and press Enter thrice to use the default values for the id_rsa location and an empty pass phrase.
The domain is not important - use any value you want. You can also provide a pass phrase, though this will cause a prompt each time you use the key.
-
Enter the following command to print out the public key in the terminal:
cat /home/vmadmin/.ssh/id_rsa.pub
-
Select all of the text (from
ssh-rsa
tojdev.com
), right-click and select "Copy". -
Go back to Chrome and browse to your VSTS Team Project.
-
Click on your profile image in the upper right. In the menu, click Security.
-
Click on "SSH public keys" and click Add.
-
Enter "jdev" for the Description and then paste into the Key Data the contents of the public key from the clipboard.
Once you set the SSH credentials, you can use the SSH URL when cloning repositories.
In this task you will configure the Maven installation settings for Jenkins.
-
On your VM, open Chrome and browse to
http://localhost:8080
to open Jenkins. -
From the list of links in the left, click Manage Jenkins. On the management page, click Global Tool Configuration.
-
Scroll the list down to Maven. Click Add Maven to add a new Maven installation.
-
Enter
default
as the name. Deselect the Install Automatically checkbox and enter/usr/share/maven
for the MAVEN_HOME property. -
Click Save.
In this task you will create a Maven job in Jenkins to compile MyShuttleCalc and to deploy the package to the VSTS Package feed.
-
Open your VSTS Team Project in Chrome and click Code to open the Code Hub. Select MyShuttleCalc from the list of repos.
-
In the upper right, click Clone. Switch to the SSH tab and copy the SSH url into the clipboard.
-
Go back to Jenkins in your browser. In the list of items in the left navigation pane in Jenkins, click New Item.
-
Enter
MyShuttleCalc
for the item name and click on "Maven Project". Click OK. -
In the Source Code Management section, select Git. Paste in the SSH url to the MyShuttleCalc repo.
-
Click Add in the Credentials section and select Jenkins.
-
In the dialog, change the "Kind" to "SSH Username with private key".
-
Select the "Enter directly" option under "Private Key".
-
Open a terminal, type
cat ~/.ssh/id_rsa
(or open it in any editor) and paste the contents into the Key textbox. Enter a passphrase (if you used one - otherwise leave it empty). Entervmadmin
as the ID. Click Add. -
Your Source Code Management should now look like this:
-
In the Build section, ensure that "Root POM" is
pom.xml
. Set the "Goals and options" todeploy -Dbuildversion=1.0.${BUILD_NUMBER}
. Click Advanced to expand the advanced settings. Change the Settings file to Settings file in filesystem and set the File path tomaven/settings.xml
. These settings instruct Maven to compile, test and package the code and then publish the package to the repository defined in the settings.xml file that was previously modified to include the authentication token. -
Scroll down to Post-build Actions. Click Add post-build action and select Record JaCoCo coverage report. You can leave all the default settings unchanged. This publishes the JaCoCo results for this job.
-
Add a new post-build action - select Archive the artifacts. Set Files to archive to
**/MyShuttleCalc*.jar
. This saves the MyShuttleCalc jar file as an artifact from this job. -
Add a new post-build action - select Collect results for TFS/Team Services. This step allows Jenkins to collect test results and coverage results so that they are available to VSTS. Click Add twice to add the two collectors:
- Type: JUnit, Files to include:
**/TEST-*.xml
- Type: JaCoCo, Files to include:
**/jacoco/**
- Type: JUnit, Files to include:
-
Click Save.
In this task you will run the build to ensure that it runs successfully.
-
Click Build Now in the links on the left to start a build. Alternatively you can schedule the build from the dashboard by clicking Schedule build.
-
Click on the build number to open the build. Click on "Console Output" to view the logs.
-
When the build completes, you can click on Back to project to go to the build summary. You should see test and coverage results as well as a link to the jar artifact.
-
Navigate to the Maven Package feed in VSTS. You should see the latest package with the version number matching 1.0.jenkins-build-number.
In this task you will configure a Team Build in VSTS that will trigger the Jenkins job and collect the results. This technique offers the integration between source control, work item tracking,test and release so that you get the output in VSTS even though the build is done in Jenkins.
-
In Jenkins, click on the Jenkins logo to navigate to the dashboard.
-
Click Manage Jenkins and then click Configure Global Security.
-
In the Access Control section, select Jenkin's own user database and select Allow users to sign up. Ensure User Authorization is selected for Anyone can do anything.
-
On the upper-right of the black toolbar, click Sign up. Create a new user account and make a note of the credentials as you will need them later.
-
Navigate to your VSTS Team Project and click on the gear icon and then Services.
-
Click + New Service Endpoint and select Jenkins from the list.
-
Enter the following:
Name Value Notes Connection Name Azure VM Jenkins
The name of this connection Server URL http://10.0.0.4:8080
The internal IP address of the Azure VM on port 8080 (the Jenkins port) Username The username you created in Jenkins Password The password you created in Jenkins -
Click Verify connection to ensure that the connection is valid.
-
In VSTS, click on Build & Release -> Builds to navigate to the Builds hub.
-
Click + New to create a new definition.
-
Enter "jenkins" into the search box and press enter. Click on the Jenkins template and click Apply. Configure the following settings and tasks.
-
In Process settings
Parameter Value Notes Default agent queue default
Run this build on your VSTS agent container Job Name MyShuttleCalc
The name of the Jenkins job Jenkins service endpoint Azure VM Jenkins
The endpoint you just created for Jenkins -
In Get Sources settings
Connect to the MyShuttleCalc repo on the master branch.
-
Configure the Queue Jenkins Job: MyShuttleCalc task as follows:
-
Configure the Download artifacts produced by MyShuttleCalc task as follows:
-
Insert a Publish Test Results task and configure it as follows:
Parameter Value Notes Test result format JUnit
Publish JUnit results Test results files **/TEST-*.xml
File containing test results Search Folder $(Build.ArtifactStagingDirectory)
Root folder for search operation -
Insert a Publish Code Coverage Results task and configure it as follows:
Parameter Value Notes Code Coverage Tool JaCoCo
Publish JaCoCo results Summary File $(Build.ArtifactStagingDirectory)/jenkinsResults/MyShuttleCalc/team-results/jacoco/target/site/jacoco/jacoco.xml
File containing JaCoCo results Report Directory $(Build.ArtifactStagingDirectory)/jenkinsResults/MyShuttleCalc/team-results/jacoco/target/site/jacoco
Folder containing detailed coverage reports -
The final list of tasks should look as follows:
-
Save and queue the build.
-
When the build completes, click on the build number to view the results.