CI/CD with Jenkins — Part 2: Use Jenkins for Continuous Integration

Installing Maven

If you have followed the first part of the tutorial then you have already installed Git and JDK during the installation of Jenkins. Log back again to your SSH instance and switch to the sudo user. You can verify if Git is installed on your instance by running git — version.

aliyun@jenkins:~$ git --version
git version 2.7.4
aliyun@jenkins:~$ javac -version
javac 1.8.0_171
tar -xf apache-maven-3.5.3-bin.tar.gz
sudo mv apache-maven-3.5.3 /opt/maven
echo 'PATH="$PATH:/opt/maven/bin"' | sudo tee -a ~/.profile
source ~/.profile
aliyun@jenkins:~$ mvn --version
Apache Maven 3.5.3 (3383c37e1f9e9b3bc3df5050c29c8aff9f295297; 2018-02-24T19:49:05Z)
Maven home: /opt/maven
Java version: 1.8.0_171, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-8-oracle/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.4.0-122-generic", arch: "amd64", family: "unix"

Configure Jenkins

Once you are successfully logged in with the administrator account, navigate to Manage Jenkins >> Global Tool Configuration. In the Global Tool Configuration interface you can configure various tools that will be used by Jenkins during the build process. Under JDKoption, provide a name for your JDK, such as JDK 1.8 etc. Uncheck the select box Install automatically since we have already installed JDK. Provide the path to your Java installation so that Jenkins can automatically identify the Java binaries. If you do not know the value of “JAVA_HOME”, you can always run the command echo $JAVA_HOME to get the Java installation path.

Create Sample Maven Project

For setting up Jenkins to run a Maven build, we will need a Maven project first. Maven itself is equipped with generating sample projects for the developers to start building the applications. We will use Maven to generate a sample web application in Java. Since we are learning how to use Jenkins, we will not go into the details of Maven project. Generate the Maven project with architecture type “maven-archetype-webapp”.

cd ~
mvn archetype:generate -DgroupId=my.maven.app -DartifactId=MyMavenApp -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
[INFO] Generating project in Batch mode
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-webapp:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: basedir, Value: /home/aliyun
[INFO] Parameter: package, Value: my.maven.app
[INFO] Parameter: groupId, Value: my.maven.app
[INFO] Parameter: artifactId, Value: MyMavenApp
[INFO] Parameter: packageName, Value: my.maven.app
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: /home/aliyun/MyMavenApp
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.599 s
[INFO] Finished at: 2018-05-05T10:39:28Z
[INFO] ------------------------------------------------------------------------
MyMavenApp
├── pom.xml
└── src
└── main
├── resources
└── webapp
├── index.jsp
└── WEB-INF
└── web.xml

Push Maven Project to Github

Open your Github account and create a new repository with any name you prefer. Make sure not to initialize the repository with any file as it can be done later upon requirement.

git config --global user.name "Alibaba Cloud User"
git config --global user.email you@example.com
cd MyMavenApp
git init
aliyun@jenkins:~/MyMavenApp$ git init
Initialized empty Git repository in /home/aliyun/MyMavenApp/.git/
echo -e "# MyMavenApp\nSample Project to demostrate Jenkins" >> README.md
git add .
git commit -m "First commit"
aliyun@jenkins:~/MyMavenApp$ git remote -v
origin https://github.com/liptanbiswas/MyMavenApp.git (fetch)
origin https://github.com/liptanbiswas/MyMavenApp.git (push)
aliyun@jenkins:~/MyMavenApp$ git push origin master
Username for 'https://github.com': liptanbiswas
Password for 'https://liptanbiswas@github.com':
Counting objects: 10, done.
Compressing objects: 100% (7/7), done.
Writing objects: 100% (10/10), 1.07 KiB | 0 bytes/s, done.
Total 10 (delta 0), reused 0 (delta 0)
To https://github.com/liptanbiswas/MyMavenApp.git
* [new branch] master -> master

Create Jenkins Project

Log in to your Jenkins application and click on “create new jobs” to get started.

Running the Build Manually

Since we have not configured any build trigger, thus we will need to start the build job manually. We can run the build by clicking on the “Build Now” button.

  1. A blue ball will be displayed for a successful build and a red ball for a failed build.
  2. “Console output” page displays the full console output logs of the build job.
  3. Tells us when the build was initiated and how much time it took to finish.
  4. Shows a summary of the changes. Since our build was the first build, there are no changes to display.

Conclusion

In this second part of the three series tutorial, we have learned to install Maven and configure it with to work with Jenkins. We have also created a sample project and configured our build project in Jenkins. Finally, we have successfully executed our first build manually.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

4.97K Followers

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com