Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

aknEvrnky's avatar

Gitlab CI testing

Hi folks, I'm using gitlab CI for testing my laravel application. The issue is before_scripts takes too many time. I'll be sharing my testing script. I want to ask that is there any all-in-one image for running tests. If not, how can I optimize my job?

test:
  image: php:8.2-cli
  stage: test
  only:
    - main
  before_script:
    - apt-get update -qq && apt-get install -y -qq sqlite3 libfreetype6-dev libjpeg62-turbo-dev libpng-dev libzip-dev ca-certificates gnupg # install dependencies
    - docker-php-ext-configure gd --with-freetype --with-jpeg && docker-php-ext-install pdo_mysql opcache pcntl gd zip # install php extensions
    - echo "date.timezone = UTC" > /usr/local/etc/php/conf.d/timezone.ini && echo "memory_limit = 512M" > /usr/local/etc/php/conf.d/memory-limit.ini # configure php extensions
    - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer # install composer
    - mkdir -p /etc/apt/keyrings && curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
    - NODE_MAJOR=20 && echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list
    - apt-get update -qq && apt-get install -y -qq nodejs && npm install -g yarn # install nodejs
  script:
    - cp .env.example .env
    - composer install --prefer-dist --no-ansi --no-interaction --no-progress --no-scripts
    - yarn install
    - php artisan key:generate
    - npm run build
    - php artisan test --exclude-group api -p --log-junit phpunit-report.xml # update test command to output JUnit XML
  artifacts:
    when: always
    paths:
      - phpunit-report.xml
    reports:
      junit: phpunit-report.xml
  retry: 2

Thanks..

0 likes
5 replies
martinbean's avatar

@aknevrnky Build your own image using those steps in the before_script section, push it to Docker Hub, and then use that image is your image instead. It will cut your “set-up” times if the pipeline just pulls an already-built image and can just start running your tests in it straight away. I did this years ago when using Bitbucket Pipelines.

1 like
aknEvrnky's avatar

@martinbean Hi Martin. I've tried what you told. But custom image had some performance issues or bugs that I couldn't get. Running time was above 10min and I had to force quit the pipe. I probably broke something during image building.

But thanks to @thijs an appropriate image with some custom ENV config worked very well. Thanks to you all.

martinbean's avatar

@aknEvrnky No. I mean you build a custom Docker image on your machine, and then upload it to Docker Hub so that you can use it as a base image in your GitLab pipeline:

test:
  image: some-vendor-name/your-image-name:tag

Building an image inside the pipeline each and every time you run it isn’t really going to save any time, is it? That’s essentially what you were already doing.

aknEvrnky's avatar

@martinbean actually I did it. I've created an image and pushed it to registry.

FROM php:8.2-cli

# install necessary dependencies and php extensions
RUN apt-get update -qq && apt-get install -y -qq sqlite3 libfreetype6-dev libjpeg62-turbo-dev libpng-dev libzip-dev ca-certificates gnupg

# enable php extensions
RUN docker-php-ext-configure gd --with-freetype --with-jpeg && docker-php-ext-install pdo_mysql opcache pcntl gd zip \
    && echo "date.timezone = UTC" > /usr/local/etc/php/conf.d/timezone.ini && echo "memory_limit = 512M" > /usr/local/etc/php/conf.d/memory-limit.ini

# install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# install nodejs
RUN mkdir -p /etc/apt/keyrings && curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg \
        && NODE_MAJOR=20 && echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list \
        && apt-get update -qq && apt-get install -y -qq nodejs

# clean up
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

It is the image that I used in my pipeline. Flow of the process was too slow such as installing composer depends, npm depends, running tests etc. It took more than 10 minutes. I had to cancel this image then.

Please or to participate in this conversation.