Generate a GitLab CI Test Pipeline Configuration
Returns a `.gitlab-ci.yml` with stages (build, test:unit, test:integration, test:e2e), parallel matrix for E2E browsers, cache keyed on lockfile, rules section for MR vs main differences, and artifact reports with explicit expiration.
When to use it
- Migrating from Jenkins or GitHub Actions to GitLab CI.
- Standardizing GitLab pipelines across teams or services.
- Adding QA gates to an existing GitLab project.
- Auditing an existing GitLab pipeline for missing best practices.
The prompt
<role>You are a GitLab CI specialist. You use stages, rules (not the deprecated 'only/except'), parallel matrix for browser fanout, and `artifacts:reports` for test result integration.</role> <context>GitLab CI structure: stages run sequentially; jobs within a stage run parallel. `rules:` (preferred over deprecated `only/except`) gates jobs by ref / changes / variables. Caching is keyed via cache:key. Artifacts have `expire_in` plus `reports:` for native GitLab integration (Jest, Cobertura, JUnit, etc.).</context> <task>Generate a `.gitlab-ci.yml` with:1. Stages: build, test:unit, test:integration, test:e2e, notify2. Cache strategy keyed on lockfile hash3. Parallel matrix for E2E browsers (chromium, firefox, webkit)4. Rules: MR vs main pipeline differences (e.g., skip E2E on doc-only changes)5. Artifacts with `expire_in` and `reports:` for JUnit / coverage6. Notification job on failure (Slack via webhook or pipeline notification)7. `needs:` keyword for cross-stage dependencies (faster than purely sequential)</task> <input>Tech stack: {stack}Test frameworks: {frameworks}Notification target: {notification}Special requirements: {special}</input> <constraints>- Use `rules:` not `only/except`.- Cache key MUST include lockfile hash.- Artifacts MUST have `expire_in` (not default).- `reports:` MUST be used for native GitLab test result integration.- E2E matrix uses `parallel:matrix` keyword (modern syntax).- Use `needs:` to break strict stage sequencing where beneficial.</constraints> <output_format>Complete YAML in a code block. Followed by "Notes" section: how to wire SSH keys via `CI_JOB_TOKEN`, dynamic environments, and what's NOT covered.</output_format> <approach>Before writing, decide whether the project benefits from `needs:` (parallel stages) or strict sequential.</approach>## RoleGitLab CI specialist. Stages + rules (not only/except). parallel:matrix for browser fanout. artifacts:reports for native integration. ## ContextStages run sequentially; jobs in same stage run parallel. `rules:` for ref/changes/variables. Cache via cache:key. Artifacts have `expire_in` + `reports:`. ## TaskGenerate `.gitlab-ci.yml` with:1. Stages: build, test:unit, test:integration, test:e2e, notify2. Cache (lockfile-keyed)3. Parallel matrix for E2E (chromium, firefox, webkit)4. Rules: MR vs main, skip E2E on doc-only changes5. Artifacts with expire_in + reports6. Notification on failure7. `needs:` for cross-stage parallelism ## Input- Stack: {stack}- Test frameworks: {frameworks}- Notification: {notification}- Special: {special} ## Constraints- `rules:` not only/except.- Cache key includes lockfile hash.- Artifacts MUST have `expire_in`.- `reports:` for native integration.- `parallel:matrix` for E2E browsers.- `needs:` for cross-stage parallelism. ## Output formatYAML code block + Notes section (CI_JOB_TOKEN, dynamic envs, what's NOT covered). ## ApproachDecide if needs: helps the project (parallel stages benefit) or strict sequencing is enough.Example
Input
Tech stack: Node.js 24 LTS + TypeScript. Single-package application (no monorepo). Test frameworks: Vitest (unit + integration), Playwright (E2E). Notification target: Slack via incoming webhook (Slack URL in `SLACK_WEBHOOK_URL` variable). Special requirements: Skip E2E on doc-only MRs; full pipeline on main; nightly schedule for full regression.
Output
```yaml
# .gitlab-ci.yml
stages:
- build
- test:unit
- test:integration
- test:e2e
- notify
variables:
NODE_VERSION: '24'
npm_config_cache: '$CI_PROJECT_DIR/.npm'
default:
image: node:$NODE_VERSION-alpine
cache:
key:
files:
- package-lock.json
paths:
- .npm/
policy: pull
build:
stage: build
cache:
key:
files:
- package-lock.json
paths:
- .npm/
- node_modules/
policy: pull-push
script:
- npm ci --prefer-offline --no-audit --no-fund
- npm run build
artifacts:
paths:
- dist/
expire_in: 1 day
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
- if: '$CI_COMMIT_BRANCH == "main"'
- if: '$CI_PIPELINE_SOURCE == "schedule"'
lint:
stage: test:unit
needs: [build]
script:
- npm ci --prefer-offline --no-audit --no-fund
- npm run lint
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
changes:
- 'src/**/*'
- 'package.json'
- 'package-lock.json'
- if: '$CI_COMMIT_BRANCH == "main"'
typecheck:
stage: test:unit
needs: [build]
script:
- npm ci --prefer-offline --no-audit --no-fund
- npm run typecheck
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
changes:
- 'src/**/*'
- 'tsconfig.json'
- if: '$CI_COMMIT_BRANCH == "main"'
unit-tests:
stage: test:unit
needs: [build]
script:
- npm ci --prefer-offline --no-audit --no-fund
- npm run test:unit -- --coverage --coverage.reporter=text-summary --coverage.reporter=cobertura --reporter=junit --outputFile=junit.xml
coverage: '/Lines\s*:\s*(\d+(?:\.\d+)?)%/'
artifacts:
when: always
expire_in: 7 days
reports:
junit: junit.xml
coverage_report:
coverage_format: cobertura
path: coverage/cobertura-coverage.xml
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
- if: '$CI_COMMIT_BRANCH == "main"'
integration-tests:
stage: test:integration
needs: [unit-tests]
script:
- npm ci --prefer-offline --no-audit --no-fund
- npm run test:integration -- --reporter=junit --outputFile=junit-integration.xml
artifacts:
when: always
expire_in: 7 days
reports:
junit: junit-integration.xml
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
- if: '$CI_COMMIT_BRANCH == "main"'
e2e-tests:
stage: test:e2e
# The image tag MUST match the @playwright/test version in package-lock.json; browsers are preinstalled in it
image: mcr.microsoft.com/playwright:v1.XX.0-noble
needs: [build]
variables:
PLAYWRIGHT_JUNIT_OUTPUT_NAME: results.xml
parallel:
matrix:
- BROWSER: [chromium, firefox, webkit]
script:
- npm ci --prefer-offline --no-audit --no-fund
- npx playwright test --project=$BROWSER --reporter=junit,html
artifacts:
when: always
expire_in: 7 days
paths:
- playwright-report/
- test-results/
reports:
junit: results.xml
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
changes:
- 'src/**/*'
- 'tests/**/*'
- 'playwright.config.ts'
- 'package.json'
- 'package-lock.json'
- if: '$CI_COMMIT_BRANCH == "main"'
- if: '$CI_PIPELINE_SOURCE == "schedule"'
notify-on-failure:
stage: notify
image: alpine:latest
script:
- apk add --no-cache curl jq
- |
# jq builds the JSON so the CI variables are expanded and safely escaped
PAYLOAD=$(jq -n --arg ref "$CI_COMMIT_REF_NAME" --arg sha "$CI_COMMIT_SHA" --arg url "$CI_PIPELINE_URL" \
'{text: "*QA pipeline failed*\nBranch: \($ref)\nCommit: \($sha)\nPipeline: <\($url)|View>"}')
curl -sS -X POST -H 'Content-Type: application/json' -d "$PAYLOAD" "$SLACK_WEBHOOK_URL"
rules:
- if: '$CI_COMMIT_BRANCH == "main"'
when: on_failure
- if: '$CI_PIPELINE_SOURCE == "schedule"'
when: on_failure
```
## Notes
- **SSH keys / private packages**: use `CI_JOB_TOKEN` for GitLab Container Registry; for external private registries, store the token in masked CI/CD variables.
- **Dynamic environments**: add a `deploy:review` job with `environment: name: review/$CI_COMMIT_REF_SLUG` for review apps per MR.
- **Schedules**: configure GitLab schedules (Settings > CI/CD > Schedules) for nightly full-regression runs. The pipeline already includes `schedule` source as a trigger.
- **NOT covered**:
- Playwright upgrades: bump the image tag in the same MR as `@playwright/test`, or the tests fail with "Executable doesn't exist".
- Visual regression: add a job using Playwright `toHaveScreenshot` + artifact upload.
- GitLab Runner autoscaling: assumes shared runners or pre-configured group runner.
- Mobile testing: add BrowserStack / Sauce Labs integration as a separate job.Common pitfalls
- Model defaults to deprecated `only/except` syntax: force `rules:`.
- Cache key uses static string instead of lockfile-based: cache never invalidates. Use `files:` directive.
- Artifacts get no `expire_in`: accumulates storage cost. Always set.
- JUnit / coverage reports not used: loses native GitLab integration (merge request widgets, test history).
Tips
- Use `needs:` to break strict stage sequencing: significant runtime savings on bigger pipelines.
- Pair with reusable templates via `include:` for org-wide standardization.
- Use protected variables for production secrets (master/protected branches only).
- Pair with `parallel-test-execution-config` for E2E sharding when suite grows.
FAQ
GitLab CI vs GitHub Actions feature parity?
Both cover ~95% of common QA pipeline needs. GitLab leads on built-in container registry, deployments, dependency proxy. GitHub Actions leads on marketplace size and OIDC adoption. Choose based on where your code lives; both can do the job.
How do I scale runners?
Use shared runners initially. Move to group/project runners with autoscaling (Kubernetes executor, AWS spot) when shared queue times hurt. Self-hosted runners are needed only for special hardware or network-restricted access.
Dynamic environments per MR?
Use `environment: name: review/$CI_COMMIT_REF_SLUG` to spawn per-MR deployments. Pair with auto-stop after a few hours to prevent zombie environments.
Migrating from Jenkins?
Approach incrementally: start by running GitLab CI on PRs alongside Jenkins. Move stages over one by one. Keep Jenkins for legacy gates initially, retire when GitLab covers them all.