GitLab CI/CD 流水线配置
GitLab CI/CD 读取仓库根目录的 .gitlab-ci.yml,由 GitLab Runner 执行 job。它支持 stage 顺序、DAG 依赖、规则匹配、缓存与制品,配置能力比 GitHub Actions 更集中。
glab ci lint
glab ci run
glab ci status
glab ci view
glab pipeline list
最小配置:
stages: [build, test, deploy]
default:
image: node:24
cache:
key:
files: [package-lock.json]
paths: [.npm/]
build:
stage: build
script:
- npm ci --cache .npm --prefer-offline
- npm run build
artifacts:
paths: [dist/]
expire_in: 1 week
test:
stage: test
needs: [build]
script:
- npm test
deploy:
stage: deploy
rules:
- if: '$CI_COMMIT_BRANCH == "main"'
when: manual
script:
- ./deploy.sh
rules 是新写法,only/except 是旧写法;同一 job 不要混用,否则行为难预测。needs 会打破 stage 顺序形成 DAG;依赖的 job 不存在或未产生制品会失败。artifacts,不要只依赖 cache。artifacts 默认只在同一 pipeline 内可用;跨 pipeline 需要 dependencies 或外部存储。$CI_* 变量在 MR pipeline 和分支 pipeline 中取值不同;写规则前先打印确认。allow_failure 会让失败 job 不阻断流水线;别用它掩盖真正的错误。gitlab-runner — Runner 管理glab — GitLab CLIgithub-actions — GitHub 的同类方案合上页面,先回答,再展开对照。
rules 和 only/except 吗?rules 是新写法,only/except 是旧写法,混用会让行为难以预测。artifacts。跨 pipeline 还需要 dependencies 或外部存储。