缓存与制品 #

一、Cache与Artifacts对比 #

text
┌─────────────────────────────────────────────────────────────┐
│                  Cache vs Artifacts                          │
├─────────────────────────────────────────────────────────────┤
│                                                              │
│  Cache (缓存)                 Artifacts (制品)               │
│  ┌─────────────────┐          ┌─────────────────┐          │
│  │ • 依赖包        │          │ • 构建产物      │          │
│  │ • 临时文件      │          │ • 测试报告      │          │
│  │ • 编译中间件    │          │ • 部署包        │          │
│  │                 │          │                 │          │
│  │ • 加速构建      │          │ • 传递给下游Job │          │
│  │ • 可丢弃        │          │ • 可下载保存    │          │
│  │ • Runner级别    │          │ • Pipeline级别  │          │
│  └─────────────────┘          └─────────────────┘          │
│                                                              │
└─────────────────────────────────────────────────────────────┘
特性 Cache Artifacts
用途 加速构建 传递产物
存储位置 Runner GitLab Server
有效期 可配置 可配置过期时间
传递方式 同一Runner Pipeline内Job传递
下载 不支持 支持手动下载

二、Cache(缓存) #

基本用法 #

yaml
cache:
  paths:
    - node_modules/
    - .npm/

install:
  script:
    - npm ci

全局缓存 #

yaml
cache:
  paths:
    - node_modules/

job1:
  script: npm test

job2:
  script: npm run build

Job级别缓存 #

yaml
job_name:
  cache:
    paths:
      - node_modules/
  script:
    - npm ci
    - npm test

缓存Key #

缓存Key用于区分不同的缓存:

yaml
cache:
  key: my-cache-key
  paths:
    - node_modules/

动态缓存Key #

yaml
cache:
  key: ${CI_COMMIT_REF_SLUG}
  paths:
    - node_modules/

多Key缓存 #

yaml
cache:
  key:
    files:
      - package-lock.json
      - yarn.lock
    prefix: ${CI_JOB_NAME}
  paths:
    - node_modules/

缓存策略 #

yaml
install:
  stage: install
  cache:
    key: ${CI_COMMIT_REF_SLUG}
    paths:
      - node_modules/
    policy: push
  script:
    - npm ci

test:
  stage: test
  cache:
    key: ${CI_COMMIT_REF_SLUG}
    paths:
      - node_modules/
    policy: pull
  script:
    - npm test
策略 说明
pull-push 拉取并更新缓存(默认)
pull 只拉取缓存,不更新
push 只更新缓存,不拉取

缓存失效 #

yaml
cache:
  key: ${CI_COMMIT_REF_SLUG}-${CI_JOB_NAME}
  paths:
    - node_modules/

常见缓存场景 #

Node.js项目 #

yaml
cache:
  key:
    files:
      - package-lock.json
  paths:
    - node_modules/

install:
  script:
    - npm ci

Python项目 #

yaml
cache:
  key:
    files:
      - requirements.txt
  paths:
    - venv/
    - .cache/pip/

install:
  script:
    - pip install -r requirements.txt

Java项目 #

yaml
cache:
  key: ${CI_JOB_NAME}
  paths:
    - .m2/repository/

build:
  script:
    - mvn package

Go项目 #

yaml
cache:
  key: ${CI_JOB_NAME}
  paths:
    - .cache/go-build/
    - go.sum

build:
  script:
    - go build ./...

三、Artifacts(制品) #

基本用法 #

yaml
build:
  stage: build
  script:
    - npm run build
  artifacts:
    paths:
      - dist/

制品过期时间 #

yaml
build:
  artifacts:
    paths:
      - dist/
    expire_in: 1 week

过期时间格式:

  • 1 hour
  • 1 day
  • 1 week
  • 1 month
  • never(永不过期)

制品名称 #

yaml
build:
  artifacts:
    name: "$CI_JOB_NAME-$CI_COMMIT_REF_NAME"
    paths:
      - dist/

条件保存 #

yaml
build:
  artifacts:
    paths:
      - dist/
    when: always
说明
on_success Job成功时保存(默认)
on_failure Job失败时保存
always 总是保存

排除文件 #

yaml
build:
  artifacts:
    paths:
      - dist/
    exclude:
      - dist/*.log
      - dist/tmp/

传递给下游Job #

yaml
build:
  stage: build
  script:
    - npm run build
  artifacts:
    paths:
      - dist/

test:
  stage: test
  dependencies:
    - build
  script:
    - ls dist/
    - npm test

限制依赖 #

yaml
build_a:
  stage: build
  script: echo "Build A"
  artifacts:
    paths:
      - dist_a/

build_b:
  stage: build
  script: echo "Build B"
  artifacts:
    paths:
      - dist_b/

test:
  stage: test
  dependencies:
    - build_a
  script:
    - ls dist_a/

不传递artifacts #

yaml
test:
  stage: test
  dependencies: []
  script:
    - npm test

四、Artifacts类型 #

1. 普通文件 #

yaml
build:
  artifacts:
    paths:
      - dist/
      - build/

2. 测试报告 #

yaml
test:
  artifacts:
    when: always
    paths:
      - test-results/
    reports:
      junit: test-results/*.xml

3. 代码覆盖率报告 #

yaml
test:
  artifacts:
    reports:
      coverage_report:
        coverage_format: cobertura
        path: coverage/cobertura-coverage.xml

4. 代码质量报告 #

yaml
code_quality:
  artifacts:
    reports:
      codequality: gl-code-quality-report.json

5. 安全报告 #

yaml
sast:
  artifacts:
    reports:
      sast: gl-sast-report.json

6. 依赖扫描报告 #

yaml
dependency_scanning:
  artifacts:
    reports:
      dependency_scanning: gl-dependency-scanning-report.json

五、Cache与Artifacts结合 #

典型工作流 #

yaml
stages:
  - install
  - build
  - test
  - deploy

variables:
  CACHE_KEY: ${CI_COMMIT_REF_SLUG}

install:
  stage: install
  cache:
    key: ${CACHE_KEY}
    paths:
      - node_modules/
    policy: push
  script:
    - npm ci

build:
  stage: build
  cache:
    key: ${CACHE_KEY}
    paths:
      - node_modules/
    policy: pull
  script:
    - npm run build
  artifacts:
    paths:
      - dist/

test:
  stage: test
  cache:
    key: ${CACHE_KEY}
    paths:
      - node_modules/
    policy: pull
  dependencies:
    - build
  script:
    - npm test
  artifacts:
    when: always
    reports:
      junit: test-results/*.xml

deploy:
  stage: deploy
  dependencies:
    - build
  script:
    - cp -r dist/* /var/www/html/

六、最佳实践 #

1. 合理设置缓存Key #

yaml
cache:
  key:
    files:
      - package-lock.json
    prefix: ${CI_JOB_NAME}
  paths:
    - node_modules/

2. 使用缓存策略优化 #

yaml
install:
  cache:
    policy: push
  script:
    - npm ci

test:
  cache:
    policy: pull
  script:
    - npm test

3. 限制artifacts大小 #

yaml
build:
  artifacts:
    paths:
      - dist/
    exclude:
      - dist/*.map
      - dist/*.log

4. 使用needs传递artifacts #

yaml
build:
  artifacts:
    paths:
      - dist/

test:
  needs:
    - job: build
      artifacts: true
  script:
    - ls dist/

5. 分离缓存和制品 #

yaml
cache:
  paths:
    - node_modules/

build:
  artifacts:
    paths:
      - dist/

七、常见问题 #

1. 缓存不生效 #

检查:

  • Runner是否配置了缓存存储
  • 缓存Key是否正确
  • 路径是否正确

2. Artifacts太大 #

解决方案:

  • 使用exclude排除不需要的文件
  • 压缩文件
  • 使用外部存储

3. Artifacts未传递 #

检查:

  • dependencies配置
  • needs配置
  • artifacts路径

八、完整示例 #

Node.js项目 #

yaml
stages:
  - install
  - lint
  - build
  - test
  - deploy

variables:
  NODE_VERSION: "18"
  CACHE_KEY: node-${NODE_VERSION}-${CI_COMMIT_REF_SLUG}

.node_template:
  image: node:${NODE_VERSION}
  cache:
    key: ${CACHE_KEY}
    paths:
      - node_modules/

install:
  extends: .node_template
  stage: install
  cache:
    key: ${CACHE_KEY}
    paths:
      - node_modules/
    policy: push
  script:
    - npm ci

lint:
  extends: .node_template
  stage: lint
  cache:
    key: ${CACHE_KEY}
    paths:
      - node_modules/
    policy: pull
  script:
    - npm run lint
  allow_failure: true

build:
  extends: .node_template
  stage: build
  cache:
    key: ${CACHE_KEY}
    paths:
      - node_modules/
    policy: pull
  script:
    - npm run build
  artifacts:
    name: "build-${CI_COMMIT_SHORT_SHA}"
    paths:
      - dist/
    expire_in: 1 week

test:
  extends: .node_template
  stage: test
  cache:
    key: ${CACHE_KEY}
    paths:
      - node_modules/
    policy: pull
  dependencies:
    - build
  script:
    - npm test
  artifacts:
    when: always
    reports:
      junit: test-results/*.xml
      coverage_report:
        coverage_format: cobertura
        path: coverage/cobertura-coverage.xml

deploy:
  stage: deploy
  image: alpine:latest
  dependencies:
    - build
  script:
    - echo "Deploying..."
  only:
    - main

下一步 #

现在你已经掌握了缓存与制品的使用,接下来让我们学习 条件与规则

最后更新:2026-03-28