Automating building and deployment using Jenkins: End-to-End Series (Part — 7)

Accredian Publication
7 min readMar 23, 2021

--

By Hemanka Sarmah

In our previous article, we looked at how to deploy a docker container in Kubernetes. Let’s see how we can automate the whole process.

What is CI/CD?

  • CI/CD or CICD generally refers to the combined practices of continuous integration and either continuous delivery or continuous deployment.
  • CI/CD bridges the gaps between development and operation activities and teams by enforcing automation in building, testing, and deployment of applications.

What is Jenkins?

  • One of the most famous Open Source CI/CD tools.
  • The leading open-source automation server
  • It provides hundreds of plugins to support building, deploying and automating any project

Advantages of Jenkins:

  • Platform independent as it is developed in java.
  • Open-Source
  • Has a huge community with 1000 plug-ins.

Also, Check out our Article on:

  1. Data Abstraction: End-to-End Series (Part — 1)
  2. Data Preprocessing and EDA: End-to-End Series (Part — 2)
  3. Model Building and Experimentation: End-to-End Series (Part — 3)
  4. Creating a WebApp using Flask+Gunicorn on Ubuntu Server: End-to-End Series (Part — 4)
  5. Containerizing the WebApp using Docker: End-to-End Series (Part — 5)
  6. Scaling our Docker Container using Kubernetes: End-to-End Series (Part — 6)

Installing Git

Run the following commands one by one.

- sudo apt-get update- sudo apt install git- git --version

Setting up Gitlab

  • Go to the GitLab site and create an account from the link.
  • Once the account has been set up. Click on create a new project
  • Then click on Blank project. You can also copy the existing repository from GitHub using the import repository option.
  • Make the repository public

Pushing files to the GitLab repository from Ubuntu Server

  • Go to the file directories in the server.

→ Add a Master branch to push

sudo git remote add origin <repository_link>

→ Select files to be added to the repository

git add .

→ Commit

git commit -m "commiting to repository"

→ Pushing files to GitLab repository

git push origin master
  • Reload your Gitlab repository and you will see all the files present.

Creating a Jenkinsfile

  • Jenkinsfile will be used by Jenkins to set up the pipeline and run the container accordingly.
  • You can create Jenkinsfile in the server or can directly create one file in the gitlab.
pipeline {
agent any
stages {
stage('Clone Repository') {
/* Cloning the repository to our workspace */
steps {
checkout scm
}
}
stage('Build Image') {
steps {
sh 'sudo docker build -t churnmodel:v1 .'
}
}
stage('Run Image') {
steps {
sh 'sudo docker run -d -p 8050:8000 --name churnapp churnmodel:v1'
}
}
stage('Testing'){
steps {
echo 'Testing..'
}
}
}
}

Setting up Jenkins on Server

In article 4 of the series, we saw how to connect to our server.
We will use the Jenkins container to run the Jenkins service for our purpose.

→ Pull the Jenkins image from the docker hub

docker pull jenkins/jenkins:lts

→ Run the Jenkins container

docker run -it -d --rm -p 5000:8080 \
-v /var/run/docker.sock:/var/run/docker.sock \
--name jenkins \
jenkins/jenkins:lts

→ Open Container bash to install docker

open bash

docker exec -it -u root jenkins bash

Install Docker

  • Write the command given below in the bash and hit Enter.
apt-get update && \
apt-get -y install apt-transport-https \
ca-certificates \
curl \
gnupg2 \
software-properties-common && \
curl -fsSL https://download.docker.com/linux/$(. /etc/os-release; echo "$ID")/gpg > /tmp/dkey; apt-key add /tmp/dkey && \
add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/$(. /etc/os-release; echo "$ID") \
$(lsb_release -cs) \
stable" && \
apt-get update && \
apt-get -y install docker-ce

Check docker version

  • After the installation is complete, you can check the version of docker.
docker --version

Exit bash

ctrl + D

→ Navigate to the address

  • To open the Jenkins service go to your browser.
  • On the address bar type in <Server_IP>:5000
    5000 because we ran our Jenkins container on port 5000.
  • On the page copy the address given for the initial password.

→ Getting initial Password

  • Once the Jenkins Service is up and running it will ask for an initial password to log in.
  • To get the initial password to navigate back to the server CLI.
  • check for Jenkins container
docker ps -a
  • Use the following command to get the initial password.
docker exec <container_name/id> cat /var/jenkins_home/secrets/initialAdminPassword
  • Copy the password from the terminal and paste it to the Jenkins page. Click on Continue.
  • Click on Install Suggested Plugins.
  • Create Username and Password
    After creating the Credentials click on save and continue.
  • Once you log into Jenkins it will show you the URL where Jenkins is working. Just click Save and Finish.

Installing Plugins

  • Click on ‘Manage Jenkins’
  • Navigate and click on ‘Manage Plugins’
  • Install Docker plugins by going on the available section.
  • Click on install without restart
  • Once the plugins are installed click on the go to the top page.
  • Then do the same for Gitlab plugins.

Setting up Jenkins Pipeline

→ Creating a Pipeline Job

  • Click on New item or Create a job.
  • Give your job a name and select pipeline and click ok.
  • After clicking OK, navigate to Pipeline.
  • Select Project script from SCM (Source Code Management)
  • Then, select Git.
  • Add Repository URL.
  • To get the repository URL to go to your Gitlab and click on Clone and copy the URL.

→ Building Pipeline

  • To build the pipeline click on the build now

→ Accessing docker container

  • Navigate to your Ubuntu <Server_IP>: port

Follow us for more upcoming future articles related to Data Science, Machine Learning, and Artificial Intelligence.

Also, Do give us a Clap👏 if you find this article useful as your encouragement catalyzes inspiration for and helps to create more cool stuff like this.

Visit us on https://www.insaid.co/

--

--

Accredian Publication
Accredian Publication

Written by Accredian Publication

One of India’s leading institutions providing world-class Data Science & AI programs for working professionals with a mission to groom Data leaders of tomorrow!

No responses yet