Share your composer cache between jobs and pipelines in gitlab
Running a Gitlab CI pipeline can take quite some time depending on the amount of tests or tasks that are being executed. Luckily we can cut some time off the composer install step which an easy hack.
This will change your composer cache directory and allow them to be cached between jobs and pipelines allowing composer to use the local cache in stead of having to download packages from packagist and github every time.
Observe the following (shortened) gitlab.ci file:
cache: paths: - composer-cache/ phpunit: stage: test image: php:7.1 script: - curl -sS https://getcomposer.org/installer | php - COMPOSER_CACHE_DIR=composer-cache php composer.phar install --no-progress --no-suggest - vendor/bin/phpunit
The cache part will cache a directory named
composer-cache using the default gitlab cache logic thus sharing all files in this folder between jobs and pipelines.
The other adjustment is to change the composer cache directory forcing composer to use this directory in stead of the default one. Luckly we can do that with a simple environment variable in the composer call:
COMPOSER_CACHE_DIR=composer-cache php composer.phar install --no-progress --no-suggest
In this way, composer won't have to look online everytime a job is executed.