Merging the new developer's code in the master branch and full automatic deployment in production using DevOps.

Merging the new developer's code in the master branch and full automatic deployment in production using DevOps.

Hi all, in this world of automation we need agile, fast and rapid development and deployment. In this I.T industry, everyone is focusing on more and more automation and less manual work. Since it's easy and more efficient and of course fast. So in this article, I came up with the solution for one of the most faced issues in the software industry.


Problem:

There is one developer working on a project and setting up all the versions of its code using a version control system called "Git". But as per industry, a single developer never works on any project. Hence, another developer working on same project and using different branch and the final code needs to be deployed on the server as soon as possible so that clients can access it. So using Git, GitHub, Jenkins, Docker we are going to fulfil this requirement.

  • A usual "master" branch is available on the repository where the code is in working condition and tested too. If some code is added in this code from any developer, working on same project, we need to test it before merging.
  • Another developer working on this project with a branch named "dev1".
This is the scenario I've implemented.

Solution :

To implement this scenario, I have created a solution by integrating GitHub, Jenkins and Docker. This diagram is the architecture for the solution. This can be implemented by creating 3 jobs in Jenkins. Each job is described below.

Start - This flow starts when a developer in dev1 branch add some code and commit. Commit will automatically push the code in GitHub dev1 branch and will fire Jenkins Job 1.

  • Job 1 - This job will go to dev1 branch in GitHub and Fetch the code in Jenkins workspace. Then it will create a new Testing Environment using Container and deploy the code in the respective folder of the container as per its configurations. When this job builds successfully then Job 2 will fire. This is called "Job Chaining" and Job 1 is an "Upstream Job" for other jobs and other respective jobs are "Downstream Jobs" for job1.
  • Job 2 - This job will perform Automation Testing on the code. If code passed, then it will fire Job 3.
  • Job 3 - This job will merge the "dev1" branch with "master" branch for the deployment of code in production and when they merge, automatically the new code will be available in production.

Implementation

Prerequisites:

a) Jenkins software is required where you can set your jobs.

No alt text provided for this image
No alt text provided for this image


b) Docker is required for the container runtime.

c) Github repository is required from where the code will be fetched.

No alt text provided for this image



Implementing Job 1:

1.1 Created a new branch named "dev1". Checkout to the new branch, edit the code and save.

No alt text provided for this image

1.2 Create a new Job named "feature_branch_job" and configured it.

No alt text provided for this image

1.3 Headup to the SCM system and give the URL of your repository to Jenkins's job.

No alt text provided for this image

1.4 In the next step, I've used a chron tab or Poll SCM to monitor on Github on dev1 branch. You can use any other functionality like "Trigger builds remotely" with webhooks.

No alt text provided for this image

1.5 Copy the files in respective directory and start the development container.

No alt text provided for this image

1.6 After writing the script in the "Execute shell" section and click on "save".

1.7 As the job will run by the Jenkins user, you need to set up the permissions for that. Either you can use "setfacl" in Linux or you can make an entry in /etc/sudoers file or make another separate file in /etc/sudoers.d/ directory.

jenkins ALL=(ALL)  NOPASSWD: ALL


Implementing Job 2:

2.1 Job 2 will run if the job 1 will run successfully as it is an upstream job and settings have been settled as per this criteria only.

No alt text provided for this image

2.2 Job chaining the configured and same Poll SCM Strategy is followed in this job too or as mentioned above you can set any policy as per your requirement or concerns.

No alt text provided for this image


2.3 Copying the files in the respective directory and checking if the connection is working or not by using "curl" command and starting the container for master.

No alt text provided for this image

2.4 Click on "apply" and "save".


Implementing Job 3:

3.1 In the case of job 3, I've merged the two branches.

No alt text provided for this image

3.2 For merging, I've given the credentials of the GitHub account and specified which branches are required to get merged with the master and hence configured.

No alt text provided for this image

3.3 In the next step, job chaining is set and the upstream jobs are feature_branch_job and master_branch_job and this job will only trigger if the upstream jobs will run successfully and completely.

No alt text provided for this image

3.4 In the next step, I've configured "branch to push" and "Target remote name".

No alt text provided for this image

Running

OKAY! IT'S TIME TO HAVE SOME FUN WITH AUTOMATION!


Here is the content on the page before the operation

No alt text provided for this image


a) Adding the content by "dev1" in the file.

No alt text provided for this image

b) As the "dev1" developer commit and push it to the GitHub repository or you can set up automatic pushing process by writing a bash script in ./git/hooks folder you can simply name that file as "post-commit" or something of your choice. You can write that script as follows:

#!/bin/bash

echo "This will push your content automatically to github. Sit back and relax."

git push

c) As dev1 made a commit, feature branch job will run and after that other connected or technically known as downstream jobs will run in the "build executor".

No alt text provided for this image

d) Automatically triggered master branch job.

No alt text provided for this image

e) Automatically triggered merge job

No alt text provided for this image

f) As all the jobs will finish, the data is transferred in "production" container and new data is shown.

No alt text provided for this image



Currently, I am pursuing my internship in the field of "DevOps Assembly Lines"

Thank you so much for investing your precious time in reading this article. I hope you all liked it.

Nived V.

Solutions Architect @ Red Hat | Application Modernization | Infrastructure Modernization | Hybrid & Multi Cloud Migrations

4 年

These are the kind of things that make you stand out of the crowd!! Going that extra mile! Kudo's to Ashutosh Saxena !! Very well written article!! ??????

Nikhil Goyal

IT Enthusiast | Internet of Things | Aws Cloud | DevOps | RedHat certified

4 年

Great representation of the work done. Its so simple to understand from a high level diagram. ????

Asha Bulani

DevOps Engineer, RHCSA Certified | Ansible Certified l ACE in GCP

4 年

Highly appreciatable ashutosh ! I wish your to grow alot! ??

Ashita Dashottar

SDE @ Amazon || Northeastern University

4 年

Keep it up!

Nikhil Maheshwari

SRE II - Zeta | Ex-Delhivery | DevOps | Cloud | RHCE | CKA | Open Source Contributor

4 年

Great work ????

要查看或添加评论,请登录

社区洞察

其他会员也浏览了