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

Playwright E2E in GitHub Actions as a mandatory step before deployment to Vercel

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

Problem with default deployment

The Vercel project connected to GitHub deploys every push in main directly into production. Tests in GitHub Actions run in parallel, and if they fail, the broken version is already serving users. We need order: first tests, then deployment, and deploy exactly the assembly that was tested.

There are two working schemes.

Scheme A. CI builds the application, runs it locally, runs Playwright, and if successful, sends the assembled artifact to Vercel via the CLI.

Scheme B. Vercel creates a preview deployment, CI tests it by URL, and if successful, the same deployment is promoted to production.

Scheme A is easier to configure and does not depend on the availability of preview. Circuit B tests the actual Vercel infrastructure (middleware, edge functions, preview environment variables). Below is the full version of scheme A and the differences for B.

Disable automatic deployment from Git

In vercel.json:

{
  "git": {
    "deploymentEnabled": false
  }
}

Can only be disabled for individual branches: "deploymentEnabled": { "main": false }. Then previews for pull requests will continue to be created automatically.

Playwright configuration

// playwright.config.ts
import { defineConfig, devices } from "@playwright/test";

const baseURL = process.env.E2E_BASE_URL ?? "http://localhost:3000";

export default defineConfig({
  testDir: "./e2e",
  fullyParallel: true,
  forbidOnly: !!process.env.CI,
  retries: process.env.CI ? 2 : 0,
  reporter: process.env.CI ? [["html", { open: "never" }], ["github"]] : "list",
  use: {
    baseURL,
    trace: "on-first-retry",
    extraHTTPHeaders: process.env.VERCEL_AUTOMATION_BYPASS_SECRET
      ? { "x-vercel-protection-bypass": process.env.VERCEL_AUTOMATION_BYPASS_SECRET }
      : undefined,
  },
  projects: [{ name: "chromium", use: { ...devices["Desktop Chrome"] } }],
  webServer: process.env.E2E_BASE_URL
    ? undefined
    : {
        command: "npm run start",
        url: "http://localhost:3000",
        reuseExistingServer: !process.env.CI,
        timeout: 120_000,
      },
});

forbidOnly will prevent an accidentally committed test.only from skipping other tests. trace: "on-first-retry" only saves traces for unstable tests to avoid bloating artifacts.

What to test

E2E before deployment is not the place for full coverage. Take scenarios that cost money or users to break:

// e2e/critical.spec.ts
import { test, expect } from "@playwright/test";

test("главная отдаёт контент и навигацию", async ({ page }) => {
  await page.goto("/");
  await expect(page.getByRole("navigation")).toBeVisible();
  await expect(page.getByRole("heading", { level: 1 })).toBeVisible();
});

test("вход с тестовым пользователем", async ({ page }) => {
  await page.goto("/login");
  await page.getByLabel("Email").fill(process.env.E2E_USER_EMAIL!);
  await page.getByLabel("Пароль").fill(process.env.E2E_USER_PASSWORD!);
  await page.getByRole("button", { name: "Войти" }).click();
  await expect(page).toHaveURL(/\/dashboard/);
});

test("страница статьи открывается при клиентской навигации", async ({ page }) => {
  await page.goto("/forum");
  await page.locator('a[href*="/forum/"]').first().click();
  await expect(page.locator("article")).toBeVisible();
});

The last test catches a class of errors that are not visible when loading the page directly: crashes when going through <Link>. A test user must exist only in the test or preview database.

Workflow for diagram A

# .github/workflows/deploy.yml
name: test-and-deploy
on:
  push:
    branches: [main]

jobs:
  e2e-deploy:
    runs-on: ubuntu-latest
    env:
      VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }}
      VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }}
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: npm
      - run: npm ci
      - run: npm i -g vercel@latest

      - run: vercel pull --yes --environment=production --token=${{ secrets.VERCEL_TOKEN }}
      - run: vercel build --prod --token=${{ secrets.VERCEL_TOKEN }}

      - run: npm run build
      - run: npx playwright install --with-deps chromium
      - run: npx playwright test
        env:
          E2E_USER_EMAIL: ${{ secrets.E2E_USER_EMAIL }}
          E2E_USER_PASSWORD: ${{ secrets.E2E_USER_PASSWORD }}

      - uses: actions/upload-artifact@v4
        if: failure()
        with:
          name: playwright-report
          path: playwright-report/
          retention-days: 7

      - run: vercel deploy --prebuilt --prod --token=${{ secrets.VERCEL_TOKEN }}

vercel pull loads project settings and environment variables, vercel build collects the output into .vercel/output, and deploy --prebuilt sends it without reassembling on the Vercel side. The deployment step is executed only if all previous ones have passed.

Here the application is built twice: vercel build for deployment and npm run build for local launch. If a test assembly goes to the production database, this is dangerous - use separate test environment variables for npm run build and next start.

Differences in scheme B

Instead of the local server, set E2E_BASE_URL with the address of the preview deployment (vercel deploy --token=... prints it to stdout). Preview is usually closed by Deployment Protection; for automation, enable Protection Bypass for Automation in the project settings and pass the secret in the x-vercel-protection-bypass header - the config above already does this. After successful tests, run vercel promote <url> --token=....

How to deal with crashes

  • Download artifact playwright-report and open npx playwright show-report. The trace shows the DOM, network, and console at each step.
  • The test crashes only in CI: most often the culprit is timeouts during a cold start or different time zones. Compare TZ and locale in use.
  • Unstable test is not treated by increasing retries. Look for a wait via waitForTimeout and replace it with a status check via expect.

When it's unnecessary

For a static site without forms and authorization, building and checking links is enough. If tests take more than 10–15 minutes, divide them: a short smoke set blocks deployment, a full set proceeds as scheduled.

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

Ещё на vbcd4u.com