You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过缓存加速GitLab CI配置?Laravel项目CI耗时优化求助

Optimizing GitLab CI Pipeline Speed for Laravel (Current Runtime ~12min 20s)

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_KEY directly to .env.testing (test environments don’t need unique keys). This lets you delete the php artisan key:generate line.
  • Cache your config: Replace php artisan config:clear with php artisan config:cache to compile your config into a single file, then add the cache directory to your paths:
    cache:
      paths:
        - vendor/
        - ~/.composer/cache/
        - bootstrap/cache/
    
    This way, Laravel doesn’t re-parse all config files on every run.
  • Skip .env copying (if possible): If .env.testing doesn’t contain sensitive data, commit it to your repo and remove the cp .env.testing .env step 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:08:01