Jenkins (CI/CD) Upstream/Downstream Project

Vineet Sharma
Nov 4 · 10 min read

A quick introduction to CI/CD and cloud deployment

CI — Continuous Integration: involves starting an automated build (and possibly running tests) whenever new code is committed to or checked into the team project’s source control repository. This gives you immediate feedback that the code builds and can potentially be deployed.

CI/CD

CD — Continuous Deployment/Delivery: involves starting an automated deployment process whenever a new successful build is available.

Together, CI and CD mean that any code changes you commit to your repository are quickly validated and deployed to a test server, a live web site, or wherever you need it.


Cloud Environments

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

  • Staging
  • Production

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 instructions here to install git (if it’s not already installed). Note that for this tutorial we will be using git on the command line only. While there are some great git GUIs (graphical user interfaces), I think it’s easier to learn git using git-specific commands first and then to try out a git GUI once you’re more comfortable with the command.

Once you’ve done that, create 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).

To use git we’ll be using the terminal. If you don’t have much experience with the terminal and basic commands, check out this tutorial (especially the ‘Navigating the Filesystem’ and ‘Moving Around’ sections).

To begin, open up a terminal and move to where you want to place the project on your local machine using the cd (change directory) command. For example, if you have a 'projects' folder on your desktop, you'd do something like:

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

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 or modified files in a folder containing a git repo, git will notice that changes have been made inside the repo. But, git won’t officially keep track of the file (that is, put it in a commit — we’ll talk more about commits next) unless you explicitly tell it to.

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

What this basically says is, “Hey, we noticed you created a new file called mnelson.txt, but unless you use the ‘git add' command we aren't going to do anything with it."

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

One of the most confusing parts when you’re first learning git is the concept of the staging environment and how it relates to a commit.

A commit is a record of what files you have changed since the last time you made a commit. Essentially, you make changes to your repo (for example, adding a file or modifying one) and then tell git to put those files into a commit.

Commits make up the essence of your project and allow you to go back to the state of a project at any point.

So, how do you tell git which files to put into a commit? This is where the staging environment or index come in. As seen in Step 2, when you make changes to your repo, git notices that a file has changed but won’t do anything with it (like adding it in a commit).

To add a file to a commit, you first need to add it to the staging environment. To do this, you can use the git add <filename> command (see Step 3 below).

Once you’ve used the git add command to add all the files you want to the staging environment, you can then tell git to package them into a commit using the git commitcommand.

Note: The staging environment, also called ‘staging’, is the new preferred term for this, but you can also see it referred to as the ‘index’.

Step 3: Add a file to the staging environment

Add a file to the staging environment using the git add command.

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

To reiterate, the file has not yet been added to a commit, but it’s about to be.

Step 4: Create a commit

It’s time to create your first commit!

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

The message at the end of the commit should be something related to what the commit contains — maybe it’s a new feature, maybe it’s a bug fix, maybe it’s just fixing a typo. Don’t put a message like “asdfadsf” or “foobar”. That makes the other people who see your commit sad. Very, very, sad.

Step 5: Create a new branch

Now that you’ve made a new commit, let’s try something a little more advanced.

Say you want to make a new feature but are worried about making changes to the main project while developing the feature. This is where git branches come in.

Branches allow you to move back and forth between ‘states’ of a project. For instance, if you want to add a new page to your website you can create a new branch just for that page without affecting the main part of the project. Once you’re done with the page, you can merge your changes from your branch into the master branch. When you create a new branch, Git keeps track of which commit your branch ‘branched’ off of, so it knows the history behind all the files.

Let’s say you are on the master branch and want to create a new branch to develop your web page. Here’s what you’ll do: Run git checkout -b <my branch name>. This command will automatically create a new branch and then 'check you out' on it, meaning git will move you to that branch, off of the master branch.

After running the above command, you can use the git branch command to confirm that your branch was created:

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, your new branch won’t see any of those changes until you merge those changes onto your new branch.

Step 6: Create a new repository on GitHub

If you only want to keep track of your code locally, you don’t need to use GitHub. But if you want to work with a team, you can use GitHub to collaboratively modify the project’s code.

To create a new repo on GitHub, log in and go to the GitHub home page. You should see a green ‘+ New repository’ button:

After clicking the button, GitHub will ask you to name your repo and provide a brief description:

Step 6 New repository is created (maven-project)

Step 7 Lets push the Git code to the repository (Maven -Project)

Step 8 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.

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

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

Step 4: Update the packages

Step 5: Now install Jenkins.

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

Step 7: Now copy the password and paste into 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 products quality while saving a significant amount of time of developers. Follow the below steps to integrate GitHub with Jenkins:

Prerequisite: Install GitHub Jenkins plugin.

Step 1 Go to Manage Jenkins -> Manage 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 addnew 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 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 Source Code Management tab, select Git and then set the Repository URL to point to your GitHub Repository.

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

Step 5 At the end, execute Shell script to take a clone from dev. When the configuration is done, click on 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 , same war file is copied by other project (Other project to build)

Step7 Create one more project for Deployment into tomcat server hosted on EC2 machine (Deploy- to -Staging)

you will see that its copying artifacts from previous project (Package) and also mentioning to copy the latest successful build of previous project

Step 8 Now we will deploy the war file to 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 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 browser with /Production

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade