name: Lint and Test
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
lint:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [20.x]
steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm run lint
To implement a GitHub Actions workflow where linting is automatically run when a pull request is created, and the pull request is automatically closed if the linting fails
GitHub CLI needs a token and this token needs a permission to act on pull requests. I have modified the workflow by configuring default GITHUB_TOKEN with required permissions at the job level - to pull a repo and to write to PRs. I have also configured GH_TOKEN for GitHb CLI.
See modified workflow with these changes:
name: Lint and Test
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
lint:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
strategy:
matrix:
node-version: [20.x]
steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm run lint
- name: Close PR if Lint Fails
if: failure()
run: gh pr close ${{ github.event.pull_request.number }}
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}