CI/CD with Dockers and Jenkins

Kubernetes Advocate
AVM Consulting Blog
10 min readJun 14, 2020

CI — Continuous Integration: involves beginning an automatic build (and presumably running tests) whenever new code is committed to or checked into the team project’s supply management repository.

This gives you immediate feedback that the code builds and may doubtless be deployed.

CI/CD

CD — Continuous Deployment/Delivery:: involves beginning an automatic preparation method whenever a brand new palmy build is out there

Cloud Environments

The deployment environments for a cloud application can be summarized as follows:

  • Staging
  • Production

Deployment Flow Process :

Git Hub +Jenkins +Tomcat server

GIT installation step by step

Step 0: Install git and create a GitHub account

The first two things you’ll want to do are install git and create a free GitHub account.

Follow the directions here to put in lowlife (if it’s not already installed).

Note that for this tutorial we’ll be victimization lowlife on the instruction solely.

While there are some nice lowlife GUIs (graphical user interfaces), I believe it’s easier to find out lowlife victimization lowlife-specific commands 1st and so to undertake out a git GUI

Once you’ve done that, produce a GitHub account here. (Accounts are free for public repositories, but there’s a charge for private repositories.)

Step 1: Create a local git repository

When creating a new project on your local machine using git, you’ll first create a new repository (or often, ‘repo’, for short).

If you don’t have abundant expertise with the terminal and basic commands, scrutinize this tutorial (especially the ‘Navigating the Filesystem’ and ‘Moving Around’ sections).

To begin, open up a terminal and move to wherever you wish to put the project on your native machine victimization the cd (change directory) command. For example, if you have a ‘projects’ folder on your desktop, you’d do something like:

vineet@vineet-ndi-lap-688:~$ cd Desktop/Projects/maven-project/
vineet@vineet-ndi-lap-688:~/Desktop/Projects/maven-project$ ls
Jenkinsfile pom.xml server webapp

To initialize a git repository in the root of the folder, run the git init command:

vineet@vineet-ndi-lap-688:~/Desktop/Projects/maven-project$ git init
Reinitialized existing Git repository in /home/vineet/Desktop/Projects/maven-project/.git/

Step 2: Add a new file to the repo

Go ahead and add a new file to the project, using any text editor you like or running a touch command.

Once you’ve added additional or changed files during a folder containing a git repo, git can notice that changes are created within the repo.

But, git won’t formally keep track of the file (that is, place it during a commit — we’ll speak a lot of regarding commits next) unless you expressly tell it to.

vineet-ndi-lap-688:~/Desktop/Projects/maven-project$ touch website.txt
vineet-ndi-lap-688:~/Desktop/Projects/maven-project$ ls
Jenkinsfile pom.xml server webapp website website.txt

After creating the new file, you can use the git status command to see which files git knows to exist.

vineet@vineet-ndi-lap-688:~/Desktop/Projects/maven-project$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
Jenkinsfile
website
website.txt
nothing added to commit but untracked files present (use "git add" to track)

What this primarily says is, “Hey, we tend to notice you created a brand new file known as mnelson.txt, however, unless you employ the ‘git add’ command we tend to are not reaching to do something

An interlude: The staging environment, the commit, and you

One in all the foremost confusing components, when you’re initially learning GIT, is the conception of the staging surroundings and the way it relates to a commit.

A commit could be a record of what files you’ve got modified since the last time you created a commit.

Essentially, you create changes to your repo (for example, adding a file or modifying one) so tell GIT to place those files into a commit.

Commits structure the essence of your project and permit you to travel back to the state of a project for any purpose.

So, however, does one tell GIT the files to place into a commit?

This is wherever the staging surroundings or index are available.

As seen in Step two, after you build changes to your repo, GIT notices that a file has modified however won’t do something with it (like adding it in a very commit).

To add a file to a commit, you initially ought to add it to the staging surroundings.

To do this, you’ll be able to use the GIT add the command (see Step three below).

Once you’ve used the git add a command to feature all the files you wish to the staging surroundings, you’ll be able to then tell GIT to package them into a commit

Note: The staging surroundings, additionally known as ‘staging’, is that the new most popular term for this, however, you’ll be able to additionally see it spoken because of the ‘index’.

Step 3: Add a file to the staging environment

Add a file to the staging surroundings victimization the GIT add command.

If you rerun the GIT status command, you’ll see that GIT has another file to the staging surroundings (notice the “Changes to be committed” line).

devops-ndi-lap-688:~/Desktop/Projects/maven-project$ git add .
devops-ndi-lap-688:~/Desktop/Projects/maven-project$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: Jenkinsfile
new file: website
new file: website.txt

To repeat, the file has not nonetheless been another to a commit, however, it is close to being.

Step 4: Create a commit

It’s time to create your first commit!

Run the command git commit -m "Jenkins File created"

devops-ndi-lap-688:~/Desktop/Projects/maven-project$ git commit -m "Jenkins file created "
[master 9e50b6e] Jenkins file created
3 files changed, 32 insertions(+)
create mode 100644 Jenkinsfile
create mode 100644 website
create mode 100644 website.txt

Run the command skunk commit -m “Jenkins File created” The message at the top of the commit ought to be one thing associated with what the commit contains — perhaps it’s

a new feature, perhaps it’s a bug fix, perhaps it’s simply fixing a mistake. Don’t place a message like “asdfadsf” or “foobar”.

Step 5: Create a new branch

Now that you’ve created a brand new commit, let’s strive for one thing a touch additional advanced.

Say you wish to create a brand new feature however square measure disquieted concerning creating changes to the most project whereas developing the feature.

Branches allow you to move back and forth between the ‘states’ of a project.

For instance, if you wish to feature a brand new page to your web site you’ll be able to produce a brand new branch only for that page while not touching the most a part of the project.

Once you’re finished the page, you’ll be able to merge your changes from your branch into the master branch. After running the above command, you can use the git branch the command to confirm that your branch was created:

devops-ndi-lap-688:~/Desktop/Projects/maven-project$ git Branch
master
* Development

The branch name with the asterisk next to it indicates which branch you’re pointed to at that given time.

Now, if you switch back to the master branch and make some more commits, This command can mechanically produce a brand new branch so mechanically you out’ on that, which means GIT can move you merge a branch, off of the master branch.

Step 6: Create a new repository on GitHub

If you merely wish to stay track of your code regionally, you oughtn’t to use GitHub.

But if you wish to figure with a team, you’ll be able to use GitHub to collaboratively modify the project’s code.

To create a brand new repo on GitHub, log in and move to the GitHub home page.

You should see an inexperienced ‘ New repository’ button: when clicking the button, GitHub can raise you to call your repo and supply a quick description:

Step 7: New repository is created (maven-project)

Step 8: Let’s push the Git code to the repository (Maven -Project)

Step 9: We can see that code has been successfully pushed to Github Repository

Tomcat 8 Installation step by step:

1. java -version
2. sudo apt install OpenJDK-8-JDK-headless
3. sudo apt-get update
4. sudo add-apt-repository ppa:webupd8team/java
5. java -version
6. sudo apt-get update
7. sudo apt-get install tomcat8
8. sudo apt-get install tomcat8-docs tomcat8-admin tomcat8-examples
9. systemctl start tomcat 8
10. systemctl start tomcat 8

You need to make changes in Tomcat-users.xml file after tomcat is successfully installed

sudo vi /etc/tomcat8/tomcat-users.xml

Jenkins Installation

Follow the below steps:

Step 1: First install Java, using the following command.

$ sudo apt-get install openjdk-8-jre-headless -y

Step 2: Add the key and source list to apt for Jenkins.

$ wget -q -O - https://pkg.jenkins.io/debian/jenkins-ci.org.key | sudo apt-key add -

Step 3: Now create a source list for Jenkins, using the below commands

$ sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'

Step 4: Update the packages

$ sudo apt-get update

Step 5: Now install Jenkins.

$ sudo apt-get install jenkins

Step 6: After installing Jenkins. Go to: http://your_ip_address:8080 and run the following commands to get the admin password.

$ sudo nano /var/lib/jenkins/secrets/initialAdminPassword

Step 7: Now copy the password and paste it into the browser.

When finished, you should see a screen similar to the below one.

Integrating GitHub with Jenkins

The integration of GitHub with Jenkins automates deployment, testing, and improves product quality while saving a significant amount of time for developers. Follow the below steps to integrate GitHub with Jenkins:

Prerequisite: Install GitHub Jenkins plugin.

Step 1 Go to Manage Jenkins -> Manager Plugin.

Step 2 Search Github Plugin in the Available tab then click on Download now and install after the restart.

Step 3 Install Maven Plugin. Go to Jenkins- Manage Jenkins- Global Tool Configuration, Click OK

How to integrate Jenkins with GitHub so automatically CI/ CD takes place when any commit made by developers

Step 1 Go to Jenkins- click on System configuration- Go to GitHub section

Step2 Go to Github Repository- click on settings- Go to webhook and add new webhook

Paste the Jenkins Hook URL in the Github webhook

Creating a Jenkins job

Step 1 To Create a new task for Jenkins, click on “New Item” then enter an item name that is suitable for your project and select the Freestyle project. Now click Ok.

Step 2 Select the GitHub project checkbox and set the Project URL to point to your GitHub Repository.

Step 3: Under the Source Code Management tab, select Git, and then set the Repository URL to point to your GitHub Repository.

Step 4: Now Under the Build Triggers tab, select the “Build when a change is pushed to GitHub” checkbox.

Step 5: In the end, execute the Shell script to take a clone from dev. When the configuration is done, click on the Save button.

Step 6: Click OK and Build a Job and you will see that a war file is created and as soon as build is successfully triggered, the same war file is copied by other projects (Other projects to build)

Step 7: Create one more project for Deployment into a tomcat server hosted on an EC2 machine (Deploy- to -Staging)

You will see that it is copying artifacts from a previous project (Package) and also mentioning to copy the latest successful build of the previous project

Step 8: Now we will deploy the war file to the tomcat 8 server hosted on EC2 machine with IP 172.31.90.150:8080(Port number of Tomcat 8)

Step 9: As soon as we create a build of this Job we will see that the Java application is deployed on Tomcat 8 hosted on an EC2 machine

Step 10: we can see our app is deployed on path/var/lib/tomcat8/webapps

ubuntu@ip-172–31–90–150:~$ cd /var/lib/tomcat8/webapps/production/
ubuntu@ip-172–31–90–150:/var/lib/tomcat8/webapps/production$ ls
index.JSP META-INF WEB-INF

Step 11: We can now copy URL and paste on a browser with /Production

👋 Join us today !!

️Follow us on LinkedIn, Twitter, Facebook, and Instagram

If this post was helpful, please click the clap 👏 button below a few times to show your support! ⬇

--

--

Kubernetes Advocate
AVM Consulting Blog

Vineet Sharma-Founder and CEO of Kubernetes Advocate Tech author, cloud-native architect, and startup advisor.https://in.linkedin.com/in/vineet-sharma-0164