首页 > 解决方案 > 在 GitLab 运行器上配置缓存 - 仅由 composer 安装依赖项一次

问题描述

我对 gitlab 缓存配置有疑问。

我使用 GitLab 社区版 11.7.7,在文档中我们有: https ://docs.gitlab.com/ee/ci/caching/#caching-php-dependencies

所以,我的配置:

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

stages:
  - code-style
  - analysis

before_script:
  - composer global require hirak/prestissimo
  - composer install --ignore-platform-reqs --no-scripts

php-cs-fixer:
  stage: code-style
  script:
    - vendor/bin/php-cs-fixer fix --dry-run --diff --verbose --config=.php_cs.dist src tests

php-cs-fixer2:
  stage: analysis
  script:
      - vendor/bin/php-cs-fixer fix --dry-run --diff --verbose --config=.php_cs.dist src tests

不幸的是,composer 会在每个作业中重新安装所有依赖项。

我只想通过composer安装所有依赖项,我该如何实现呢?

标签: phpcachingcomposer-phpgitlab

解决方案


您的.gitlab-ci.yml配置类似于我自己在我公司的自托管 Gitlab 上运行的配置,我认为它应该也可以工作,因此我创建了一个示例 repo,它根据您的配置运行管道以重现您的问题。

正如你在这里看到的,我试图通过完成配置来制作一个工作管道,例如,错误的图像,缺少 composer 可执行文件......类似的东西,最终的配置与你的并没有太大的不同。

image: php:7.2-alpine

cache:
  key: $CI_COMMIT_REF_SLUG
  paths:
    - vendor/

stages:
  - code-style
  - analysis

before_script:
  - php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
  - php -r "if (hash_file('sha384', 'composer-setup.php') === 'a5c698ffe4b8e849a443b120cd5ba38043260d5c4023dbf93e1558871f1f07f58274fc6f4c93bcfd858c6bd0775cd8d1') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
  - php composer-setup.php --install-dir=/usr/local/bin --filename=composer
  - export PATH="/usr/local/bin:$PATH"
  - php -r "unlink('composer-setup.php');"
  - composer global require hirak/prestissimo
  - composer install --ignore-platform-reqs --no-scripts

php-cs-fixer:
  stage: code-style
  script:
    - vendor/bin/php-cs-fixer fix --dry-run --diff --verbose --config=.php_cs.dist src tests || true
  allow_failure: true

php-cs-fixer2:
  stage: analysis
  script:
      - vendor/bin/php-cs-fixer fix --dry-run --diff --verbose --config=.php_cs.dist src tests
  allow_failure: true

最后一份工作#97744833有一个线索来回答你的问题。

比较上一个失败作业的最后一部分输出https://gitlab.com/Gasol/php-composer-cache-on-gitlab-ci/-/jobs/358118759#L191

187 Checked all files in 0.006 seconds, 10.000 MB memory used
191 ERROR: Job failed: exit code 8

https://gitlab.com/Gasol/php-composer-cache-on-gitlab-ci/-/jobs/358121571#L190

构建成功时正在创建缓存。

190 Creating cache master...
191 vendor/: found 4860 matching files                 
192 Uploading cache.zip to https://storage.googleapis.com/gitlab-com-runners-cache/project/15472690/master 
193 Created cache
196 Job succeeded

因此,缓存创建后,下一阶段的作业应该可以下载缓存存档了。

https://gitlab.com/Gasol/php-composer-cache-on-gitlab-ci/-/jobs/358121572#L20

18 Checking cache for master...
19 Downloading cache.zip from https://storage.googleapis.com/gitlab-com-runners-cache/project/15472690/master 
20 Successfully extracted cache

https://gitlab.com/Gasol/php-composer-cache-on-gitlab-ci/-/jobs/358121572#L46

提取缓存时无需安装任何内容。这就是你想要的结果。

43 $ composer install --ignore-platform-reqs --no-scripts
44 Loading composer repositories with package information
45 Installing dependencies (including require-dev) from lock file
46 Nothing to install or update
47 Generating autoload files

推荐阅读