DEV Community

Gulnur Baimukhambetova
Gulnur Baimukhambetova

Posted on

Continuous Integration for Node.js project

Hello everyone! 🙋🏻‍♀️

Today, I wanted to share how to implement continuous integration in a few easy steps using Github Actions.

In the previous post, I was telling how I added the tests using Jest framework to my SSGulnur tool. This will be a continuation, as we will use these tests as a step in Github Actions workflow.

Steps 🙌

Basically, I followed this little step-by-step guide by Github and made a few configurations to match my workflows needs. Below, I will explain it in more detail.

I navigated to my GitHub repository, clicked "Actions" tab and chose to create a new workflow, where I was presented with different kinds of starter workflows. I chose the one for Node.js, as it is what my tool is using.

This will create a configuration yaml file in your .github/workflows folder which will explain Github what and when to do to your repo changes.

I changed a few lines to match my scripts for linter and tests and this is what I ended up with:

# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs

name: Node.js CI

#will work on every push and pull request to the main branch
on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

jobs:
  build:

    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [14.x, 16.x, 18.x]
        # See supported Node.js release schedule at https://nodejs.org/en/about/releases/

    steps:
    - uses: actions/checkout@v3
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v3
      with:
        node-version: ${{ matrix.node-version }}
        cache: 'npm'
    - run: npm ci #runs clean installation
    - run: npm run lint #linter  
    - run: npm test #run tests
Enter fullscreen mode Exit fullscreen mode

I then committed this to my repo and checked the "Actions" tab for the successful run. I also made an intentional mistake to double check that my workflow will fail if something is wrong, and changed it back.

Unrelated but also experience 😁

Today, I also reviewed and added a few tests to another project, which was a similar tool of my friend. I was a little happy when I realized that I have structured my work in an easy way for testing such as I have lots of isolated small functions. Meanwhile, my friend had to copy his main function and rewrite it to be able to test its logic. It was a little confusing at first to figure out how everything works and to find something testable, but because he was also using Node.js and Jest, it was manageable.

Also, while reviewing each other's pull requests, we learned a valuable lesson on how to make reviews and request changes, which was a very fun experience.

Conclusion 💡

Apparently, CIs can be very easy if you already have your scripts set up and can use one of the starter workflows.

Top comments (0)