Continuous Integration for Node.js project
Tags: webdev, javascript, tutorial, github
Published: 2022-11-18 21:38:11

##Hello everyone! 🙋🏻‍♀️

Today, I wanted to share how to implement continuous integration in a few easy steps using [Github Actions](https://github.com/features/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](https://docs.github.com/en/actions/using-workflows/using-starter-workflows) 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](https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs#running-on-a-different-operating-system), as it is what my tool is using. 

This will create a configuration [yaml file](https://github.com/gulyapulya/SSGulnur/commit/0972d7b2a47f42d7c2a258cd54e90dc5b0766c85) 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:
```yaml
# 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
```

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](https://github.com/mnosov622/simple-ssg1/pull/10), 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.