GitLab CI:如何在作业之间重用已安装的npm包?

use*_*554 12 node.js gulp gitlab-ci npm-install gitlab-pages

我有一个使用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/
Run Code Online (Sandbox Code Playgroud)

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

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

我认为这cache应该是有用的,但它似乎仍然会重新下载所有内容.

Cli*_*ara 6

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

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
Run Code Online (Sandbox Code Playgroud)

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