CI/CD for PHP Laravel with Gitlab

gitlab | runner | ci/cd laravel | ci/cd react | sast

Introduction

note we could build on the runner but we are here building on the server for the time being. in that case you would omit --exclude /vendor/ for those to be pushed to the server.

here assuming master as development branch.

Setup

on the target server, prepare and maintain permissions for Laravel altogether.

on the gitlab server, you need to have a runner available that can talk to the destination server.

on the repository, you need to have a ci/cd script with the following content (sample).

#
# php 8.1 & laravel 9
#
# no --filter=':- .gitignore' as it prevents .env from being synced
# we want the latest composer version which can be either in /usr/bin/ or in /usr/local/bin/
# enforcing php version but it needs to know where to find the composer as it is an argument
#

Deploy to machine-name:
  stage: deploy
  variables:
    host_path: "/data/www/validate-laravel"
  script:
    - current_pwd=`pwd`
    - echo host_path is $host_path
    - echo "$ENV_FILE_DEV" > .env # contains passwords
    - ls -lhF .env && wc -l .env && md5sum .env
    - rsync -a --omit-dir-times --delete --exclude '/.git*' --exclude /bootstrap/cache/ --exclude /storage/ --exclude /vendor/ $current_pwd/ runner@machine-name:$host_path/
    - ssh runner@machine-name "cd $host_path/ && ls -lhF .env && wc -l .env && md5sum .env"
    - ssh runner@machine-name "which php && php --version"
    - ssh runner@machine-name "which php8.1 && php8.1 --version"
    - ssh runner@machine-name "which composer && composer --version"
    - ssh runner@machine-name "cd $host_path/ && time time nice php8.1 /usr/bin/composer install --ansi && echo OK"
  only:
    - master
  tags:
    - runner1

HOME | GUIDES | LECTURES | LAB | SMTP HEALTH | HTML5
Copyright © 2022 Pierre-Philipp Braun