#90DaysOfDevOps Challenge - Day 80 - DevOps Project 1 - Jenkins Freestyle Project

#90DaysOfDevOps Challenge - Day 80 - DevOps Project 1 - Jenkins Freestyle Project

Welcome to Day 80 of the #90DaysOfDevOps challenge. Today, we are diving into our first DevOps project, where we will automate the building and deployment process of a web application using Jenkins and GitHub. This project will empower you to efficiently manage the development lifecycle and streamline the deployment process. So, let's get started!

Project Description

Project Aim: Automate the building and deployment process of a web application using Jenkins and GitHub.

Project Overview: In this project, we will create a Jenkins pipeline that will be triggered automatically by GitHub whenever changes are made to the code repository. The pipeline will include multiple stages such as cloning the code from GitHub, building the application and deploying the application on the target server. Additionally, we'll set up notifications and alerts to inform us in case of any failed builds or deployments.

Hands-on Project: Jenkins Freestyle Project

  1. Set up GitHub Repository:

    • If you haven't already, create a GitHub repository to host the web application's code.

    • Push your web application code to the repository. I'll use my django-notes-app repository

  2. Install and Configure Jenkins:

    • Create a new EC2 instance. Install Docker and Jenkins using the below User Data file.

        #!/bin/bash
      
        echo "Docker installation"
        echo "------------------------------------------------------------------"
        sudo apt update
        sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
        curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
        echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
        sudo apt update
        sudo apt install -y docker-ce docker-ce-cli containerd.io
        sudo systemctl start docker
        sudo systemctl enable docker
        sudo usermod -aG docker $USER
      
        echo "Jenkins installation"
        echo "------------------------------------------------------------------"
        sudo apt update
        sudo apt install -y openjdk-17-jre
        curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | sudo tee \
          /usr/share/keyrings/jenkins-keyring.asc > /dev/null
        echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
          https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
          /etc/apt/sources.list.d/jenkins.list > /dev/null
        sudo apt-get update
        sudo apt-get install -y jenkins
      
    • Once created, connect using your preferred method and verify Docker and Jenkins are up and running.

  3. Install Required Jenkins Plugins:

    • Access the Jenkins dashboard in your web browser and install the suggested plugins.

    • Create your user and log in with the newly created username and password.

  4. Create Jenkins Pipeline:

    • In the Jenkins dashboard, click on "New Item."

    • Enter a name for your pipeline and select "Freestyle" as the project type.

    • Under "Source Code Management," choose "Git" and provide your GitHub Repository URL.

    • In the "Build Triggers" section, select Poll SCM to trigger the Jenkins Pipeline when changes are discovered. We'll schedule it to run a scan every minute.

    • Select "Delete workspace before build starts" to ensure every time we run the Jenkins Pipeline, there are no residual files.

    • Lastly, we will set up our Pipeline in the "Build Steps" section. Let's add a new build step, with the type 'Execute shell' and add the below instructions.

        echo "Clone Code Stage --------------------------------"
        cd /var/lib/jenkins/workspace/devops-project-1
        git clone https://github.com/estebanmorenoit/django-notes-app
        echo "Build Stage --------------------------------"
        cd /var/lib/jenkins/workspace/devops-project-1/django-notes-app
        docker build . -t django-notes-app
        echo "Image created"
        echo "Deploy Stage --------------------------------"
        docker stop django-notes-app || true
        docker rm django-notes-app || true
        docker run -d -p 8000:8000 --name django-notes-app django-notes-app
        echo "Container is created and running"
      

    • Apply the changes, and save the Pipeline.

  5. Run the Pipeline:

    • Make changes to your web application code and push them to the GitHub repository.

    • The GitHub changes will automatically trigger the Jenkins pipeline.

    • Monitor the pipeline's progress in the Jenkins Pipeline dashboard.

  6. Test the Pipeline:

    • Check the job's console output, where we can see if the Docker container has been successfully created.

    • Navigate to http://<ec2-instance-public-ip-address>:8000 and access your newly deployed app.

  7. Configure Notifications and Alerts:

    • Click on Manage Jenkins->Configure System. Here scroll down to the Email Notification section. Now enter the details as the following image

    • If you use Gmail with MFA enabled, follow the below steps:

      Create a custom app in your Gmail security settings.

      1. Log in to Gmail with your account

      2. Navigate to https://security.google.com/settings/security/apppasswords

      3. In 'select app' choose 'custom', give it an arbitrary name and press generate

      4. It will give you 16 chars token.

Use the token as a password in combination with your full Gmail account and two-factor authentication will not be required.

  • Use Jenkins' built-in features to set up email notifications for pipeline status (success/failure) in the "Post-build Actions"

  • Modify the Pipeline Build Steps to ensure the job fails, and wait for an email notification to land in your inbox

Congratulations on completing Day 80 of the #90DaysOfDevOps challenge! Today, we successfully set up a Jenkins pipeline to automate the building and deployment of a web application using GitHub. We configured the Jenkins pipeline to be triggered automatically using Poll SCM whenever changes are pushed to the GitHub repository. Additionally, we set up notifications and alerts to stay informed about the pipeline's status.

Tomorrow, we'll take our DevOps journey further and explore a new project where we'll upgrade this project by using a Jenkins Declarative Pipeline instead. Stay tuned!

Did you find this article valuable?

Support Esteban Moreno by becoming a sponsor. Any amount is appreciated!