DevOps Blog - Nicolas Paris

Unit Test Laravel Application with MSSQL on Gitlab

Docker

On the Previous Dockerfile Let's add a gitlab CICD stage that will lauch unit test on a docker MSSQL image

test_laravel:
stage: test
image: ${IMAGE}:${TAG}
services:
- name: mcr.microsoft.com/mssql/server:2019-latest
alias: mssql
variables:
MSSQL_PID: Developer
MSSQL_SA_PASSWORD: yourStrong.Password
ACCEPT_EULA: Y
tags:
- gcp-docker
except:
- tags
before_script:
- ACCEPT_EULA=Y apt-get install -y mssql-tools18
- curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
- cd /var/www/html
- composer install
- php artisan route:clear
- /opt/mssql-tools18/bin/sqlcmd -S mssql -U sa -P "yourStrong.Password" -q "CREATE DATABASE arf_backend_test" -C
- php artisan migrate
script:
- php artisan test

This will add a mssql server as a Gitlab service that will be fresh created on every build. The trick is on the database creation, there is on initial setup to do this on the MSSQL server. That's why the mssql-tools is installed and the dadtabase is created.

You might need to add DB_TRUST_SERVER_CERTIFICATE='true' on your .env if you have some certificate errors.