From c7704c11b44d01c864fdf89f1dc87e86c8daa29c Mon Sep 17 00:00:00 2001 From: Daniel-I-Am Date: Sat, 4 Sep 2021 13:32:35 +0200 Subject: [PATCH 01/14] First attempt of adding CI/CD for php --- .gitlab-ci.yml | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 1a965b9..4d13256 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,7 +1,46 @@ stages: + - build + - test - docker - deploy +variables: + MYSQL_DATABASE: portfolio + MYSQL_ROOT_PASSWORD: password + +php-build-job: + stage: build + image: composer:2 + script: + - composer install + artifacts: + paths: + - vendor/ + +php-test-job: + stage: test + image: php:8-cli + services: + - mysql:5 + needs: + - php-build-job + dependencies: + - php-build-job + script: + - yes | pecl install xdebug + - echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" > /usr/local/etc/php/conf.d/xdebug.ini + - echo "xdebug.mode=coverage" >> /usr/local/etc/php/conf.d/xdebug.ini + - export DB_CONNECTION=mysql + - export DB_HOST=mysql + - export DB_PORT=3306 + - export DB_DATABASE="$MYSQL_DATABASE" + - export DB_USERNAME=root + - export DB_PASSWORD="$MYSQL_ROOT_PASSWORD" + - ./vendor/bin/phpunit --coverage-cobertura code-coverage.xml + artifacts: + reports: + cobertura: code-coverage.xml + docker-auth-job: stage: docker image: docker:stable @@ -21,6 +60,8 @@ docker-build-job: only: refs: - master + needs: + - php-test-job tags: - docker script: From 8858cb7a1ec2752adb6c05f91f4dfb2626d3a34c Mon Sep 17 00:00:00 2001 From: Daniel-I-Am Date: Sat, 4 Sep 2021 13:45:51 +0200 Subject: [PATCH 02/14] Remove the yes pipe, since the pipeline is inexplicably failing --- .gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 4d13256..acd1037 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -27,7 +27,7 @@ php-test-job: dependencies: - php-build-job script: - - yes | pecl install xdebug + - pecl install xdebug - echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" > /usr/local/etc/php/conf.d/xdebug.ini - echo "xdebug.mode=coverage" >> /usr/local/etc/php/conf.d/xdebug.ini - export DB_CONNECTION=mysql From 55f6c5a7e1eb38f6c226cddd8a7a35ca1ba2ee4f Mon Sep 17 00:00:00 2001 From: Daniel-I-Am Date: Sat, 4 Sep 2021 13:50:11 +0200 Subject: [PATCH 03/14] Configure env file during php-build --- .env.testing | 15 +++++++++++++++ .gitlab-ci.yml | 3 +++ 2 files changed, 18 insertions(+) create mode 100644 .env.testing diff --git a/.env.testing b/.env.testing new file mode 100644 index 0000000..b415448 --- /dev/null +++ b/.env.testing @@ -0,0 +1,15 @@ +APP_NAME=Laravel +APP_ENV=testing +APP_KEY= +APP_DEBUG=true +APP_URL=http://localhost + +LOG_CHANNEL=stack +LOG_LEVEL=debug + +BROADCAST_DRIVER=log +CACHE_DRIVER=file +FILESYSTEM_DRIVER=local +QUEUE_CONNECTION=sync +SESSION_DRIVER=file +SESSION_LIFETIME=120 diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index acd1037..274ad65 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -13,8 +13,11 @@ php-build-job: image: composer:2 script: - composer install + - cp .env.testing .env + - php artisan key:generate artifacts: paths: + - .env - vendor/ php-test-job: From c7a6e20fadab2e908ae79aca298d06dd09595c59 Mon Sep 17 00:00:00 2001 From: Daniel-I-Am Date: Sat, 4 Sep 2021 13:55:49 +0200 Subject: [PATCH 04/14] Cache vendor folder between builds --- .gitlab-ci.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 274ad65..fa05e2e 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -19,6 +19,9 @@ php-build-job: paths: - .env - vendor/ + cache: + paths: + - vendor/ php-test-job: stage: test From a3985586145820178106f7206a97783a9aaabae3 Mon Sep 17 00:00:00 2001 From: Daniel-I-Am Date: Sat, 4 Sep 2021 13:55:59 +0200 Subject: [PATCH 05/14] Read the .env file before running phpunit --- .gitlab-ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index fa05e2e..d9a2594 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -36,6 +36,7 @@ php-test-job: - pecl install xdebug - echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" > /usr/local/etc/php/conf.d/xdebug.ini - echo "xdebug.mode=coverage" >> /usr/local/etc/php/conf.d/xdebug.ini + - source .env - export DB_CONNECTION=mysql - export DB_HOST=mysql - export DB_PORT=3306 From b289a4725be148edaab04ac2ef09085718ab4643 Mon Sep 17 00:00:00 2001 From: Daniel-I-Am Date: Sat, 4 Sep 2021 14:06:11 +0200 Subject: [PATCH 06/14] PHPUnit uses .env.testing by default, so that file needs to contain the APP_KEY --- .gitlab-ci.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index d9a2594..7b31931 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -15,9 +15,11 @@ php-build-job: - composer install - cp .env.testing .env - php artisan key:generate + - cp .env .env.testing artifacts: paths: - .env + - .env.testing - vendor/ cache: paths: @@ -43,7 +45,7 @@ php-test-job: - export DB_DATABASE="$MYSQL_DATABASE" - export DB_USERNAME=root - export DB_PASSWORD="$MYSQL_ROOT_PASSWORD" - - ./vendor/bin/phpunit --coverage-cobertura code-coverage.xml + - php artisan test artifacts: reports: cobertura: code-coverage.xml From 602729d07c6d510fd3589de59a607ac2252051f8 Mon Sep 17 00:00:00 2001 From: Daniel-I-Am Date: Sat, 4 Sep 2021 14:11:06 +0200 Subject: [PATCH 07/14] Move env copy to test job --- .gitlab-ci.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 7b31931..dd14dab 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -15,11 +15,9 @@ php-build-job: - composer install - cp .env.testing .env - php artisan key:generate - - cp .env .env.testing artifacts: paths: - .env - - .env.testing - vendor/ cache: paths: @@ -38,7 +36,7 @@ php-test-job: - pecl install xdebug - echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" > /usr/local/etc/php/conf.d/xdebug.ini - echo "xdebug.mode=coverage" >> /usr/local/etc/php/conf.d/xdebug.ini - - source .env + - cp -f .env .env.testing - export DB_CONNECTION=mysql - export DB_HOST=mysql - export DB_PORT=3306 From 3f7744e51c4a66a3e0f0ba30d8c2b2fa2fd4664f Mon Sep 17 00:00:00 2001 From: Daniel-I-Am Date: Sat, 4 Sep 2021 14:17:15 +0200 Subject: [PATCH 08/14] Add coverage reporting back in --- .gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index dd14dab..6a7778a 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -43,7 +43,7 @@ php-test-job: - export DB_DATABASE="$MYSQL_DATABASE" - export DB_USERNAME=root - export DB_PASSWORD="$MYSQL_ROOT_PASSWORD" - - php artisan test + - php artisan test --coverage-cobertura code-coverage.xml artifacts: reports: cobertura: code-coverage.xml From 87159673a5d3a6e373cf4110e6d1f8fa4751099a Mon Sep 17 00:00:00 2001 From: Daniel-I-Am Date: Sat, 4 Sep 2021 14:17:27 +0200 Subject: [PATCH 09/14] Add some debugging of the env files --- .gitlab-ci.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 6a7778a..6fde7e7 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -43,6 +43,8 @@ php-test-job: - export DB_DATABASE="$MYSQL_DATABASE" - export DB_USERNAME=root - export DB_PASSWORD="$MYSQL_ROOT_PASSWORD" + - cat .env || true + - cat .env.testing || true - php artisan test --coverage-cobertura code-coverage.xml artifacts: reports: From 51cd79505c5405a3dfe219b0f8dc11eb84237ecd Mon Sep 17 00:00:00 2001 From: Daniel-I-Am Date: Sat, 4 Sep 2021 14:29:32 +0200 Subject: [PATCH 10/14] Add even more debugging --- .gitlab-ci.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 6fde7e7..a3d66b7 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -13,8 +13,14 @@ php-build-job: image: composer:2 script: - composer install + - cat .env || true + - cat .env.testing || true - cp .env.testing .env + - cat .env || true + - cat .env.testing || true - php artisan key:generate + - cat .env || true + - cat .env.testing || true artifacts: paths: - .env From 02b487191c2a3de8fa1a6d9f7b1b4c135c3697ed Mon Sep 17 00:00:00 2001 From: Daniel-I-Am Date: Sat, 4 Sep 2021 14:33:10 +0200 Subject: [PATCH 11/14] Artisan undocumented functionality seems to be the cause... --- .gitlab-ci.yml | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index a3d66b7..afd0025 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -13,17 +13,10 @@ php-build-job: image: composer:2 script: - composer install - - cat .env || true - - cat .env.testing || true - - cp .env.testing .env - - cat .env || true - - cat .env.testing || true - php artisan key:generate - - cat .env || true - - cat .env.testing || true artifacts: paths: - - .env + - .env.testing - vendor/ cache: paths: @@ -42,15 +35,12 @@ php-test-job: - pecl install xdebug - echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" > /usr/local/etc/php/conf.d/xdebug.ini - echo "xdebug.mode=coverage" >> /usr/local/etc/php/conf.d/xdebug.ini - - cp -f .env .env.testing - export DB_CONNECTION=mysql - export DB_HOST=mysql - export DB_PORT=3306 - export DB_DATABASE="$MYSQL_DATABASE" - export DB_USERNAME=root - export DB_PASSWORD="$MYSQL_ROOT_PASSWORD" - - cat .env || true - - cat .env.testing || true - php artisan test --coverage-cobertura code-coverage.xml artifacts: reports: From 1d4af01bcf848afa9f152d56807c94a295050193 Mon Sep 17 00:00:00 2001 From: Daniel-I-Am Date: Sat, 4 Sep 2021 14:39:28 +0200 Subject: [PATCH 12/14] Add node-build-job in order to create mix-manifest --- .gitlab-ci.yml | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index afd0025..645a81d 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -22,15 +22,34 @@ php-build-job: paths: - vendor/ -php-test-job: +node-build-job: + stage: build + image: node:16 + script: + - npm install --global yarn + - yarn + - yarn dev + artifacts: + paths: + - public/js + - public/css + - public/fonts + - public/mix-manifest.json + cache: + paths: + - node_modules/ + +test-job: stage: test image: php:8-cli services: - mysql:5 needs: - php-build-job + - node-build-job dependencies: - php-build-job + - node-build-job script: - pecl install xdebug - echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" > /usr/local/etc/php/conf.d/xdebug.ini @@ -41,10 +60,10 @@ php-test-job: - export DB_DATABASE="$MYSQL_DATABASE" - export DB_USERNAME=root - export DB_PASSWORD="$MYSQL_ROOT_PASSWORD" - - php artisan test --coverage-cobertura code-coverage.xml + - php artisan test --coverage-cobertura php-coverage.xml artifacts: reports: - cobertura: code-coverage.xml + cobertura: php-coverage.xml docker-auth-job: stage: docker @@ -66,7 +85,7 @@ docker-build-job: refs: - master needs: - - php-test-job + - test-job tags: - docker script: From a40bd89d10340e565d1813b00231b50cb5a7b15f Mon Sep 17 00:00:00 2001 From: Daniel-I-Am Date: Sat, 4 Sep 2021 14:43:38 +0200 Subject: [PATCH 13/14] Yarn comes preinstalled with the node docker image --- .gitlab-ci.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 645a81d..466650d 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -26,7 +26,6 @@ node-build-job: stage: build image: node:16 script: - - npm install --global yarn - yarn - yarn dev artifacts: From d594e264acd7cd1516f59c4fbb3cdcc645c13123 Mon Sep 17 00:00:00 2001 From: Daniel-I-Am Date: Sat, 4 Sep 2021 14:57:55 +0200 Subject: [PATCH 14/14] Switch to calling phpunit directly to allow coverage-text to be printed, read that text into pipeline report --- .gitlab-ci.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 466650d..e3f94a7 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -59,10 +59,11 @@ test-job: - export DB_DATABASE="$MYSQL_DATABASE" - export DB_USERNAME=root - export DB_PASSWORD="$MYSQL_ROOT_PASSWORD" - - php artisan test --coverage-cobertura php-coverage.xml + - ./vendor/bin/phpunit --coverage-text --coverage-cobertura php-coverage.xml artifacts: reports: cobertura: php-coverage.xml + coverage: '/^\s*Lines:\s*\d+(.\d+)?\%/' docker-auth-job: stage: docker