In this case study, we will explore the implementation of CI/CD practices in a web project. This will include setting up a CI/CD pipeline, automating builds and tests, deploying to a staging environment, and finally deploying to production. By the end of this case study, you should have a clear understanding of how to apply CI/CD principles to a real-world web project.
Project Overview
Project Description
We will use a simple web application built with the following stack:
- Frontend: React.js
- Backend: Node.js with Express
- Database: MongoDB
Objectives
- Automate the build process for both frontend and backend.
- Run automated tests to ensure code quality.
- Deploy the application to a staging environment for testing.
- Deploy the application to a production environment.
Step 1: Setting Up the CI Environment
Tools and Technologies
- Version Control: Git and GitHub
- CI Tool: Jenkins
- Deployment Platform: AWS (Amazon Web Services)
Setting Up Jenkins
-
Install Jenkins:
- Follow the official Jenkins installation guide for your operating system: Jenkins Installation Guide
-
Configure Jenkins:
- Install necessary plugins:
- Git Plugin
- NodeJS Plugin
- Docker Plugin (if using Docker for deployment)
- Create a new Jenkins job for the web project.
- Install necessary plugins:
Jenkins Job Configuration
-
Source Code Management:
- Configure the job to pull the code from the GitHub repository.
git url: 'https://github.com/your-repo/web-project.git'
-
Build Triggers:
- Set up the job to trigger on every push to the repository.
triggers { scm('H/5 * * * *') }
-
Build Steps:
- Install Node.js and dependencies.
npm install
- Build the frontend and backend.
npm run build
-
Post-build Actions:
- Archive the build artifacts.
archiveArtifacts 'build/**'
Step 2: Automated Testing
Setting Up Automated Tests
-
Unit Tests:
- Write unit tests for both frontend and backend using Jest.
- Example test for a React component:
import React from 'react'; import { render } from '@testing-library/react'; import App from './App'; test('renders learn react link', () => { const { getByText } = render(<App />); const linkElement = getByText(/learn react/i); expect(linkElement).toBeInTheDocument(); });
-
Integration Tests:
- Write integration tests for API endpoints using Supertest.
- Example test for an Express endpoint:
const request = require('supertest'); const app = require('../app'); describe('GET /api/users', () => { it('should return a list of users', async () => { const res = await request(app).get('/api/users'); expect(res.statusCode).toEqual(200); expect(res.body).toHaveProperty('users'); }); });
Integrating Tests in Jenkins
-
Add Test Steps:
- Run unit and integration tests as part of the build process.
npm test
-
Publish Test Results:
- Use Jenkins plugins to publish test results.
junit 'test-results.xml'
Step 3: Deployment to Staging
Staging Environment Setup
-
Create a Staging Environment:
- Use AWS EC2 instances or Docker containers to set up a staging environment.
-
Deploy to Staging:
- Use Jenkins to automate the deployment process.
sshPublisher(publishers: [ sshPublisherDesc(configName: 'staging-server', transfers: [ sshTransfer(sourceFiles: 'build/**', remoteDirectory: '/var/www/html', removePrefix: 'build') ]) ])
Smoke Testing
- Run Smoke Tests:
- Ensure the application is running correctly in the staging environment.
- Example smoke test:
curl -f http://staging-server/api/health || exit 1
Step 4: Deployment to Production
Production Environment Setup
-
Create a Production Environment:
- Use AWS EC2 instances or Docker containers to set up a production environment.
-
Deploy to Production:
- Use Jenkins to automate the deployment process.
sshPublisher(publishers: [ sshPublisherDesc(configName: 'production-server', transfers: [ sshTransfer(sourceFiles: 'build/**', remoteDirectory: '/var/www/html', removePrefix: 'build') ]) ])
Monitoring and Feedback
-
Set Up Monitoring:
- Use tools like AWS CloudWatch or New Relic to monitor the application.
- Configure alerts for critical metrics.
-
Collect Feedback:
- Gather feedback from users and stakeholders to continuously improve the application.
Summary
In this case study, we covered the end-to-end implementation of CI/CD for a web project. We set up a CI environment using Jenkins, automated the build and test processes, deployed to staging and production environments, and set up monitoring and feedback mechanisms. By following these steps, you can ensure a smooth and efficient CI/CD pipeline for your web projects.
Next Steps
- Apply these principles to your own web projects.
- Explore more advanced CI/CD practices and tools.
- Continue learning and improving your CI/CD pipeline.
CI/CD Course: Continuous Integration and Deployment
Module 1: Introduction to CI/CD
Module 2: Continuous Integration (CI)
- Introduction to Continuous Integration
- Setting Up a CI Environment
- Build Automation
- Automated Testing
- Integration with Version Control
Module 3: Continuous Deployment (CD)
- Introduction to Continuous Deployment
- Deployment Automation
- Deployment Strategies
- Monitoring and Feedback
Module 4: Advanced CI/CD Practices
Module 5: Implementing CI/CD in Real Projects
Module 6: Tools and Technologies
Module 7: Practical Exercises
- Exercise 1: Setting Up a Basic Pipeline
- Exercise 2: Integrating Automated Tests
- Exercise 3: Deployment in a Production Environment
- Exercise 4: Monitoring and Feedback