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:
- Faster and more reliable deployments.
- Automated testing and error detection.
- 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:
-
Jenkins Setup:
- Jenkins must be installed and running (locally or on a server).
- Required plugins:
- Pipeline
- Git
- SSH Agent
-
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.
-
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.
-
- The repository should include build, test, and deployment scripts:
-
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
- Open the Jenkins dashboard.
- Click on New Item, enter the job name, and select Pipeline.
- 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.'
}
}
}
Explanation of Key Stages
Clone Repository:
Pulls the latest code from the repository.Install Dependencies:
Installs required packages for the application (e.g.,npm install
for Node.js).Build Application:
Executes the build process, generating the application artifacts.Run Tests:
Runs automated tests to validate the application.-
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.
- Uses
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
'''
}
}
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'
}
}
3. Artifact Archiving
Store build artifacts for future use:
post {
success {
archiveArtifacts artifacts: '**/build/*', fingerprint: true
}
}
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'
}
}
Testing and Validation
Trigger the Pipeline:
Commit changes to the repository or manually trigger the pipeline from the Jenkins dashboard.-
Verify Deployment:
- Access the application using the EC2 instance's public IP or domain.
- Check server logs for troubleshooting (e.g.,
/var/log/nginx/
).
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
- Set Up Jenkins: Install Jenkins and configure required plugins.
- Prepare the Application: Ensure the repository contains build, test, and deployment scripts.
- Deploy to AWS: Automate deployment to AWS EC2 using the provided pipeline script.
- Enhance the Pipeline: Add optional features such as artifact archiving, Dockerization, or manual approval.
- 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)