如何通过缓存加速GitLab CI配置?Laravel项目CI耗时优化求助
Question
I'm new to GitLab CI and looking to optimize my Laravel project's pipeline. Right now, my only job runs PHPUnit tests, but the pipeline takes 12 minutes and 20 seconds to complete. Here's my current .gitlab-ci.yml configuration:
image: php:7.2 cache: paths: - vendor/ - node_modules/ before_script: - apt-get update -yqq - apt-get install git libzip-dev libcurl4-gnutls-dev libicu-dev libmcrypt-dev libvpx-dev libjpeg-dev libpng-dev libxpm-dev zlib1g-dev libfreetype6-dev libxml2-dev libexpat1-dev libbz2-dev libgmp3-dev libldap2-dev unixodbc-dev libpq-dev libsqlite3-dev libaspell-dev libsnmp-dev libpcre3-dev libtidy-dev -yqq - docker-php-ext-install mbstring pdo_mysql curl json intl gd xml zip bz2 opcache bcmath - pecl install xdebug - docker-php-ext-enable xdebug - curl -sS https://getcomposer.org/installer | php - php composer.phar check-platform-reqs - php composer.phar install - cp .env.testing .env - php artisan key:generate - php artisan config:clear test: script: - php vendor/bin/phpunit --coverage-text --colors=never
Can I use caching to speed this up? Any other suggestions are welcome!
Answer
Great question—12 minutes is way too slow for a test-only pipeline, and caching (plus a few targeted tweaks) will make a massive difference. Let’s break down the optimizations:
1. Ditch the Generic PHP Image for a Pre-Built Laravel-Friendly One
Your biggest time sink right now is installing all PHP extensions from scratch every run. Instead, use an image that comes pre-configured with all Laravel-required extensions (including xdebug, if needed). This cuts out the entire apt-get and docker-php-ext-install block, which is probably taking 5+ minutes alone.
Replace your image line with something like:
image: laravelphp/php:7.2-fpm
This image includes mbstring, pdo_mysql, gd, zip, bcmath, and even xdebug—no need to install them manually.
2. Supercharge Composer Caching
You’re already caching vendor/, but you’re missing Composer’s global download cache (~/.composer/cache/), which stores package archives so you don’t re-download them every time. Update your cache paths:
cache: paths: - vendor/ - ~/.composer/cache/ # Remove node_modules/ if you don’t run frontend builds in this pipeline (you’re not using it now)
Also, tweak your Composer install command for speed:
- php composer.phar install --prefer-dist --no-interaction --no-suggest
--prefer-dist: Downloads pre-built package archives instead of cloning Git repos (faster)--no-interaction: Skips any prompts--no-suggest: Avoids loading unnecessary package suggestions
3. Cut Redundant Environment Setup Steps
You’re wasting time on steps that don’t need to run every pipeline:
- Hardcode your test APP_KEY: Add a fixed
APP_KEYdirectly to.env.testing(test environments don’t need unique keys). This lets you delete thephp artisan key:generateline. - Cache your config: Replace
php artisan config:clearwithphp artisan config:cacheto compile your config into a single file, then add the cache directory to your paths:
This way, Laravel doesn’t re-parse all config files on every run.cache: paths: - vendor/ - ~/.composer/cache/ - bootstrap/cache/ - Skip
.envcopying (if possible): If.env.testingdoesn’t contain sensitive data, commit it to your repo and remove thecp .env.testing .envstep entirely.
4. Final Optimized Config
Here’s how your .gitlab-ci.yml will look after all tweaks:
image: laravelphp/php:7.2-fpm cache: paths: - vendor/ - ~/.composer/cache/ - bootstrap/cache/ before_script: - curl -sS https://getcomposer.org/installer | php - php composer.phar check-platform-reqs - php composer.phar install --prefer-dist --no-interaction --no-suggest # Remove this line if .env.testing is committed to your repo - cp .env.testing .env - php artisan config:cache test: script: - php vendor/bin/phpunit --coverage-text --colors=never
With these changes, your pipeline runtime should drop to 2-3 minutes (or even less). The pre-built image alone will eliminate the bulk of the wait time!
内容的提问来源于stack exchange,提问作者joe_inz

