Continuous Integration (CI) is a development practice where developers frequently integrate their code changes into a shared repository, which is then automatically verified and tested. This helps in identifying integration issues early, improving the quality of software and reducing the time to market.
In this tutorial, we will set up a continuous integration pipeline for an Express.js application using GitHub Actions, one of the most popular CI/CD tools available today. We'll cover how to configure the pipeline to automatically run tests whenever changes are pushed to your repository.
Before diving into the setup, let's understand the key components involved in setting up a CI/CD pipeline:
First, create a new repository on GitHub for your Express.js application if you haven't already done so.
$ git init$ git add .$ git commit -m "Initial commit"$ git branch -M main$ git remote add origin https://github.com/yourusername/express-app.git$ git push -u origin main
Create a Workflow File: Navigate to your repository on GitHub, go to the "Actions" tab, and click on "New workflow". Choose "Set up a workflow yourself".
Configure the Workflow:
Create a new file named .github/workflows/ci.yml in your repository with the following content:
1name: Node.js CI23on:4push:5branches: [ main ]6pull_request:7branches: [ main ]89jobs:10build:11runs-on: ubuntu-latest1213strategy:14matrix:15node-version: [14.x, 16.x]1617steps:18- uses: actions/checkout@v219- name: Use Node.js ${{ matrix.node-version }}20uses: actions/setup-node@v221with:22node-version: ${{ matrix.node-version }}23- run: npm install24- run: npm test
This workflow file defines a job named build that runs on the latest Ubuntu environment. It specifies two Node.js versions (14.x and 16.x) to be tested using a matrix strategy. The steps include checking out the code, setting up the specified Node.js version, installing dependencies, and running tests.
Commit the .github/workflows/ci.yml file to your repository:
$ git add .github/workflows/ci.yml$ git commit -m "Add GitHub Actions CI workflow"$ git push origin main
After pushing the changes, go back to the "Actions" tab in your GitHub repository. You should see a new workflow run triggered by the push event. Click on the workflow to view the details and ensure that all steps are executed successfully.
Run npm install added 123 packages from 45 dependencies in 10s Run npm test PASS test/app.test.js ā should return welcome message (10 ms) Test Suites: 1 passed, 1 total Tests: 1 passed, 1 total Snapshots: 0 total Time: 2.34 s Ran all tests.
In this tutorial, we have set up a basic CI/CD pipeline using GitHub Actions for an Express.js application. In the next section, we will explore how to integrate Travis CI with Express.js, another popular CI/CD tool.
By automating your testing process with CI/CD tools like GitHub Actions or Travis CI, you can ensure that your codebase remains stable and reliable as it evolves over time.