Giter Site home page Giter Site logo

kuralabs_deployment_3's Introduction

Deployment 3

In this deployment we will be deploying our application to our customized VPC in AWS.

Prerequisites

You will need the following tools to complete this tutorial:
  • Install Jenkins on an EC2 (if you haven’t already) with an Ubuntu image and add ports 22 and 8080
  • Create an EC2 in your Public Subnet of VPC
    • Add ports 22 and 5000
    • Install the following packages
      • default-jre
      • python3-pip
      • python3.10-venv
      • nginx

Configure and connect a Jenkins agent to Jenkins server

Access the UI of the Jenkins server EC2 (i.e. public_ip:8080) and select the Build Executor status in the bottom left corner of the menu.

Now enter the configurations below:

  • Name: awsDeploy

  • Description: Deployment server

  • Number of executors: 1

  • Remote root directory: /home/ubuntu/agent

  • Labels: awsDeploy

  • Usage: only build jobs with label…

  • Launch method: launch agents via ssh

  • Host: {Enter the public IP of your EC2 in the Public subnet and not this text}

  • Credentials:

    • Select “Add” => “Jenkins” => Kind: “SSH username with private key”
    • Enter the ID, Description, username (JenkinsAgent, Deployment agent server, ubuntu)
  • To add the key, select “Enter Directly” => select “add” => copy and paste the private key of the aws key pair used when creating your Jenkins server

    into the white box and save.

  • Host key verification strategy: non verifying verification strategy

  • Availability: keep this agent online as much as possible

  • Save the configurations and wait for Jenkins to connect to the agent.

Create a Pipeline build in Jenkins:

SSH into your Jenkins Agent EC2, your second EC2, and edit the “/etc/nginx/sites-enabled/default” configuration file.

Navigate to the server block and change port 80 with 5000 server { listen 805000 default_server; listen [::]:805000 default_server; ** Next navigate to the location block and replace it with the text below:** location / { proxy_pass http://127.0.0.1:8000; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }

Download the Jenkins plugin entitled “Pipeline Keep Running Step” on Jenkins(server) UI
Now edit the Jenkinsfile in your repo to the script below:
pipeline {
    agent any
    stages {
        stage ('Build') {
            steps {
                sh '''#!/bin/bash
                python3 -m venv test3
                source test3/bin/activate
                pip install pip --upgrade
                pip install -r requirements.txt
                export FLASK_APP = application
                flask run &
                '''
              
            }
        }
        stage ('test') {
            steps {
                sh '''#!/bin/bash
                source test3/bin/activate
                py.test --verbose --junit-xml test-reports/results.xml 
                '''
              
            }
            post {
                always {
                    junit 'test-reports/results.xml'
                }
            }
        }
    
    stage ('Clean') {
        agent {
            label 'awsDeploy'
        }
        steps {
            sh '''#!/bin/bash
            if [[$(ps aux | grep - i "gunicorn" | tr -s " " | head -n 1 | cut -d " " -f 2) != 0]]
            then
            ps aux | grep -i "gunicorn" | tr -s " " | head - n 1 | cut -d " " -f 2 > pid.txt
            kill $(cat pid.txt)
            exit 0
            fi
            '''
            
        }
    }
    stage ('Deploy') {
        agent {
            label 'awsDeploy'
        }
        steps {
            keepRunning {
                sh '''#!/bin/bash
                pip install -r requirements.txt
                pip install gunicorn
                python3 -m gunicorn -w 4 application:app -b 0.0.0.0 --daemon 
                '''
                
            }
        }
    }
}
}

Configure a multi branch pipeline Make sure you connect Jenkins to your GitHub Repo and then start your build!

    $ sudo apt-get updates
    $ sudo apt-get update
    $ sudo apt-get upgrade -y
    $ sudo apt-get install default-jre git nodejs npm maven libgtk2.0-0 libgtk-3-0 libgbm-dev libnotify-dev
       libgconf-2-4 libnss3 libxss1 libasound2 libxtst6   xauth xvfb -y

Challenges

Original Build

One of the challenges I faced while completing this deployment was not being able to link my Jenkins sever and Jenkins agent in addition to multiple failed builds. Why? For multiple reasons stated below, so be sure not to repeat my mistakes. When trying to establish a connection between my Jenkins server and agent nodes I struggled and eventually through some troubleshooting efforts with my colleagues I realized that I was using the wrong SSH key pair while setting up my Jenkins credentials. Also, I noticed I was using the wrong user and root directory in my Jenkins credentials set-up. I entered “jenkins” and “/home/jenkins” in the username and root directory fields instead of “ubuntu” and “home/ubuntu/agent”.

Build with Cypress Test

Another challenge I encountered was getting my Cypress test to pass. According to the logs from the console output my Cypress configuration file could not be located in the /home/ubuntu/agent02/Workspace folder.

Additional Tools and Tutorials You May Find Useful

https://www.youtube.com/watch?v=RkaqRsockfg https://www.youtube.com/watch?v=lOdrdV0eDrs https://www.jenkins.io/doc/book/installing/linux/#debianubuntu https://installati.one/ubuntu/22.04/python3.10-venv/

Deployment 3 Diagram

kuralabs_deployment_3's People

Contributors

techstacksavvy avatar kura-labs01 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.