v(i)b(e)c(o)d(e)4u.com
Назад на форум vbcd4u.com
🔥 Нейросети2026-11-17T10:05:00.000Z▲ 0 upvotes

GitHub Actions for Next.js: cache, lint, typecheck, tests and protection main

@murdasoftРедакция vbcd4u · MurdaSoft
Открыть все обсуждения →

Goal

No commit gets into main unless the linter, type checking, and tests pass. Checks go quickly due to the cache and do not waste minutes on outdated push notifications. Below is the workflow and repository settings that turn it from a recommendation into a mandatory requirement.

Workflow

# .github/workflows/ci.yml
name: CI

on:
  pull_request:
  push:
    branches: [main]

concurrency:
  group: ci-${{ github.ref }}
  cancel-in-progress: true

permissions:
  contents: read

jobs:
  quality:
    name: lint-typecheck-test
    runs-on: ubuntu-latest
    timeout-minutes: 15
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version-file: .nvmrc
          cache: npm

      - run: npm ci

      - name: Lint
        run: npm run lint

      - name: Typecheck
        run: npx tsc --noEmit

      - name: Unit tests
        run: npm test -- --run

  build:
    name: build
    runs-on: ubuntu-latest
    needs: quality
    timeout-minutes: 20
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version-file: .nvmrc
          cache: npm
      - run: npm ci
      - uses: actions/cache@v4
        with:
          path: .next/cache
          key: nextjs-${{ runner.os }}-${{ hashFiles('package-lock.json') }}-${{ hashFiles('**/*.ts', '**/*.tsx') }}
          restore-keys: |
            nextjs-${{ runner.os }}-${{ hashFiles('package-lock.json') }}-
      - run: npm run build

What's important here

`concurrency` with `cancel-in-progress`. Three pushes in a row into one branch trigger three runs, only the last one is needed. Group by github.ref reverts previous ones for the same branch or PR, but does not affect others.

`permissions: contents: read`. By default, GITHUB_TOKEN can gain write permissions. They are not needed for checks, and if a dependency that runs in CI is compromised, the least-privileged token limits the damage.

npm cache. setup-node with cache: npm caches the global package cache using the lock file hash. npm ci still installs node_modules from scratch, but without downloading from the network. You should not cache node_modules itself: if you change the Node version, the native modules will become incompatible.

Cache `.next/cache`. Next.js reuses the results of the previous build. The key includes the source hash, and restore-keys allows you to take the latest cache with the same lock file if there is no exact match. Without this cache, next build in CI displays a warning about its absence.

`.nvmrc`. One version of Node locally and in CI. Version discrepancies are a common cause of “works for me.”

Scripts in package.json

{
  "scripts": {
    "lint": "eslint .",
    "typecheck": "tsc --noEmit",
    "test": "vitest",
    "build": "next build"
  }
}

The next lint command has been deprecated in new versions of Next.js, so the linter is called directly. next build itself checks types, but the separate step tsc --noEmit in the first task fails earlier and does not waste time on the build.

Protect main

Workflow without repository rules is just an indicator. Set up ruleset (Settings → Rules → Rulesets) or classic branch protection:

  1. 1.Require a pull request before merging - direct pushes are prohibited in main.
  2. 2.Require status checks to pass - add checks lint-typecheck-test and build. The names are taken from the name field of the task; if you rename the task, the rule will wait for a check that will never come again, and the PR will get stuck.
  3. 3.Require branches to be up to date - PR is checked with the last state main, and not with the one from which it was once taken.
  4. 4.Block force pushes and prohibiting branch deletion.
  5. 5.Don’t add yourself to bypass rules “just in case”: bypass is used precisely on the evening when tests fail.

Workflow security

  • The pull_request event for PRs from forks is triggered without access to secrets - this is correct. Don't change it to pull_request_target for the sake of secrets: there the workflow is executed in the context of the base branch with rights, and checkout of code from PR gives other people's code access to them.
  • Attach third-party actions by the full SHA of the commit, and not by the tag: the tag can be outweighed. The official actions/* for the major version is a reasonable compromise.
  • Keep secrets for deployment in Environments with mandatory confirmation, and not in the general secrets of the repository.

Diagnostics

  • PR does not merge, the check is “Expected - Waiting for status”. The name of the required check does not match the name of the task, or the workflow did not start due to the paths filter. If the workflow with the path filter has not started, the mandatory check will remain pending.
  • The cache is not restored. Caches are isolated by branches: a PR sees the cache of its branch and the base one, but not of neighboring PRs. The first run in a new branch is always cold.
  • Tests hang until timeout. Vitest in observation mode waits for changes. In CI, the CI=true variable is set automatically and Vitest takes this into account, but an explicit --run is more reliable.

When is it enough and when is it not

For a team of a couple of people, this set covers the basics. E2E tests on Playwright are added as a separate task with needs: build when critical user scenarios appear. If CI takes more than ten minutes, split the tests into shards through the matrix rather than disabling them.

Обсуждение (0)

Ещё на vbcd4u.com