My task was straight-forward: Add GitLab CI to a Sylius project.
While I have setup a lot of projects with TravisCI, I had never before with GitLab CI. All I knew was, analogous to TravisCI, that I would need some kind of configuration file which controls the automatization. GitLab says, that it is super easy – just create a .gitlab-ci.yml
in your project root. But the more I read GitLabs documentation and blog posts, the more questions came to my mind.
Do I need to create a Docker container?
Where to store it?
Where to register it?
Installing a Gitlab Runner? Where? Why?
What are shared runners?
I went straight to the Sylius Slack Channel to ask if somebody already has a configuration file available … but the answer was negative. I thought that this is a common task and since Sylius is based on Symfony 4 aka Flex, somebody has to have written a config file for it. But … all I found was outdated or misleading information. At the end I cherry picked piece by piece from all those findings and literally pushed line by line to GitLab waiting for the outcome. It took me a little less than 100 commits to get it working. Another 10 for fine tuning.
In the following I will share and explain my config file with you. It can be used for a Sylius 1.3 project.
TLDR: Here you go. If your are in a hurry, just copy the file and commit it to your project.
1stages: 2 - test 3 4variables: 5 MYSQL_DATABASE: sylius_test_cached 6 MYSQL_ROOT_PASSWORD: root 7 MYSQL_USER: root 8 # This is normally provided by the .env file. But since we have to use "mysql" as host name, its defined 9 # here. We not using any .env file!10 # The pattern is: mysql://user:password@host/database11 DATABASE_URL: mysql://$MYSQL_USER:$MYSQL_ROOT_PASSWORD@mysql/$MYSQL_DATABASE12 13 # Set memory limit to infinite to avoid running out of memory issues14 COMPOSER_MEMORY_LIMIT: -115 16 # Set the environments variables manually17 APP_ENV: test_cached18 APP_DEBUG: 119 APP_SECRET: 154275231420 MAILER_URL: smtp://localhost21 22 PHP_DATE_TIMEZONE: Europe/Copenhagen23 24cache:25 key: $CI_COMMIT_REF_NAME26 paths:27 - vendor28 - node_modules29 - public30 - .yarn31 32test:33 stage: test34 35 allow_failure: false # Its there for easy switching36 37 services:38 - mysql:5.739 - memcached40 41 # This image was made for Laravel but works for Sylius as well42 # https://github.com/edbizarro/gitlab-ci-pipeline-php43 image: edbizarro/gitlab-ci-pipeline-php:7.2-alpine44 45 before_script:46 - sudo yarn config set cache-folder .yarn47 - mkdir -p public/media/cache && mkdir -p public/media/image # Avoiding error: public/media/image folder not found48 49 script:50 # Install the dependencies. Don't execute scripts because the cache clean up take too long, sometimes times out51 - composer install --prefer-dist --no-ansi --no-scripts --no-interaction --no-progress --no-suggest52 - bin/console doctrine:database:create --if-not-exists --env=test_cached -vvv # Have to run with debug = true, to omit generation proxies before setting up the database53 - yarn install54 - bin/console sylius:theme:assets:install public55 - bin/console sylius:install:assets56 - yarn build57 - bin/console cache:warmup --env=test_cached --no-debug -vvv58 - bin/console doctrine:migrations:migrate --no-interaction --env=test_cached --no-debug -vvv59 60 # Running checks61 - vendor/bin/phpcs -n --standard=PSR2 src62 - vendor/bin/security-checker security:chec63 - vendor/bin/phpspec run --no-interaction -f dot64 65 # Set up a frontend with fixture data66 - bin/console sylius:fixtures:load --no-interaction --env=test_cached --no-debug -vvv67 - bin/console server:run 127.0.0.1:8080 -d web --env test_cached > /dev/null 2>&1 &68 69 # Running tests70 - echo "Starting BEHAT tests."71 - cp behat.yml.dist behat.yml72 - php bin/behat73 - echo "BEHAT tests done."
I learned that I don't have to create a Docker container by myself. Some awesome people already did this and I (and you) can profit from it. The task was to find this container which comes with batteries included. The batteries here a the a fully installed LAMP stack. I tried alpine
first only to find out it was missing a lot of stuff. I ended with the image from edbizarro/gitlab-ci-pipeline-php which addresses Laravel users but works very well for Flex/Sylius.
It took me a while to configure the connection to the database. Used to use localhost
or 127.0.0.1
for the --host
I kept getting a Connection refused error
. I learned to use mysql
for the host option, since I import a MySQL Docker container when I specify mysql
as a service in .gitlab-ci.yml
.
One of my sources for my .gitlab-ci.yml
was the .travis.yml
file at https://github.com/Sylius/Sylius-Standard/blob/master/.travis.yml. From there I took the rough structure and the environment. In case you wondered why I use --env=test_cached
… now you know :-)
If you read the file you will see that they use a specific .env.test_cached
file, which responds to the test_cached
environment. The copy it and source it to make the environment variables available.
1- cp .env.test_cached.dist .env.test_cached2- set -a && source .env.test_cached && set +a
You will miss this in my config because it sets the DATABASE_URL
to mysql:://root@127.0.0.1/sylius_test_cached
. This was too static for me, so I decided to set the environment variables manually and use those variables to create the database connection string.
All dependencies of Sylius are managed by Composer. There are two spots in the config I want to highlight:
1. Composer run out of memory: Sometimes while he was fetching the dependencies from upstream, sometimes while clearing the cache after installing the dependencies. Therefore I set the memory limit to infinite on line 14.
2. Composer hit max execution time: Again the clear cache command was the culprit. It takes ages to clear an–I guess already empty cache–that it slows down the whole process and sometimes even hits the max_execution_time
limit. I decided to run composer install with the --no-scripts option
(line 51).
I think the rest is pretty basic stuff and self explaining. If you have any question, please leave a comment.