GitLab:在构建映像中挂载 /builds 作为 tmpfs

tir*_*hen 5 gitlab tmpfs docker gitlab-ci gitlab-ci-runner

在我们的 GitLab CI 环境中,我们有一个具有大量 RAM 和机械磁盘的构建服务器,运行 npm install 需要很长时间(我添加了缓存,但它仍然需要处理现有的包,因此缓存无法单独解决所有这些问题)。

我想在构建器 docker 映像中挂载 /builds 作为 tmpfs,但我很难弄清楚将此配置放在哪里。我可以在构建器映像本身中执行此操作,还是可以在 .gitlab-ci.yml 中为每个项目执行此操作?

目前我的 gitlab-ci.yml 看起来像这样:

image: docker:latest

services:
  - docker:dind

variables:
  DOCKER_DRIVER: overlay

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

stages:
  - test

test:
  image: docker-builder-javascript
  stage: test
  before_script:
    - npm install
  script:
    - npm test
Run Code Online (Sandbox Code Playgroud)

tir*_*hen 3

我发现可以通过直接在 before_script 部分使用 mount 命令来解决这个问题,尽管这需要您复制源代码,但我设法减少了很多测试时间。

image: docker:latest

services:
  - docker:dind

variables:
  DOCKER_DRIVER: overlay

stages:
  - test

test:
  image: docker-builder-javascript
  stage: test

  before_script:
    # Mount RAM filesystem to speed up build
    - mkdir /rambuild
    - mount -t tmpfs -o size=1G tmpfs /rambuild
    - rsync -r --filter=":- .gitignore" . /rambuild
    - cd /rambuild

    # Print Node.js npm versions
    - node --version
    - npm --version

    # Install dependencies
    - npm ci

  script:
    - npm test
Run Code Online (Sandbox Code Playgroud)

由于我现在使用的是npm ci命令,而不是npm install我不再使用缓存,因为无论如何它都会在每次运行时清除缓存。