在GitLab runner上配置缓存-composer只安装一次依赖项



我的gitlab缓存配置有问题。

我使用GitLab Community Edition 11.7.7,在我们的文档中:https://docs.gitlab.com/ee/ci/caching/#caching-php依赖

所以,我的配置:

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只安装一次所有依赖项,如何实现?

您的.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

最新更新