Building a CI/CD pipeline with Docker

CI/CD Terminology :

Docker Registry: A repository of images. Registries are public or private that contain images for download. Some registries allow users to upload images to make them available to others

Dockerfile: A configuration file with build instructions for Docker image.Dockerfile automate the steps for image customization.

As we discussed two types of registry are available:

Public Docker Registry: Users just want to explore or want a in-build solution. They can use hosted docker registries. Docker hub is the example of public registry where user can host the registry.

Private Docker Registry: Lot of organization have security concern for public docker registry.

CI/CD workflow

  • Developers pushes a Commit to Github
  • Github uses a webhook to notify jenkins for update
  • Jenkins pull the Github repository, including the dockerfile describing the image as well as the application and test code
  • Jenkins builds a docker image on the jenkins slave node
  • Jenkins intestates the docker container  on the slave node and does appropriate testing.
  • If image are successful the image is pushed to docker registry.
  • An tester/ user an pull the image from the docker registry and use it.

 

 echo “# projectnode” >> README.md
git init
git add README.md
git commit -m “first commit”
git remote add origin https://github.com/amitopenwriteup/projectnode.git
git push -u origin master

Jenkins setup

https://pkg.jenkins.io/redhat/

sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat/jenkins.repo
sudo rpm –import https://pkg.jenkins.io/redhat/jenkins.io.key
yum install jenkins

Installing Github plugins on jenkins : http<ip>:8080

Create slave node in Jenkins->configuration->Manage nodes

Create a project in jenkins and configure it for source code management

Now map the gitlab in jenkins project

Note: I have created local gitlab in my centos follow the steps mentioned in below blog: https://www.server-world.info/en/note?os=CentOS_7&p=gitlab

Even we can create local docker registry as well

yum install docker-registry

systemctl start docker-registry

In this blog we have created gitlab,jenkins and docker registry.

Now push docker file and code to gitlab. Configure the webhook in git and configure jenkins.

Below is the git configuration

 

All the components of CI/CD are ready in-house lab.

 

configure git on personal linux box

Developers , those want to use git can map linux virtual machine to github. In this way dev can port the code to github. Its easy to maintain the repository as well.

  1. Create a account on github.com
  2. After creating the account ,create a repository for yourself.
  3. SSH to your linux box and generate rsa key
    • ssh-keygen -t rsa -C “<emailid>
      copy the public key from .ssh/<>.pub to git
  4. Copy the public key to github
    • git3
  5. Commands to add file to new repository
    • git config –global user.email “<email id>”

      git clone git@github.com:<username>/mycode.git
      Cloning into ‘mycode’…
      remote: Counting objects: 3, done.
      remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
      Receiving objects: 100% (3/3), done.
      Checking connectivity… done.

      root [ ~ ]# cd mycode
      root [ ~/mycode ]# git add README.md
      root [ ~/mycode ]# git commit -m “add README.md”
      On branch master
      Your branch is up-to-date with ‘origin/master’.
      nothing to commit, working directory clean

      root [ ~/mycode ]# git push -u origin master
      Warning: Permanently added the RSA host key for IP address ‘192.30.252.129’ to the list of known hosts.
      Branch master set up to track remote branch master from origin.
      Everything up-to-date

    • Screenshot from github
    • git1
  6. Commands to add file to existing repository
    • cd <existing folder>

      root [ / ]# cd root/mycode/
      root [ ~/mycode ]# ls
      README.md
      root [ ~/mycode ]# git init
      Reinitialized existing Git repository in /root/mycode/.git/

      root [ ~/mycode ]# git add README1.md
      root [ ~/mycode ]# git commit README1.md

      [master 86ccf2f] new file: README1.md
      1 file changed, 0 insertions(+), 0 deletions(-)
      create mode 100644 README1.md

      root [ ~/mycode ]# git push -u origin master
      Counting objects: 3, done.
      Compressing objects: 100% (2/2), done.
      Writing objects: 100% (3/3), 278 bytes | 0 bytes/s, done.
      Total 3 (delta 0), reused 0 (delta 0)
      To git@github.com:amit23comp/mycode.git
      fc752b5..86ccf2f master -> master
      Branch master set up to track remote branch master from origin

    • git2

 

The Git Story: export pyvmomi git

In this blog we will be using some real time project to explore it.

Below is the github link:

https://github.com/vmware/pyvmomi

vpsherepython

https://github.com/vmware/pyvmomi.git

$ git clone https://github.com/vmware/pyvmomi-community-samples.git

You can download the offline zip and perform the below steps: pyvmomi-master.zip

git add .
git commit -m “committing pyvmomi”

[root@mytest pyvmomi-master]# git log
commit e3155b203764691577f82a6cc7e0b841fd5f4256
Author: Amit <amit@openwriteup.com>
Date: Mon Oct 19 09:29:43 2015 -0400
committing pyvmommi

This link will provide details for development pyvmommi:

http://vmware.github.io/pyvmomi-community-samples/#getting-started

The Git story: Add,commit,delete commands

In this blog, we will see git in details. Git has three stages:
-Working dir
-Staging
-Repository

Whenever we create a new file it would be part of working dir. We need to add this file in staging.Post staging we need to commit to store changes in repository

– Initial stage of working dir
[root@mytest amit]#
[root@mytest amit]# git status
# On branch master
nothing to commit (working directory clean)
[root@mytest amit]#

-Created two file test2.sh and test3.sh in working dir
[root@mytest amit]# git status
# On branch master
# Untracked files:
# (use “git add <file>…” to include in what will be committed)
#
# test2.sh
# test3.sh
nothing added to commit but untracked files present (use “git add” to track)

-Adding test2.sh in staging and commit
[root@mytest amit]#
[root@mytest amit]# git add test2.sh
[root@mytest amit]# git status
# On branch master
# Changes to be committed:
# (use “git reset HEAD <file>…” to unstage)
#
# new file: test2.sh
#
# Untracked files:
# (use “git add <file>…” to include in what will be committed)
#
# test3.sh

#git commit -m “second file”
[master 8a31c70] Second file
Committer: Amit <amit@openwriteup.com>
1 files changed, 2 insertions(+), 0 deletions(-)
create mode 100644 test2.sh

Want to check diff between files version

git diff
[root@mytest amit]# git diff
diff –git a/test.sh b/test.sh
index 7a870bd..2b56848 100644
— a/test.sh
+++ b/test.sh
@@ -1,2 +1,2 @@
#!/bin/bash
-echo “Hello World”
+echo “i theHello World”

1 #!/bin/bash
2 echo “i theHello World”

If file is added in staging ,it will not show in diff.

[root@mytest amit]# git diff
[root@mytest amit]# git diff –staged
diff –git a/test.sh b/test.sh
index 7a870bd..2b56848 100644
— a/test.sh
+++ b/test.sh
@@ -1,2 +1,2 @@
#!/bin/bash
-echo “Hello World”
+echo “i theHello World”

git to Delete file:
git rm test.sh
rm ‘test.sh’

[root@mytest amit]# git status
# On branch master
# Changes to be committed:
# (use “git reset HEAD <file>…” to unstage)
#
# deleted: test.sh
#

root@mytest amit]# git commit -m “deleted”
[master ab81bbb] deleted
Committer: Amit <amit@openwriteup.com>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
1 files changed, 0 insertions(+), 2 deletions(-)
delete mode 100644 test.sh
[root@mytest amit]# git status
# On branch master
nothing to commit (working directory clean)

Renaming the file in Git
[root@mytest amit]# git mv test2.sh test4.sh
[root@mytest amit]# git status
# On branch master
# Changes to be committed:
# (use “git reset HEAD <file>…” to unstage)
#
# renamed: test2.sh -> test4.sh
#
[root@mytest amit]# git commit -m “renamed”
[master 1c8445b] renamed
Committer: Amit <amit@openwriteup.com>1 files changed, 0 insertions(+), 0 deletions(-)
rename test2.sh => test4.sh (100%)
[root@mytest amit]# git status
# On branch master
nothing to commit (working directory clean)

The Git Story: Git Repository, add and commit changes

Git Repository:

To initialize the project. Git start tracking the changes.

[root@mytest ~]# mkdir /root/amit
[root@mytest ~]# cd /root/amit
[root@mytest amit]# git init
Initialized empty Git repository in /root/amit/.git/

Make changes,add changes and commit changes
[root@mytest amit]# ls -la
total 12
drwxr-xr-x. 3 root root 4096 Oct 19 04:49 .
dr-xr-xr-x. 9 root root 4096 Oct 19 04:49 ..
drwxr-xr-x. 7 root root 4096 Oct 19 04:49 .git
[root@mytest amit]# ls -la .git
total 40
drwxr-xr-x. 7 root root 4096 Oct 19 04:49 .
drwxr-xr-x. 3 root root 4096 Oct 19 04:49 ..
drwxr-xr-x. 2 root root 4096 Oct 19 04:49 branches
-rw-r–r–. 1 root root 92 Oct 19 04:49 config
-rw-r–r–. 1 root root 73 Oct 19 04:49 description
-rw-r–r–. 1 root root 23 Oct 19 04:49 HEAD
drwxr-xr-x. 2 root root 4096 Oct 19 04:49 hooks
drwxr-xr-x. 2 root root 4096 Oct 19 04:49 info
drwxr-xr-x. 4 root root 4096 Oct 19 04:49 objects
drwxr-xr-x. 4 root root 4096 Oct 19 04:49 refs

[root@mytest amit]# git add .
[root@mytest amit]# git commit -m “First Commit”
[master (root-commit) 609a623] First Commit
Committer: Amit <amit@openwriteup.com>
1 files changed, 2 insertions(+), 0 deletions(-)
create mode 100644 test.sh

Writing commit messges:
[root@mytest amit]# git log
commit 609a623c90c643377b12810e5efb1d789a4bdf81
Author: Amit <amit@openwriteup.com>
Date: Mon Oct 19 04:56:05 2015 -0400
First Commit

Shows no of commit:

git log -n 1
commit 609a623c90c643377b12810e5efb1d789a4bdf81
Author: Amit <amit@openwriteup.com>
Date: Mon Oct 19 04:56:05 2015 -0400
First Commit

until and since keyword in Git log
[root@mytest amit]# git log –since=2015-10-19
[root@mytest amit]# git log –since=2015-10-18
commit 609a623c90c643377b12810e5efb1d789a4bdf81
Author: Amit <amit@openwriteup.com>
Date: Mon Oct 19 04:56:05 2015 -0400
First Commit
[root@mytest amit]# git log –until=2015-10-18
[root@mytest amit]# git log –until=2015-10-19
commit 609a623c90c643377b12810e5efb1d789a4bdf81
Author: Amit <amit@openwriteup.com>
Date: Mon Oct 19 04:56:05 2015 -0400
First Commit

Check author name using git
[root@mytest amit]# git log –author=”Amit”
commit 609a623c90c643377b12810e5efb1d789a4bdf81
Author: Amit <amit@openwriteup.com>
Date: Mon Oct 19 04:56:05 2015 -0400
First Commit

The Git story: Git installation and configuration

Git can be installed on Windows,linux and mac-os Packages can be downloaded from: www.git-scm.com.

For windows and MAC, we can download offline packages.for linux vendors provide the repo location

Debian/Ubuntu
$ apt-get install git
Fedora
$ yum install git
Gentoo
$ emerge –ask –verbose dev-vcs/git
Arch Linux
$ pacman -S git
openSUSE
$ zypper install git
FreeBSD
$ cd /usr/ports/devel/git
$ make install
Solaris 11 Express
$ pkg install developer/versioning/git
OpenBSD
$ pkg_add git

In my lab vm, I have installed using yum.

yum install git

# git –version
git version 1.7.1

git maintain the three configuration files:

system:
Linux : /etc/gitconfig
Windows: program files/git/etc/gitconfig

user:
Windows: ~/.gitconfig
Linux: $HOME\.gitconfig

project:
my_project/.git/config
From command line , if we want to modify file:
-System
git config –system
-User
git config –global
-Project
git config

Lab commands:
git config –global user.name “Amit”
git config –global email.name “Amit@openwriteup.com”

git config –list

user.name=Amit
email.name=Amit@openwriteup.com

git config user.name

ls -la
cat .gitconfig
[user]
name = Amit
[email]
name = Amit@openwriteup.com

For setting Vim editor and color

git config –global core.editor “vim” ; git config –global color.ui true
git config –list
user.anme=Amit
user.name=Amit
email.name=Amit@openwriteup.com
core.editor=vim

The Git story : Source code management

This blog will give the history of Version control management, source code management.
I will be more focusing on the history of Version control system which is mainly focused on source code management. Lot of tools came up for source code management, and got fame across the time.

SCM: Source code management
VCS: Version control System

Some of the famous SCM are mentioned below:

-Source code control system (SCCS)
1972,free with UNIX
-Revision Control system (RCS)
1982,open source
-Concurrent Version System (CVS)
1986-1990 open source
-Apache subversion (SVN)
2000,open source
-Bitkeeper SCM
2005,closed source,propriety, Community version was free with some features limit.

Before we go and understand git, two types of maintaining repository:

-Central repository : Centralized repository case code are stored on centralized location. Anytime if you want to perform code changes, you need to check out the code,and make changes and then perform the check-in.They track changes across version.
-Distributed version control:Different users maintain their own repositories, instead of working from central repository Changes are stored as “change sets” or “patches”.

In April 2005, Linux genius “Linus Torvalds” created git, A distributed source code management system. Git uses concept of distributed version control, doesn’t require any centralize repository. Any user who use git, has to maintain local copy, only changes are submitted.

Benefits with git:
-no need to communicate with center server
-This makes git really faster
-No network access required
-no single point of failure
-developers can work independently.

Next blog, we will see how to install git, and submit changes.