Skip to content

GitLab CI CD configuration

Tom de Wit edited this page Feb 9, 2021 · 4 revisions

CI/CD configuration via .gitlab-ci.yml

Define stages

Define the code quality stages under the stages parameter. Please note that, for brevity, we leave out the build stage, test stages, etc.

stages:
  - Code quality
  - Code quality converter

Define tools

Define the code quality tools under their own parameters. We've included examples in all WIKI entries related to the tools we support:

Example .gitlab-ci.yml

In the following example we've skipped the image parameter, but also the before_scripts, variables and any other unrelated code (in the second example we'll show you the missing code). The following sections can be copy/pasted into your existing .gitlab-ci.yml file.

stages:
  - Code quality
  - Code quality converter

PHPLint:
  stage: Code quality
  allow_failure: true
  script:
    - ./vendor/bin/phplint ./ --exclude=vendor --no-cache --json=phplint.json
  artifacts:
    paths:
      - phplint.json
    when: always

PHPStan:
  stage: Code quality
  allow_failure: true
  script:
    - ./vendor/bin/phpstan analyse app --level max --error-format=gitlab > phpstan.json
  artifacts:
    paths:
      - phpstan.json
    when: always

Psalm:
  stage: Code quality
  allow_failure: true
  script:
    - ./vendor/bin/psalm --report=psalm.json
  artifacts:
    paths:
      - psalm.json
    when: always

PHP_CodeSniffer:
  stage: Code quality
  allow_failure: true
  script:
    - ./vendor/bin/phpcs app --basepath=. --report=json > phpcs.json
  artifacts:
    paths:
      - phpcs.json
    when: always

Phan:
  stage: Code quality
  allow_failure: true
  script:
    - ./vendor/bin/phan --output-mode=json --output=phan.json
  artifacts:
    paths:
      - phan.json
    when: always

PHP-CS-Fixer:
  stage: Code quality
  allow_failure: true
  script:
    - ./vendor/bin/php-cs-fixer fix --allow-risky=yes --format=gitlab --dry-run > php-cs-fixer.json
  artifacts:
    paths:
      - php-cs-fixer.json
    when: always

CodeQuality:
  stage: Code quality converter
  dependencies:
    - Build project
    - PHPLint
    - PHPStan
    - Psalm
    - PHP_CodeSniffer
    - Phan
    - PHP-CS-Fixer
  script:
    - ./vendor/bin/converter convert --phplint --phplint-json-file=phplint.json --psalm --psalm-json-file=psalm.json --phpstan --phpstan-json-file=phpstan.json --php_codesniffer --php_codesniffer-json-file=phpcs.json --phan --phan-json-file=phan.json --php-cs-fixer --php-cs-fixer-json-file=php-cs-fixer.json
  artifacts:
    reports:
      codequality: code-climate.json
    when: always

In the following block of code we'll show you the missing pieces we've skipped over in the previous example. For example, to support Phan you'd need to install and enable AST.

image: php:7.3

before_script:
  - apt-get update -yqq
  - apt-get install libzip-dev zip unzip git wget libicu-dev -yqq
  - pecl install ast
  - docker-php-ext-configure intl
  - docker-php-ext-install pdo_mysql zip intl
  - docker-php-ext-enable ast
  - wget https://composer.github.io/installer.sig -O - -q | tr -d '\n' > installer.sig
  - php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
  - php -r "if (hash_file('SHA384', 'composer-setup.php') === file_get_contents('installer.sig')) { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
  - php composer-setup.php
  - php -r "unlink('composer-setup.php'); unlink('installer.sig');"

variables:
  MYSQL_DATABASE: test
  MYSQL_ROOT_PASSWORD: root
  COMPOSER_CACHE_DIR: "${CI_PROJECT_DIR}/.composer/cache"
  REDIS_PORT: '6379'

stages:
  - Build project
  - Code quality
  - Code quality converter

cache:
  untracked: false
  key: "$CI_BUILD_REF_NAME"

Build project:
  stage: Build project
  script:
    - php composer.phar install --no-scripts
  cache:
    paths:
      - .composer
  artifacts:
    paths:
      - vendor/

Please note the aforementioned example was built inside a Laravel project.

Clone this wiki locally