DEV Community

Vivesh
Vivesh

Posted on

Deploying an Application to a Cloud Provider Using a CI-CD Pipeline

In modern software development, Continuous Integration and Continuous Deployment (CI/CD) pipelines play a pivotal role in automating code delivery processes, improving deployment efficiency, and minimizing human error. This article demonstrates how to deploy an application to a cloud provider using a CI/CD pipeline. We'll focus on deploying to AWS EC2 as the target environment.


Introduction to CI/CD Pipelines

A CI/CD pipeline automates the steps of integrating, building, testing, and deploying code changes to production environments. A well-structured pipeline ensures:

  1. Faster and more reliable deployments.
  2. Automated testing and error detection.
  3. Consistency in application delivery.

For this article, the objective is to automate the deployment of a sample web application to AWS EC2 using Jenkins as the CI/CD tool.


Prerequisites

To successfully set up and deploy the application, the following prerequisites must be met:

  1. Jenkins Setup:

    • Jenkins must be installed and running (locally or on a server).
    • Required plugins:
      • Pipeline
      • Git
      • SSH Agent
  2. Cloud Environment:

    • An AWS EC2 instance created and accessible via SSH.
    • Necessary dependencies (e.g., Node.js, Python, or Docker) installed on the EC2 instance.
  3. Application Code:

    • The repository should include build, test, and deployment scripts:
      • build.sh: Script to build the application.
      • test.sh: Script to run tests.
      • deploy.sh: Script to deploy the application.
  4. Jenkins Credentials:

    • SSH key configured in Jenkins for accessing the EC2 instance.
    • AWS CLI configured on the Jenkins server for optional deployment methods.

Setting Up the CI/CD Pipeline

Below is a step-by-step guide to configure and automate the CI/CD pipeline:

Step 1: Create a New Pipeline Job in Jenkins

  1. Open the Jenkins dashboard.
  2. Click on New Item, enter the job name, and select Pipeline.
  3. Click OK to create the pipeline job.

Step 2: Define the CI/CD Pipeline Script

Add the following script to the Pipeline section of your Jenkins job configuration:

pipeline {
    agent any

    environment {
        SSH_KEY = credentials('aws-ec2-key') // Replace with your SSH key ID in Jenkins
        EC2_USER = 'ec2-user' // Default username for Amazon Linux
        EC2_HOST = 'YOUR_EC2_PUBLIC_IP' // Replace with the EC2 instance public IP
    }

    stages {
        stage('Clone Repository') {
            steps {
                git 'https://github.com/your-repo/sample-app.git' // Replace with your repo URL
            }
        }

        stage('Install Dependencies') {
            steps {
                sh 'npm install' // Example for Node.js application
            }
        }

        stage('Build Application') {
            steps {
                sh './build.sh' // Build script
            }
        }

        stage('Run Tests') {
            steps {
                sh './test.sh' // Test script
            }
        }

        stage('Deploy to AWS EC2') {
            steps {
                sshagent(['aws-ec2-key']) { // Use SSH credentials stored in Jenkins
                    sh '''
                    scp -o StrictHostKeyChecking=no -r ./build/ ${EC2_USER}@${EC2_HOST}:/var/www/html
                    ssh -o StrictHostKeyChecking=no ${EC2_USER}@${EC2_HOST} "sudo systemctl restart nginx"
                    '''
                }
            }
        }
    }

    post {
        success {
            echo 'Deployment successful!'
        }
        failure {
            echo 'Deployment failed. Check logs for details.'
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation of Key Stages

  1. Clone Repository:
    Pulls the latest code from the repository.

  2. Install Dependencies:
    Installs required packages for the application (e.g., npm install for Node.js).

  3. Build Application:
    Executes the build process, generating the application artifacts.

  4. Run Tests:
    Runs automated tests to validate the application.

  5. Deploy to AWS EC2:

    • Uses scp to transfer the build artifacts to the EC2 instance.
    • Restarts the web server (e.g., Nginx) to reflect the changes.

Optional Enhancements

1. Using AWS CLI for Deployment

Instead of manual SSH-based deployment, automate using the AWS CLI:

stage('Deploy to AWS EC2') {
    steps {
        sh '''
        aws s3 cp ./build s3://your-app-bucket --recursive
        aws deploy create-deployment \
            --application-name your-app-name \
            --deployment-group-name your-deployment-group \
            --s3-location bucket=your-app-bucket,key=build.zip,bundleType=zip
        '''
    }
}
Enter fullscreen mode Exit fullscreen mode

2. Manual Approval for Production Deployment

Add a manual approval step before deploying to production:

stage('Deploy to Production') {
    steps {
        input message: 'Deploy to production?'
        sh './deploy-production.sh'
    }
}
Enter fullscreen mode Exit fullscreen mode

3. Artifact Archiving

Store build artifacts for future use:

post {
    success {
        archiveArtifacts artifacts: '**/build/*', fingerprint: true
    }
}
Enter fullscreen mode Exit fullscreen mode

4. Docker Integration

Containerize the application using Docker and push the image to a container registry:

stage('Build Docker Image') {
    steps {
        sh 'docker build -t sample-app:latest .'
        sh 'docker push sample-app:latest'
    }
}
Enter fullscreen mode Exit fullscreen mode

Testing and Validation

  1. Trigger the Pipeline:
    Commit changes to the repository or manually trigger the pipeline from the Jenkins dashboard.

  2. Verify Deployment:

    • Access the application using the EC2 instance's public IP or domain.
    • Check server logs for troubleshooting (e.g., /var/log/nginx/).
  3. Monitor Performance:
    Use monitoring tools like AWS CloudWatch to observe application performance.


Outcome

By the end of this exercise, you will have:

  • A fully functional CI/CD pipeline.
  • Automated deployment to a cloud environment (AWS EC2).
  • Improved development efficiency with reliable, consistent deployments.

Tasks

  1. Set Up Jenkins: Install Jenkins and configure required plugins.
  2. Prepare the Application: Ensure the repository contains build, test, and deployment scripts.
  3. Deploy to AWS: Automate deployment to AWS EC2 using the provided pipeline script.
  4. Enhance the Pipeline: Add optional features such as artifact archiving, Dockerization, or manual approval.
  5. Monitor the Application: Validate the deployment and monitor performance.

With these steps, you’ll achieve a robust CI/CD pipeline capable of automating deployments to the cloud.

Happy Learning !!!

Top comments (0)