In today’s fast-paced development world, Continuous Integration (CI) and Continuous Deployment (CD) are essential practices to ensure code quality, collaboration, and faster releases. For Express.js applications, implementing CI/CD streamlines the development workflow, reduces manual errors, and enables automated testing and deployment.
CI/CD is a software development methodology that combines Continuous Integration (automated testing and merging of code) and Continuous Deployment/Delivery (automated deployment to production).
Implementing CI/CD provides several advantages for Express.js applications:
Before implementing CI/CD, we need a functional Express.js project. Here’s how to set up a simple Express.js app with a basic route.
mkdir express-ci-cd
cd express-ci-cd
npm init -y
npm install express
// app.js
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello, CI/CD with Express.js!');
});
module.exports = app;
// index.js
const app = require('./app');
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
Run the server using node index.js
, and visit http://localhost:3000
to confirm it’s working.
Writing automated tests is crucial for CI. Here, we use Jest and Supertest for testing.
npm install --save-dev jest supertest
// test/app.test.js
const request = require('supertest');
const app = require('../app');
describe('GET /', () => {
it('should return a welcome message', async () => {
const response = await request(app).get('/');
expect(response.status).toBe(200);
expect(response.text).toBe('Hello, CI/CD with Express.js!');
});
});
"scripts": {
"test": "jest"
}
4. Run Tests: Run the tests with the following command:
npm test
Output: The output should display test results, including any passed or failed tests.
Several CI/CD tools can automate our pipelines:
For this example, we’ll focus on GitHub Actions as it’s free for public repositories and simple to set up.
GitHub Actions allows us to set up workflows to automate CI/CD directly in GitHub.
Create a GitHub Repository: Push the project to a GitHub repository.
Add a Workflow File: Create the .github/workflows
folder in the root directory, and inside it, create ci.yml:
# .github/workflows/ci.yml
name: CI
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '14'
- name: Install dependencies
run: npm install
- name: Run tests
run: npm test
This workflow triggers on pushes and pull requests to the main branch, installs dependencies, and runs tests.
In our setup, GitHub Actions automatically runs tests every time code is pushed. It provides feedback on code quality and ensures that code merging only occurs if tests pass.
Continuous Deployment requires deploying tested code to a server. Services like Heroku and AWS offer straightforward integration.
npm install -g heroku
2. Set Up Deployment in GitHub Actions: Add a deployment step to ci.yml:
- name: Deploy to Heroku
env:
HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }}
run: |
git remote add heroku https://git.heroku.com/your-app-name.git
git push heroku main
In GitHub, add the HEROKU_API_KEY in your repository’s Settings > Secrets.
AWS, Azure, and Google Cloud provide CI/CD integration. Here’s an outline for AWS deployment:
This approach offers scalability and is ideal for large-scale applications.
Setting up CI/CD for Express.js applications optimizes the development process by automating testing and deployment, ensuring a smooth workflow from code push to production release. Using CI/CD best practices and tools like GitHub Actions and Heroku can help streamline your workflow, making your Express.js application robust, reliable, and production-ready. Happy Coding!❤️