Jenkins on EC2 : 3. Creating a Maven Project
Our source code for Jenkins Maven project is in
https://github.com/bogo-devops/https://github.com/bogo-devops/JenkinsOnEC2MavenProject.
We want the project to be built automatically by Jenkins.
Click on "New Item" and give it a name. Since it is a Maven project we tell Jenkins build it with Maven:
Click "OK"
Under "Configuration", first we select "git" for "Source Code Management", then for "Repository URL", we give the same URL we use to clone:
"https://github.com/bogo-devops/JenkinsOnEC2MavenProject.git".
Now, we have to setup some credentials so that Jenkins instance can access this repository. We need to create public/private key pair for Jenkins user to allow check out code from our repository.
So, let's switch back to EC2 instance.
ubuntu@ip-172-31-52-90:~$ sudo su - jenkins jenkins@ip-172-31-52-90:~$ pwd /var/lib/jenkins jenkins@ip-172-31-52-90:~$ ssh-keygen -t dsa Generating public/private dsa key pair. Enter file in which to save the key (/var/lib/jenkins/.ssh/id_dsa): Created directory '/var/lib/jenkins/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /var/lib/jenkins/.ssh/id_dsa. Your public key has been saved in /var/lib/jenkins/.ssh/id_dsa.pub. The key fingerprint is: 3a:47:1c:3c:f4:b3:f3:da:8e:0a:4a:2b:37:27:01:e9 jenkins@ip-172-31-52-90 The key's randomart image is: +--[ DSA 1024]----+ | . | | o . | | . + o | | o . o o | | . . S o | | E . o o | | ..+ . . | | ..+o.+ + | | oo+ ..o.o | +-----------------+
Now we want to copy the key for GitHub deploy key:
jenkins@ip-172-31-52-90:~$ cd .ssh jenkins@ip-172-31-52-90:~/.ssh$ pwd /var/lib/jenkins/.ssh jenkins@ip-172-31-52-90:~/.ssh$ ls id_dsa id_dsa.pub jenkins@ip-172-31-52-90:~/.ssh$ cat id_dsa.pub ssh-dss AAAAB3NzaC1kc3MAAACBAI9yvl+Amck7ql6ceBa+aFc3pL2vnDkNR/k5m1b5ZpRr40B/SJc3IIl90jJOKqvctrON981T4SowEEQKjd0V8f34iiyIBRDHdayhQboTtxVpJVd8laS+Px3H1I+a8+v17hQDcCQ4mx8xg7ZAczKeT/bVsJs2dsnV7U/+izLD20T/AAAAFQDjuCnGMX/BF9Qk9WIswbNIpVzyHwAAAIByQ+fHGAd/Cy9FOGGne5ZiLhdAD6F3xQcLr8RqbBO3IpZUBbTpEvTj1IF5m0H98+QElbFj21Trf0fz72JsmO3vQj9rjWq40TNfEATBYz5shd84RG6cch+y2AmkcorFNq67OIs2U0SkHp0VsMOJj/bg58PM6af0HrhNgq5zXV6fywAAAIA5pKqlakmZTLaEqY7PfoyPmf3QGAXKD1B4UJK/QACdw2Zt5BmiSsVGdK6CFe4We3U6O+CAIfN2IDZPgfI/CJYC/BJHnjHKKQgFRMavwBHPcZvDMR2mhzprW5xLCOVyxjJxe0oBrivl9JX70Q+j1hkjzgGY5Lo6920Hk/aFggS/Fw== jenkins@ip-172-31-52-90
Go back to repository, under "Settings", click on "Deploy keys"=>"Add deploy key". Give it a name and paste the key:
This allows Jenkins server to pull code from the repository but it does not give it write access to the repository. It only gives it read-only access which is probably what we want.
Switch back to Jenkins server, "Configure".
For the "Goals", we're going to have it run clean first, we'll have a run the clean target to make sure that it is building from scratch and then we'll have it run the install target so that it will build everything run all the tests and install the jar file or the artifact that is generated from the project into the local Maven repository.
Everything looks good, and click on "Save" to create the project.
Click on "Build"
When we look at "Console Output", we can see it checked out the code from GitHub repository:
Started by user K Hong Building in workspace /var/lib/jenkins/workspace/JenkinsOnEC2Maven Cloning the remote Git repository Cloning repository https://github.com/bogo-devops/JenkinsOnEC2MavenProject.git > git init /var/lib/jenkins/workspace/JenkinsOnEC2Maven # timeout=10 Fetching upstream changes from https://github.com/bogo-devops/JenkinsOnEC2MavenProject.git > git --version # timeout=10 > git fetch --tags --progress https://github.com/bogo-devops/JenkinsOnEC2MavenProject.git +refs/heads/*:refs/remotes/origin/* > git config remote.origin.url https://github.com/bogo-devops/JenkinsOnEC2MavenProject.git # timeout=10 > git config remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10 > git config remote.origin.url https://github.com/bogo-devops/JenkinsOnEC2MavenProject.git # timeout=10 Fetching upstream changes from https://github.com/bogo-devops/JenkinsOnEC2MavenProject.git > git fetch --tags --progress https://github.com/bogo-devops/JenkinsOnEC2MavenProject.git ...
Then, it did maven clean install on the "pom.xml". It's going to take longer on the first invocation of Maven clean install because it has to download all the plugins for Maven and install them into its local repository. Subsequent build, however, will be much faster.
Parsing POMs Discovered a new module com.bogotobogo:bogotobogo_JenkinsOnEC2 bogotobogo_JenkinsOnEC2 Modules changed, recalculating dependency graph [JenkinsOnEC2Maven] $ /usr/lib/jvm/java-7-oracle/bin/java -cp /var/lib/jenkins/plugins/maven-plugin/WEB-INF/lib/maven3-agent-1.5.jar:/usr/share/maven/boot/plexus-classworlds-2.x.jar org.jvnet.hudson.maven3.agent.Maven3Main /usr/share/maven /var/cache/jenkins/war/WEB-INF/lib/remoting-2.47.jar /var/lib/jenkins/plugins/maven-plugin/WEB-INF/lib/maven3-interceptor-1.5.jar /var/lib/jenkins/plugins/maven-plugin/WEB-INF/lib/maven3-interceptor-commons-1.5.jar 42364 <===[JENKINS REMOTING CAPACITY]===>channel started Executing Maven: -B -f /var/lib/jenkins/workspace/JenkinsOnEC2Maven/pom.xml clean install
We can also see that it ran all of the tests after it compiled the code though we don't have real test right now:
------------------------------------------------------- T E S T S ------------------------------------------------------- Results : Tests run: 0, Failures: 0, Errors: 0, Skipped: 0 [JENKINS] Recording test results
"Back to Project"=>"Bakc to Dashboard", the blue ball indicate a success:
We have a project setup on Jenkins. In the next Chapter we're going to get the GitHub to notify our Jenkins server when changes have been pushed so that it will automatically build our code for us without us having to manually issue a build and we'll also setup line coverage reporting for our project so that we can know the line coverage of our unit tests.
Jenkins
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization