Continuous Integration (CI) and Continuous Deployment (CD) are essential practices in modern software development. They help automate the testing and deployment processes, ensuring that your code is always reliable and up-to-date. In this tutorial, we will explore how to set up CI/CD pipelines for Node.js applications using popular tools like GitHub Actions and CircleCI.
Continuous Integration involves merging all developers' working copies to a shared mainline several times a day. Each integration is verified by an automated build and tests. This helps detect integration errors as quickly as possible, reducing the length of time between integration and deployment.
Continuous Deployment extends CI by automatically deploying every change that passes the test phase into a production environment. This ensures that your application is always ready to be released at any time.
We will set up two different CI/CD pipelines: one using GitHub Actions and another using CircleCI.
GitHub Actions is a powerful automation tool integrated directly into GitHub repositories. It allows you to automate workflows, including building, testing, and deploying your Node.js application.
Create a new file named .github/workflows/nodejs.yml in the root of your repository:
1.github/workflows/nodejs.yml2name: Node.js CI/CD34on:5push:6branches: [ main ]7pull_request:8branches: [ main ]910jobs:11build:12runs-on: ubuntu-latest1314strategy:15matrix:16node-version: [14.x, 16.x]1718steps:19- uses: actions/checkout@v220- name: Use Node.js ${{ matrix.node-version }}21uses: actions/setup-node@v222with:23node-version: ${{ matrix.node-version }}24- run: npm install25- run: npm test2627deploy:28needs: build29runs-on: ubuntu-latest30if: github.ref == 'refs/heads/main'31steps:32- uses: actions/checkout@v233- name: Use Node.js ${{ matrix.node-version }}34uses: actions/setup-node@v235with:36node-version: ${{ matrix.node-version }}37- run: npm install38- run: npm run build39- name: Deploy to Heroku40uses: akhileshns/heroku-deploy@v3.12.1241with:42heroku_api_key: ${{ secrets.HEROKU_API_KEY }}43heroku_app_name: "your-heroku-app-name"44heroku_email: "your-email@example.com"`45}
In the GitHub repository settings, go to "Secrets and variables" > "Actions" and add your Heroku API key as HEROKU_API_KEY.
CircleCI is another popular tool for automating CI/CD pipelines. It provides a flexible configuration file that allows you to define complex workflows.
Create a new file named .circleci/config.yml in the root of your repository:
1.circleci/config.yml2version: 2.134jobs:5build:6docker:7- image: cimg/node:14.17.08steps:9- checkout10- run: npm install11- run: npm test1213deploy:14docker:15- image: cimg/node:14.17.016steps:17- checkout18- run: npm install19- run: npm run build20- run: |21git remote add heroku https://git.heroku.com/your-heroku-app-name.git22git push heroku main2324workflows:25version: 2.126build-and-deploy:27jobs:28- build29- deploy:30requires:31- build32filters:33branches:34only: main
In the CircleCI dashboard, go to your project settings and add your Heroku API key as an environment variable.
Now that you have set up CI/CD pipelines for your Node.js application, it's time to focus on version control. In the next section, we will explore how to use Git effectively to manage changes in your codebase.
Happy coding!