Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to speed up Gitlab CI configuration with caching?

This is the first time I use gitlab CI, so if there are some observations regarding the configuration it would be great to hear them

I have this configuration of CI pipeline for a laravel project in gitalb, in it, I just run the tests suits with PHPUnit:

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

when the test is triggered it took a duration of 12 minutes 20 seconds is there a way to use caching to speed up the job?

like image 529
joe_inz Avatar asked Sep 12 '25 10:09

joe_inz


1 Answers

Build your own docker image

In GitLab CI, you can define cache: to temporary store project dependencies. In your ci.yml, you already did. The majority of the running time of your project pipeline was wasted on apt-get install. Since to cache dependencies install from apt-get install will be complicated, build your own docker image that included dependencies from apt-get for testing will be better.

Steps

  1. write a dockerfile
  2. build, push your docker image to somewhere
  3. replace image: php:7.2 in ci.yml
  4. commit, push your change and run pipeline in GitLab

Dockerfile

FROM php:7.2

RUN set -eux; \
    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

Build, Push image to somewhere

docker build --tag image-name:tag-name --file Dockerfile .
docker tag image-name:tag-name registry.example.com/organization/image-name:tag-name
# I assume you've already authenticated by your registry
docker push registry.example.com/organization/image-name:tag-name

update .gitlab-ci.yml

image: registry.example.com/organization/image-name:tag-name

cache:
  paths:
    - vendor/
    - node_modules/

before_script:
  - 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

Last step

And now, commit the changes and push to your GitLab server.

like image 96
Yz. Avatar answered Sep 14 '25 03:09

Yz.