DEV Community

Cover image for Building a Docker-Jenkins CI/CD Pipeline for a Python App (Part 1)
Nyukeit
Nyukeit

Posted on • Edited on • Originally published at nyukeit.dev

Building a Docker-Jenkins CI/CD Pipeline for a Python App (Part 1)

Introduction

In this article, we will look at how we can deploy an app using a CI/CD pipeline involving git, GitHub, Jenkins, Docker and DockerHub. The basic premise is that when a code update is pushed to git, it will get updated on GitHub. Jenkins will then pull this update, build the Docker Image from a Dockerfile and Jenkinsfile configuration, push it to Docker Hub as a registry store, and then pull it and run it as a container to deploy our app.

Prerequisites

  1. We will use a Python app for this tutorial. The sample app will be included in the GitHub repo.
  2. GitHub account to sync our local repo and connect with Jenkins.
  3. Docker Hub account. If you do not already have one, you can create it at hub.docker.com

Installing/Updating Java

First we will check if Java is installed and what version is it.



java -version


Enter fullscreen mode Exit fullscreen mode

Java Not Installed

As you can see, it shows Java is not installed.

Since Jenkins will require Java 11, we will go ahead install it using the official documentation of Jenkins.



sudo apt-get install -y openjdk-11-jre


Enter fullscreen mode Exit fullscreen mode

Java Install

Once the installation is complete, you can now check and verify the java version again.



java -version


Enter fullscreen mode Exit fullscreen mode

Java Installed

As we can see, Java is now successfully installed with version 11.0.17.

Now, let's install Git.

Installing Git

Git will help us in maintaining and versioning our code in an efficient manner.

First let us check if Git is already available in our system or not.



git --version


Enter fullscreen mode Exit fullscreen mode

Git Version

As we can see, Git was already installed on the system with the version 2.17.1. If you still do not have it installed, you can install it using this command:



sudo apt-get install -y git


Enter fullscreen mode Exit fullscreen mode

Configuring Git (Local Repo)

Let's first create a folder for our project. We will be working inside this folder throughout the tutorial.



mkdir pythonapp


Enter fullscreen mode Exit fullscreen mode

We will initialize our local Git repository inside this folder.



cd pythonapp


Enter fullscreen mode Exit fullscreen mode

But before we initialize our local repository, we need to make some changes to the default Git configuration.



git config --global init.defaultBranch main


Enter fullscreen mode Exit fullscreen mode

By default, Git uses 'master' as the default branch. However, GitHub and most developers like to use 'main' as the default branch.

Further, we will also configure our name and email ID for Git.



git config --global user.name "your_name"
git config --global user.email "your@email.com"


Enter fullscreen mode Exit fullscreen mode

To verify your modifications to the Git configuration, you can use this command:



git config --list


Enter fullscreen mode Exit fullscreen mode

Git Configurations

Now it's time to initialize our local repository.



git init


Enter fullscreen mode Exit fullscreen mode

Initialising Git

This will create an empty repository in the folder. You can also alternatively create a repository on GitHub first and then clone it to your local system.

Setting up GitHub (Remote Repo)

Our local Git repository is not setup and initialized. We will now create a remote repo on GitHub to sync with local.

Login to your GitHub account and click on your Profile picture. Click on 'Your Repositories'.

On the page that opens, click on the green 'New' button.

Let's name our repo 'pythonapp' to keep it same as our folder name. This is not necessary but it will keep things simpler.

Creating GitHub Repository

Keep the repository as 'Public' and click on 'Create Repository'

Connecting to GitHub

For this tutorial, we will use SSH to connect the local repo to our remote repo. Please note that GitHub has stopped allowing username/password combinations for connections. If you wish to use https instead, you can check out this tutorial to connect using Personal Access Tokens.

First we will create an SSH key in our Ubuntu system.



ssh-keygen


Enter fullscreen mode Exit fullscreen mode

Press 'enter' three times without typing anything.

Generating an SSH Keypair

This will create an SSH key in your system. We will use this key in our GitHub account. To access the key, use this command



cat ~/.ssh/id_rsa.pub


Enter fullscreen mode Exit fullscreen mode

Copy the entire key.

On GitHub, go to your repository and click on 'Settings'.

On the left, in the 'Security' section, click on 'Deploy Keys'.

GitHub SSH Key Addition

Name the key to whatever you wish. Paste the key that you copied from the terminal inside the 'Key' box. Be sure to tick the 'Allow Write Access' box.

Now click on 'Add Key'. We now have access to push to our remote repo using SSH.

Now we will add the remote that will allow us to perform operations to the remote repo.



git remote add origin git@github.com:nyukeit/pythonapp.git


Enter fullscreen mode Exit fullscreen mode

To verify your remote



git remote


Enter fullscreen mode Exit fullscreen mode

Verifying our Git Remotes

To verify and connect our configuration, we will do



ssh -T git@github.com


Enter fullscreen mode Exit fullscreen mode

When prompted, type 'yes'. You should see a message that says 'You have successfullly authenticated, but GitHub does not provide shell access.'

GitHub SSH Connection Verifiction

Python App

Let's create Python app that will display Hello World! in the browser when executed.

Inside your terminal, make sure you are in the project folder. Create a folder named 'src' and create a file name 'helloworld.py' inside this folder like this:



mkdir src
cd src


Enter fullscreen mode Exit fullscreen mode


sudo nano helloworld.py


Enter fullscreen mode Exit fullscreen mode

Now let's write a Python script! Inside the nano editor, type this:



from flask import Flask, request
from flask_restful import Resource, Api

app = Flask(__name__)
api = Api(app)

class Greeting (Resource):
    def get(self):
        return 'Hello World!'

api.add_resource(Greeting, '/') # Route_1

if __name__ == '__main__':
    app.run('0.0.0.0','3333')


Enter fullscreen mode Exit fullscreen mode

Press ctrl + x + y to save the file.

Head over to Part 2 where we will go through the installation & configuration of Jenkins, Docker and creating the scripts to finish our pipeline.

Top comments (0)