Github action for lint
GitHub Actions is a continuous integration and continuous delivery (CI/CD) platform that allows you to automate your build, test, and deployment pipeline. You can create workflows that build and test every pull request to your repository or deploy merged pull requests to production.
Some terminologies to be familiar with are mentioned below:
A workflow is a configurable automated process that will run one or more jobs. Workflows are defined by a YAML file checked in the .github/workflows directory in a repository.
An event is a specific activity in a repository that triggers a workflow run. For example, activity can originate from GitHub when someone creates a pull request, opens an issue, or pushes a commit to a repository.
An action is a custom application for the GitHub Actions platform that performs a complex but frequently repeated task.
Prerequisites:
ESlint or any other linter should be configured in the project
Get Kanwal Shariq’s stories in your inbox
Join Medium for free to get updates from this writer.
Linting is the automated checking of your source code for programmatic and stylistic errors. To trigger the checking of code against eslint rules on the project whenever a pull request is created, we need to add the following code in the YAML file in .github/workflows/eslint.yml:
name: ESLint check
on:
pull_request:
types: [opened, synchronize]
branches:
- branchname
jobs:
eslint:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
ref: ${{ github.event.pull_request.head.sha }}
- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: '16.14.2'
- name: Restore cached dependencies
id: cache-restore
uses: actions/cache@v3
with:
path: ./src/node_modules
key: npm-${{ hashFiles('./src/package-lock.json') }}
- name: Install dependencies
if: steps.cache-restore.outputs.cache-hit != 'true'
run: |
cd src
npm ci - ignore-scripts
- name: Lint
run: |
cd src
npm run lintWe have defined the workflow name as ESLint check. Every time there is a pull request against the mentioned branch, This GitHub action will:
- Check out the committed code due to ‘ref’
- Set up node with v.16.14.2 (or any other version compatable with your project)
- Restore the dependencies from cache with a key that uses package-lock.json hash file as name. This is done to skip installing dependencies at every run and only install it when there is a change in package-lock file.
- If the cache did not exist or package.lock was updated, it will install dependencies using npm ci.
- Run ‘npm run lint’ on the entire project to report any linting errors or warnings.
Details:
- types: [opened, synchronize]
‘types’ parameter specifies the types of pull request on which the pipeline should be triggered. Option ‘opened’ will trigger the pipeline when the new pull request is created. Option ‘synchronize’ will trigger pipeline when new commit is added to the pull request
- ref: ${{ github.event.pull_request.head.sha }}
By using ‘github.event.pull_request.head.sha’, GitHub will checkout to the most recent commit on the head branch of the pull request.
- key: npm-${{ hashFiles(‘./src/package-lock.json’) }}
This creates a cache key. ‘hashFiles’ generates SHA256 hash of ‘package-lock.json ‘ file in the repository. We create this key to ensure that if the package-lock.json file is changed, a new cache will be created.
- if: steps.cache-restore.outputs.cache-hit != ‘true’
This IF condition checks if the step with id ‘cache-restore’ was successful in finding the cache or not. If successful, it will skip npm ci step otherwise execute it.
Hope this helps!









