MLops task-1: Integration of Jenkins, Docker and Git&GitHub(using master and feature branch)
Richard Nadar
Cyber Security Enthusiast | SOC Analyst | Threat Hunting & Threat Intelligence Enthusiast | Learner
Jenkins workflow:
Task Description:
JOB#1
If Developer push to dev branch then Jenkins will fetch from dev and deploy on dev-docker environment.
JOB#2
If Developer push to master branch then Jenkins will fetch from master and deploy on master-docke environment. Both dev-docker and master-docker environment are on different docker containers.
JOB#3
Manually the QA team will check (test) for the website running in dev-docker environment. If it is running fine then Jenkins will merge the dev branch to master branch and trigger job2.
Before performing the task, there are some pre-requisites:
a] Redhat (RHEL 8 in my case) or any linux OS
b] Account on GitHub
c] Already configured Jenkins Setup
d] Git Bash software on windows or linux
STEP 1:
First create a local repository in Git as well as a repository in GitHub. But if you are using Git for first time then we have to tell Git our GitHub username and email ID by using the following commands.
commands:
git config --global user.email "[email protected]"
git config --global user.name "your_name"
We have to initialize the local repository using git init command. Also, To add a new remote, use the git remote add command on the terminal, in the directory your repository is stored at (here origin is our remote name).
remote origin : Adding remote SCM url to which you want to push the local repository.
STEP 2:
By default both Git and GitHub will give you a master branch. So, first in master branch I created a file named mypage.html and wrote some code in it.
After this I used series of commands and pushed this file to GitHub into master branch.
STEP 3:
Now, I created a feature branch named dev1 using the following command:
By this command, branch is also created and we are also switched to that branch.
In dev1 branch, I updated my code of mypage.html by adding <style> attribute to add background color.
Here also I used series of commands and pushed this file to GitHub into dev1 branch this time.
STEP 4:
Here after doing the above step, I have also created a hook. Git hooks are scripts that run automatically every time a particular event occurs in a Git repository. They let you customize Git's internal behavior and trigger customizable actions at key points in the development life cycle.
I have created a post-commit hook, meaning after every commit automatically it will push to GitHub.
Let's create the jobs in Jenkins.
JOB 1:
This job will check the dev1 branch in the GitHub and copy the code from Jenkins to developer folder in root directory of base OS i.e RHEL 8 also launch a docker image (OS) which is the testing environment if it is not there and if it is there then it will remove that and create a new image (good practice in testing) and is not exposed to the clients. The contents of developer folder will be mounted to a particular folder.
It will check the changes in dev1 branch and will trigger JOB 3 once the testing is successful.
Here I have done Poll SCM, meaning Jenkins will keep on checking GitHub for any new code every minute. We can do remote trigger also, by using the authentication token.
Console output for JOB 1:
JOB 2:
In this job, Jenkins will pull/copy code from master branch. This job will check the master branch in the GitHub and copy the code from Jenkins to master folder in root directory of base OS i.e RHEL 8 also launch a docker image (OS) which is the production environment. If it is not there it will launch a new image/container and if it is there then it will print "Already Running" in console output. This container/environment is exposed to clients as well.
Console output for JOB 2:
JOB 3:
It will merge the two branches only if the dev1 branch is completely tested and as soon as this job is done, master gets changed and JOB 2 is triggered to deploy changes into the production environment.
Console output for JOB 3: