Adding Continuous Integration: GitHub Actions Workflow
Tags: opensource, javascript, github, beginners
Published: 2021-11-20 03:14:43

During the Hactoberfest, I had a problem with GitHub Actions Workflow. This week I learned about it, so this is my introduction.

### :white_check_mark:Why do I need CI (Continuous Integration)?
Testing is very important because many people are involved in an open source project. I used Jest last week to test my Static Site Generator(SSG), but I might forget testing when I push or merge pull requests. CI is a way to automatically build and run tests (integrate everything into our project) whenever something is pushed to the repository or a new pull request is made. Therefore, adding CI to my project is very useful for managing and making it work all the time.

### :white_check_mark:How did I implement CI?
I used GitHub Action Workflow for it. You can use actions to automate your workflow in response to events in your GitHub repository. I chose to build Node.js from the Actions tab in my SSG repository. The following `yaml` file (config file) was automatically generated.
```
name: Node.js CI
on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]
jobs:
  build:
    runs-on: ubuntu-latest
  strategy:
    matrix:
      node-version: [10.x, 12.x, 14.x, 16.x]
      # See supported Node.js release schedule at https://nodejs.org/en/about/releases/
steps:
    - uses: actions/checkout@v2
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v2
      with:
        node-version: ${{ matrix.node-version }}
        cache: 'npm'
    - run: npm ci
    - run: npm test
```
- on: specify when this workflow is triggered.
- jobs: run on ubuntu (can add/change mac or Windows' OS)
- matrix: for all of the different versions of node that it's going to run against.
- steps: compile code and test code. I also can add any steps that I have to do or if I need to install other dependencies.

### :white_check_mark:How did I test my CI?
I added a new test code to my project, ran Jest to make sure it passed, then pushed and created a PR. Then, on GitHub, the pull request triggered the CI workflow and it looks like the image below.
![success workflow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ds4v89hijh1nboybjgc7.JPG)
For practice, I went back to the test code again to see how this workflow was interrupted. I rewrote the code so that the test failed, and I pushed to my repo from the same test branch. As I expected, CI failed this time as shown below.
![failed workflow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yv8v9lttloq08jt035ez.JPG)

### :white_check_mark:Adding tests to another Project
I added a test code to my partner Jun's project and sent a pull request.
Jun used a mock file that I couldn't implement last week. It was difficult to think and add some test code to his project, but it was a very good experience and I also would like to use his idea/code for my SSG project. Jun added almost the same `yaml` file as I did, but since he deployed his project with Netlify, Netlify's CI was also run.
![jun's workflow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wq0pby7m1v7w65d2ee19.png)
 
### :white_check_mark:Conclusion
I learned about unit testing and e2e testing last week and found testing was very difficult, but GitHub Actions CI Workflow was very easy to set up by following my professor's lecture.
I will continue to study testing. This time I used Node.js for my JavaScript project, but I hope I can practice in different languages ​​and different construction environments.

### :link:Links
[My SSG repo](https://github.com/MizuhoOkimoto/pajama-ssg) | [My PR to test my CI](https://github.com/MizuhoOkimoto/pajama-ssg/pull/25) | [Jun's SSG repo](https://github.com/juuuuuuun/jun-ssg) | [My PR on Jun's repo](https://github.com/juuuuuuun/jun-ssg/pull/21)