CoReview.io

ESLint Github Action

Why?

ESLint is a static code analizer and can run in your editor as well as in the Github or Bitbucket pipelines.
Running eslint as a GitHub action can be an alternative to using check runs such as Follow Your Tribe, provide extra information and contribute to a cleaner code base, but the setup is a bit more complicated. Here is how you can setup eslint as a github action yourself.

How?

The code will create an Ubuntu instance which runs your command, on GitHub. It operates on your branch when a pullrequest on github is opened or updated, and does a checkout, installs node and everything, and runs eslint.

            name: ESLint Check
            on: 
              pull_request:
                branches: [main]
                types: [opened, synchronize]
            
            jobs:
              eslint:
                runs-on: ubuntu-latest
                steps:
                   - uses: actions/checkout@v1
                   - uses: actions/setup-node@v2
                   - run: npm install
                   - name: create tmp dir
                     run: mkdir ~/tmp/
                   - name: get file list
                     run: git diff ${{ github.event.pull_request.base.sha }} ${{ github.sha }} --name-only --relative '*.js' > ~/tmp/filelist
                   - name: run lint
                     run: npx eslint $(cat ~/tmp/filelist)
        
Now, when you open or update a PR, this action will run and either pass and show a green tick, or it will be red in which case you want to go to the Checks tab, open the ESLint check and find your way to the 'run lint' step with the log and output of eslint.



For more info of github actions visit https://github.com/actions/toolkit