Day 12 Project 2: Todo App - End to End CI/CD Project

Day 12 Project 2: Todo App - End to End CI/CD Project

on AWS

Β·

7 min read

Continuous Integration (CI):

Continuous Integration (CI) πŸ”„ is a development methodology wherein developers contribute code to a shared GitHub repository πŸ™. This repository serves as the primary source from which DevOps Engineers retrieve the code for further processing. The code undergoes a systematic build process using Docker 🐳, resulting in the creation of a Docker image πŸ–ΌοΈ.

Once the code is successfully built, the generated Docker image is then pushed to a repository, with Docker Hub being a common choice 🏠. This repository acts as versioned storage for the Docker images, maintaining a history of changes over time ⏳.

The seamless orchestration of these steps in an ongoing and automated fashion characterizes Continuous IntegrationπŸ”„. In essence, CI ensures that code integration, Docker image creation, and versioning occur continuously, facilitating a streamlined and efficient development pipeline πŸš€.

Continuous Delivery/Deployment (CD)

Upon successfully creating and versioning the Docker image πŸ–ΌοΈ, the next step involves deploying it to the Cloud ☁️, with popular platforms such as AWS or Azure being common choices. When this deployment process is carried out manually, it falls under the umbrella of Continuous Delivery 🚚. However, if the entire deployment workflow is automated, it is termed Continuous Deployment πŸš€. This distinction highlights the efficiency and agility introduced by automation in the software development and deployment lifecycle.

Pipelines

In Jenkins, a widely-used automation server πŸš€, this entire flow can be orchestrated through what is known as pipelines. Jenkins pipelines define the workflow, automating the steps from code integration to deployment βš™οΈ. These pipelines streamline the process, allowing for seamless execution of tasks such as fetching code from GitHub πŸ™, building Docker images πŸ–ΌοΈ, and deploying to the Cloud ☁️.

Jenkins pipelines serve as a powerful tool to automate and manage this end-to-end process in a coordinated and efficient manner πŸ€–.

Prerequisites βœ…

  1. EC2 instance πŸ–₯️

  2. GitHub πŸ™

  3. Docker 🐳

  4. Docker Hub 🏠

  5. Jenkins πŸš€

Steps Included:

InstallationsπŸ› οΈ

To set up Jenkins on an EC2 instance, follow these steps:

  1. Create Instance πŸ–₯️:

    • Provision an EC2 instance on AWS with the desired specifications.
  2. Connect with SSH πŸ”—:

    • Connect to your EC2 instance using SSH. Use the following command:

      ssh -i your-key.pem ec2-user@your-instance-ip

  3. Update System πŸ”„:

    • Update the system packages to ensure you have the latest software:

      sudo yum update -y

  4. Install Java β˜•:

    • Install Java on your EC2 instance. Jenkins requires Java to run.

      sudo apt install openjdk-11-jre

  5. Install Jenkins πŸš€:

    • Download and install Jenkins on the EC2 instance.

        curl -fsSL https://pkg.jenkins.io/debian/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 binary/ | sudo tee \
          /etc/apt/sources.list.d/jenkins.list > /dev/null
        sudo apt-get update
        sudo apt-get install jenkins
      
  6. Edit Inbound Rules in Security πŸ›‘οΈ:

    • Open the AWS Management Console, and navigate to the EC2 instance's security group.

    • Edit inbound rules to allow traffic on port 8080 (Jenkins default port). Add a new rule for port 8080 and save the changes.

  7. Access Jenkins Web Interface 🌐:

    • Open a web browser and navigate to http://your-instance-ip:8080. Jenkins setup wizard will prompt you. Retrieve the initial admin password from the instance:

      sudo cat /var/lib/jenkins/secrets/initialAdminPassword

    • Copy the password and paste it into the Jenkins setup wizard.

  8. Set up Jenkins πŸ› οΈ:

    • Follow the instructions in the Jenkins setup wizard to complete the installation. Install suggested plugins and create an admin user.
  9. Access Jenkins Dashboard πŸš₯:

    • Once setup is complete, log in to the Jenkins dashboard. You can now start configuring Jenkins jobs and pipelines.

This sequence of steps guides you through the process of setting up an EC2 instance, installing Java and Jenkins, and configuring Jenkins to be accessible on port 8080. This lays the foundation for creating continuous integration and continuous deployment pipelines using Jenkins.

Set up Jenkins job:

To initiate the setup of a new Jenkins job, follow these steps:

  1. Create a new job and choose the Pipeline option.

  2. Select the "GitHub project"πŸ™ option and input the GitHub repository URL.

  3. Choose the "Pipeline script"πŸ“œ option and draft the pipeline using a Groovy script.

The structure of the Groovy script for a Jenkins pipeline includes the following key elements:

  • Pipeline: Encompasses the entire script, defining the overall structure.πŸ”„

  • Stages: Represent distinct phases in the pipeline, where each stage comprises a set of related tasks.πŸš€

  • Steps: Within each stage, individual steps are defined, specifying the commands or tasks to be executed.πŸ› οΈ

  • Agent: Specifies the agent where the pipeline runs, facilitating the distribution of workload.πŸ€–

This skeleton in Groovy syntax provides a framework for organizing Jenkins pipelines, ensuring a clear delineation of stages, steps, and an agent to efficiently handle the workload.🌐

Example:

pipeline {

agent any

stages{

stage("Code"){

steps{

echo "Code is cloned"

}

}

stage("Build & Test"){

steps{

echo "Docker build completed"

}

}

stage("Push to Repository"){

steps{

echo "Pushed to Dockerhub"

}

}

stage("Deploy"){

steps{

echo "Docker Deployed on AWS EC2"

}

}

}

}

  1. Clone Code from Git:

    • Retrieve the source code from the Git repository.πŸ™
  2. Install Docker on the Server:

    • Set up Docker on the server where the build process will take place.🐳
  3. Grant Permissions:

    • Provide necessary permissions to the user and Jenkins by adding them to the Docker group.πŸ”
  4. Write Build Command with Docker:

    • Construct the build command using Docker to create the desired image.πŸ› οΈ
  5. Create DockerHub Account:

    • Establish an account on DockerHub, which serves as the repository for your Docker images.🏠

  6. Add DockerHub Credentials:

    • Navigate to "Manage Jenkins" -> "Credentials" -> "System" -> "Global Credentials" -> "Add" and input DockerHub credentials.πŸ”‘

  7. Use "withCredentials" Groovy Syntax:

    • Utilize the "withCredentials" Groovy syntax to embed DockerHub login credentials directly into the code.πŸ–‹οΈ
  8. Retag the Old Built Image:

    • Apply the "docker tag" command to retag the previously built Docker image.🏷️
  9. Login to DockerHub:

    • Execute the DockerHub login command within your Jenkins pipeline.πŸ”’
  10. Push Image to DockerHub:

    • Finally, push the tagged image with the "latest" tag to DockerHub.πŸš€

These steps establish a comprehensive workflow, from code retrieval to Docker image creation, tagging, and ultimately pushing the image to DockerHub, with Jenkins orchestrating the process seamlessly.

  1. Install Docker Compose on Server:

    • Set up Docker Compose on the server to facilitate the deployment of multi-container Docker applications.🐳
  2. Edit Inbound Rules in Security:

    • Navigate to the security settings and modify the inbound rules to include port 8000, allowing the specified network traffic.πŸ”’πŸ”§
  3. Deploy Code on AWS:

    • Execute the necessary command to deploy the code on the AWS infrastructure. This involves orchestrating the deployment of containers or applications using Docker Compose.β˜οΈπŸš€

These steps ensure the proper installation of Docker Compose, adjust security settings, and deploy the code on AWS, streamlining the deployment process for your application.

To inspect the Todo app on a browser:

  1. Automate Code Retrieval from Git:

    • Simplify code retrieval by incorporating a Jenkinsfile directly into the GitHub repository. Choose the "Pipeline from SCM" option in Jenkins, specify the Git repository URLπŸ™, mention the branch, set the Jenkinsfile pathπŸ“œ, and save the configuration.
  2. Build with Declarative Checkout SCM:

    • Upon configuring the pipeline, clicking the build button will trigger the Declarative Checkout SCM processπŸ”„, seamlessly fetching the code from the specified Git repository and branch.

  3. To enable automated processes triggered by GitHub events

    1. Navigate to your GitHub repository settings.

    2. Add a webhook for streamlined automation.

    3. In Jenkins, access the 'Configure' section.

    4. Navigate to 'Build Triggers.'

    5. Choose 'GitHub hook trigger for GitSCM polling.'

    6. Save your configuration changes. πŸ› οΈ"

By incorporating a Jenkinsfile in your GitHub repository and configuring Jenkins to utilize it, you enhance the automation and efficiency of code retrieval, making the process more streamlined and accessible.πŸš€

It enhances efficiency when checking the Todo app on a browser.

"πŸ™Œ Appreciate your time spent exploring this blog!πŸ“š Hope the information was both useful and enlightening.✨🌈

πŸš€ Embrace your learning journey, and feel free to connect if you have any feedback. πŸ€“ Wishing you a joyful exploration!🌟"

Happy Learning:)<3

Β