×
Community Blog CI/CD with Jenkins - Part 2: Use Jenkins for Continuous Integration

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

In this tutorial, we will install the latest version of Jenkins automation server on an Alibaba Cloud ECS Ubuntu 16.

By Liptan Biswas, Alibaba Cloud Tech Share Author. Tech Share is Alibaba Cloud’s incentive program to encourage the sharing of technical knowledge and best practices within the cloud community.

In the previous tutorial of this three-part series, we have learned how to install Jenkins on Ubuntu 16.04. In this detailed tutorial, we will learn to set up a Maven based Jenkins build job. We will start by installing Apache Maven on Alibaba ECS instance followed by configuring Jenkins to use Git, Maven, and JDK. We will then use the Maven to generate a sample Java web application as a Maven project.

Jenkins can automatically pull source code from Github or any Git repository. In this tutorial, we will configure Jenkins to pull the source from Github. For that to happen, we will also need to set up a repository in Github and push the sample Maven project to the remote repository. Once everything is configured, we will run our first Jenkins build manually. As in DevOps environment, automation of builds are important, we will learn more about different types of build triggers and source code polling in the final tutorial of the series.

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.

You should see.

aliyun@jenkins:~$ git --version
git version 2.7.4

Similarly, verify if JDK is installed by verifying its version. Run javac -version.

aliyun@jenkins:~$ javac -version
javac 1.8.0_171

Apache Maven is available in default Ubuntu repository, but the available version might be outdated. Obtain the latest copy of Maven binaries from Maven download page.

wget http://www-us.apache.org/dist/maven/maven-3/3.5.3/binaries/apache-maven-3.5.3-bin.tar.gz

Extract the archive and move the whole Maven directory into /opt directory.

tar -xf apache-maven-3.5.3-bin.tar.gz
sudo mv apache-maven-3.5.3 /opt/maven

Now, add Maven in the PATH variable so that the Maven executables can be invoked easily.

echo 'PATH="$PATH:/opt/maven/bin"' | sudo tee -a ~/.profile
source ~/.profile

You can check if Maven has been installed successfully by running mvn --version. You should see.

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"

Now that we have JDK, Git, and Maven installed, login to your Jenkins application and configure Jenkins to work with these applications.

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 JDK option, 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.

1

Leave the default values for Git since git is easily accessible by running git command. Scroll down to find the Maven installation configuration. Provide a name for your Maven installation so that you can easily identify it later and uncheck Install automatically option. Since we have installed Maven into /opt/maven, enter the same path /opt/maven into "MAVEN_HOME" textbox.

2

Finally, click on the Save button to save the changes.

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

The above command will generate a project with package name "my.maven.app", and project name "MyMavenApp". You will see a similar output.

[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] ------------------------------------------------------------------------

A new project directory "MyMavenApp" will be created with the following files.

MyMavenApp
├── pom.xml
└── src
    └── main
        ├── resources
        └── webapp
            ├── index.jsp
            └── WEB-INF
                └── web.xml

Our Maven project is now ready, now we can go ahead and create a new repository for the project on Github.

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.

3

After pushing the button "Create Repository", an empty repository will be created and you will be presented with a link to the repository.

4

Copy the link to the repository as it will be required later in the tutorial. Login to your SSH instance once again as the sudo user. Configure your local git by adding the user information.

git config --global user.name "Alibaba Cloud User"
git config --global user.email you@example.com

Switch to the newly created Maven project directory, and initialize the project as a git repository.

cd MyMavenApp
git init

You should see.

aliyun@jenkins:~/MyMavenApp$ git init
Initialized empty Git repository in /home/aliyun/MyMavenApp/.git/

Add the files to the repository and create the first commit.

echo -e "# MyMavenApp\nSample Project to demostrate Jenkins" >> README.md
git add .
git commit -m "First commit"

Add the URL of the remote Github repository. Replace the URL in the command below with your actual URL.

git remote add origin https://github.com/liptanbiswas/MyMavenApp.git

Verify the remote origin by running git remote -v.

aliyun@jenkins:~/MyMavenApp$ git remote -v
origin    https://github.com/liptanbiswas/MyMavenApp.git (fetch)
origin    https://github.com/liptanbiswas/MyMavenApp.git (push)

Since we already have committed all the files and changes, we can directly push to the remote repository by running.

git push origin master

You will be required to enter your Github credentials for pushing into the remote origin. You will see.

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

Verify if changes are reflected in Github repository. You should see that code is now uploaded into Github.

5

At this point, our sample Java web application is uploaded to Github and we can proceed to create a new pipeline in Jenkins.

Create Jenkins Project

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

6

Provide a name for your project and choose "Freestyle project" as the project type. The freestyle type project is the most flexible type project.

7

Clicking "OK" will create the project for you and you will be taken to project’s configuration page. Provide a project name and a description of the project.

8

Under "Source Code Management", select Git and provide the link to the Github repository into "Repository URL". Leave the default option for everything else since we do not have any other branch than "master" and we want to use the default behavior in accessing the repository.

9

Now, scroll down to the find the "Build" option. Select "Invoke top-level Maven target" from "Add build step" drop-down menu.

10

Select the Maven version from the drop-down which we have configured earlier and put clean packagein "Goals".

11

There are certain steps or phases in the Maven build lifecycle, which are: validate, compile, test, package, verify, install, and deploy. Build lifecycle is sequential starting from validate and ending at deploy. clean command will clean the workspace before the build and package command will initiate the build process starting from phase validate to package. Once the build is successful, Maven should have packaged the web application in "war" file.

After saving the configuration, you will be taken to build job interface for the job we just created. We are now ready to run the build job.

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.

12

This should trigger the manual build and you should see that the build has started under "Build History". A blinking ball signifies the build is currently running.

13

Since our app is a sample app, it should not take more than a few seconds to complete. Click on the build number to get more information about the build.

14

In the above screenshot of the build status, you can identify the following information.

  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.

Since the build job has completed successfully, it must have packed out our application war file. We can verify if the file is generated by browsing the files in the workspace for this job. On the project’s main page, click on the "Workspace" link to browse the project’s files.

15

Navigate to the "target" directory and you should see the "MyMavenApp.war". A war file is a cross-platform Java web application archive used to distribute a collection of files. This war package can be deployed on any Java web server such as Apache Tomcat.

16

Now that we have learned how to trigger a build manually, we will now explore other automatic methods of triggering the builds. One of the popular methods of an automatic build is to periodically poll SCM. If Jenkins detects any change in the code, it automatically triggers a new build from the updated source code.

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.

In the third and last part of the tutorial, we will configure Jenkins to automatically build our application using different types of build triggers. We will also configure Jenkins to automatically deploy our web application on Tomcat 8 web server.

0 0 1
Share on

Alibaba Clouder

1,148 posts | 190 followers

You may also like

Comments