【问题标题】:GitLab CI: How can I reuse installed npm packages between jobs?GitLab CI:如何在作业之间重用已安装的 npm 包?
【发布时间】:2017-09-30 18:28:24
【问题描述】:

我有一个使用 Gulp 进行构建的 GitLab Pages 站点。我的 .gitlab-ci.yml 文件与此类似:

image: node:latest

before_script:
  - npm install gulp-cli -g
  - npm install gulp [...and a whole bunch of packages] --save-dev

build:
  stage: build
  script:
  - gulp buildsite
  artifacts:
    paths:
    - public

pages:
  stage: deploy
  script:
  - gulp
  artifacts:
    paths:
    - public

cache:
  paths:
  - node_modules/

buildpages 作业之前,会执行npm install 命令(在每个作业之前执行一次)。由于我有很多包,这通常需要一段时间。

有没有办法在整个构建中只安装一次?

我认为这就是 cache 应该提供的帮助,但它似乎仍然会重新下载所有内容。

【问题讨论】:

  • 缓存系统没有任何保证,如果您需要 100% 的时间在作业之间发送 node_modules,请使用工件 stackoverflow.com/a/43722345/6654146 您还可以为工件添加过期时间,以便它们'不会永远保存在您的服务器上

标签: node.js gulp npm-install gitlab-ci gitlab-pages


【解决方案1】:

尽管 cmets 中的答案基本上是正确的。我认为针对您的案例的具体答案会很好。您可以使用的一种方法是添加第三阶段来承担安装节点模块的负载,此外您还可以缓存它们以加快后续构建:

image: node:latest

stages:
  - prep
  - build
  - deploy  

before_script:
  - npm install gulp-cli -g  

prep:
  stage: prep
  script:
  - npm install gulp [...and a whole bunch of packages] --save-dev
  artifacts:
   paths:
   - node_modules 
  cache:
   paths:
   - node_modules

build:
  stage: build
  script:
  - gulp buildsite
  artifacts:
    paths:
    - public

pages:
  stage: deploy
  script:
  - gulp
  artifacts:
    paths:
    - public

此解决方案将只执行一次安装并将结果缓存以供将来的 ci 管道使用,您还可以在节点模块工件上设置过期时间。

【讨论】:

    【解决方案2】:

    你需要设置cache:untracked: true才能真正缓存Git没有跟踪的文件。

    cache:
      untracked: true
      paths:
          - node_modules/
    

    【讨论】:

      猜你喜欢
      • 2022-08-16
      • 1970-01-01
      • 2016-02-05
      • 1970-01-01
      • 2019-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-05
      相关资源
      最近更新 更多