Skip to content

lakshya-chopra/CD_pipeline_setup

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 

Repository files navigation

CD_pipeline_setup

  • Requirements:

      Python 3 
      Flask : pip3 install Flask
      Docker : can be read at the link given below
      Jenkins
      JDK
      Ngrok
    
  • To set up a CD pipeline, first we'll create a basic web app written in Python's Flask, for this execute the following commands:

    $ mkdir pythonWebApp
    $ cd pythonWebApp
    $ mkdir app
    $ touch app/app.py
    $ vi app/app.py
    
    In `app.py`, paste the following code:
     
    from flask import Flask
      
      app = Flask(__name__)
      
      
      @app.route('/hello')
      def hello_world():
          return "Hello World! How are you doing?"
      
      
      if __name__ == "__main__":
          app.run(host='0.0.0.0', port=8008, debug=True)
    
    

    Save the file and then create a Dockerfile in the pythonWebApp directory (not in the app sub-folder):

    FROM python:3
    
    #creates a new directory myApp/app in the container
    WORKDIR myApp/app
    RUN apt update
    RUN pip3 install flask
    
    COPY app .
    
    EXPOSE 8008
    
    CMD ["python", "app.py", "runserver", "0.0.0.0:8008"]
    
    
  • Run, docker build -t myapp . in the parent folder (i.e. pythonWebApp)

  • After successful completion of the build, run docker images to view the image myapp.

  • Run a container using this image : docker run -p 8008:8008 myapp, use -p flag and specify port 8080, so that we can access the container on the 8080 port of our host.

  • Open your browser, and enter the localhost url along with the port 8080, image below:

  • image

  • Congrats! your first containerized application is successful.

Setting up a CD pipeline using Jenkins:

  • Install Java (preferably OpenJDK-17): sudo apt-get install openjdk-17-jdk, then test it using java -version.
  • Later you may add Java to global environment variables so that other applications can access it.
  • Install git
  • Install Jenkins:
    $ wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key |sudo gpg --dearmor -o /usr/share/keyrings/jenkins.gpg
    $ sudo sh -c 'echo deb [signed-by=/usr/share/keyrings/jenkins.gpg] http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
    $ sudo apt install jenkins
    $ #enable and start
    $ sudo systemctl enable jenkins
    $ sudo systemctl start jenkins
    $ sudo systemctl status jenkins
    
  • Allow Jenkins default port 8080:
    $ sudo ufw allow 8080
    $ sudo ufw status
    $ sudo ufw enable
    $ sudo ufw status
    
  • After this, login in into Jenkins by going to it’s default link: http://127.0.0.1:8008, here enter the initialAdminPassword.
  • Create a new pipeline project, and configure it as a github project:
  • After this, write a Jenkinsfile in your project's root, like this:
  • pipeline {
      agent any
    
      stages {
          stage("Clone Code") {
              steps {
                  echo "Cloning the code"
                  git url: "https://github.com/lakshya-chopra/pythonWebApp.git", branch: "main"
              }
          }
    
          stage("Build") {
              steps {
                  echo "Building the Docker image"
                  sh "docker build -t myapp ."
              }
          }
    
    
          stage("Deploy") {
              steps {
                  echo "Deploying the container"
                  sh "docker run -p 80:80 myapp"
                  
              }
          }
      }
    }
    
    • Also do this:

    • image

    • image

    • image

    • image

GitHub Webhook setup:

  • In your Jenkins dashboard, go to Manage Jenkins -> System Configuration and from there set up the Github server with your credentials:

  • image

  • In your github webhook, you'll need to have a public web url, where github can post data on, this can be done via ngrok or by using any public cloud provider.

    • Install ngrok via: sudo snap install ngrok
    • Create a new ngrok account, and then run ngrok http 80 which directs your the public url -> localhost:80
    • Simulatenously, deploy the app using docker run -it -p 80:80 myapp, and check whether it's accessible on ngrok's website or not.
  • Navigate the settings of your github project, and create a webhook which will send a notification to your app whenever you do some changes to your github repo (you may disable SSL if your payload url is not https secure)

  • image

  • Finally, build the pipeline and do some changes to your github code and see whether you're receiving the notifications or not:

  • like this:

  • image

  • You might face some errors while building, so ensure that git is properly configured (path correctly set in the Git plugin) & that your github credentials are correct and available. Also ensure that your Github branch is named as main, or you'll have to change the jenkins config.

  • Check this for setting Git exe

  • And this too

  • Ensure that Jenkins isn't blocked by some firewall.

  • You may, also tick this option to stop the old builds, whenever you build a new one, as this can lead to port conflicting in docker.

  • image

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published