GitLab 运行程序没有携带 node_modules 和缓存到下一个作业?

Joe*_*oey 6 npm node-modules gitlab gitlab-ci-runner cypress

我遇到一个问题,我的 e2e 测试在 GitLab 中失败,但正常通过。我将问题范围缩小到了 node_modules,并且 cypress 缓存没有延续到下一个作业。这就是WARN Local package.json exists, but node_modules missing, did you mean to install出现错误的原因,因为 node_modules 不存在。

我认为缓存没有保留的原因是因为它被保存在一个运行器上,但是如果下一个作业在另一个运行器上,则缓存将不存在并且最终会失败。我可以将所有内容都放在一项工作中,但如果我要添加另一项工作进行部署,那么我仍然会遇到同样的问题。

这是我的 gitlab-ci.yml 文件:

image: cypress/base:12.18.4

variables:
  NPM_REGISTRY: https://registry.npmjs.org/
  npm_config_cache: $CI_PROJECT_DIR/.npm
  CYPRESS_CACHE_FOLDER: $CI_PROJECT_DIR/cache/Cypress

stages:
  - build
  - test:unit
  - test:e2e
  - lint

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

build:
  stage: build
  script:
    # set the npm registry if different from the default
    - npm config set registry $NPM_REGISTRY
    - npm i
    - npx cypress verify

test:unit:
  stage: test:unit
  script:
    - npm run test:unit:coverage
  artifacts:
    paths:
      - tests/unit/out/coverage
    when: always
    expire_in: 1 hour

test:e2e:
  stage: test:e2e
  script:
    - npm run test:e2e:headless
  artifacts:
    paths:
      - tests/e2e/out/reports
      - tests/e2e/out/coverage
    when: always
    expire_in: 1 hour

lint:
  stage: lint
  script:
    - npm run lint
Run Code Online (Sandbox Code Playgroud)

这是 GitLab 中的错误日志:

Restoring cache
00:01
Updating CA certificates...
Checking cache for fix-gitlab-pipeline...
No URL provided, cache will not be downloaded from shared cache server. Instead a local version of cache will be extracted. 
Successfully extracted cache
Executing "step_script" stage of the job script
00:01
$ npm run test:e2e:headless
> start-test serve:e2e 8089 'cypress run'
sh: 1: start-test: not found
npm ERR! code ELIFECYCLE
npm ERR! syscall spawn
npm ERR! file sh
npm ERR! errno ENOENT
npm ERR! rds@0.1.0 test:e2e:headless: `start-test serve:e2e 8089 'cypress run'`
npm ERR! spawn ENOENT
npm ERR! 
npm ERR! Failed at the test@0.1.0 test:e2e:headless script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm WARN Local package.json exists, but node_modules missing, did you mean to install?
npm ERR! A complete log of this run can be found in:
npm ERR!     /builds/test/.npm/_logs/2021-01-05T17_16_55_957Z-debug.log
Uploading artifacts for failed job
00:00
Updating CA certificates...
WARNING: ca-certificates.crt does not contain exactly one certificate or CRL: skipping
WARNING: ca-cert-ca.pem does not contain exactly one certificate or CRL: skipping
Uploading artifacts...
WARNING: tests/e2e/out/reports: no matching files  
WARNING: tests/e2e/out/coverage: no matching files 
ERROR: No files to upload                          
ERROR: Job failed: exit code 1
Run Code Online (Sandbox Code Playgroud)

Ada*_*all 2

我在这里回答了类似的问题:https ://stackoverflow.com/a/65798418/2307873

要点是,默认情况下,每个运行程序都将缓存的项目存储在该运行程序的文件系统上,因此其他运行程序将无法访问它。如果您的运行程序可以连接到共享文件系统,您可以配置它的存储路径,或者您可以使用 S3(或 GC 或 Azure 的等效项)或任何实现这些 API 的东西。我们使用 Minio ( https://min.io ) 来实现 S3 API。

gitlab 和 Minio 的文档位于链接的答案中。