Configuring gitlab-ci.yaml for Drupal with DTT, DrupalCS, DrupalCBF, and GrumPHP Pre-commit Hooks.
When working on Drupal projects, it’s essential to maintain high code quality standards and ensure that the codebase follows Drupal’s coding standards. This can be achieved by setting up CI/CD pipelines in GitLab and using tools like Drupal Test Traits (DTT), Drupal Coding Standards (DrupalCS), Drupal Code Beautifier and Fixer (DrupalCBF), and GrumPHP for pre-commit hooks. Additionally, using Docker images for these tools ensures consistency across development environments.
Below is a guide to configuring your gitlab-ci.yaml to incorporate these tools into your Drupal project.
Step 1: Setting Up Docker Image
Firstly, it's crucial to define the Docker image that will be used in the pipeline. Using a custom Docker image that includes all the necessary tools for testing, coding standards, and pre-commit hooks will streamline the CI/CD process.
image: registry.gitlab.com/your-repo/custom-drupal-image:latest
services:
- docker:dind
variables:
DOCKER_DRIVER: overlay2
before_script:
- docker pull registry.gitlab.com/your-repo/custom-drupal-image:latest
In this example, replace registry.gitlab.com/your-repo/custom-drupal-image:latest with the actual Docker image path that contains DTT, DrupalCS, DrupalCBF, and GrumPHP.
Step 2: Define Stages
Define the stages that will be used in the pipeline. Typically, these include stages like test, code_quality, and pre_commit.
stages:
- test
- code_quality
- pre_commit
Step 3: Drupal Test Traits (DTT) Configuration
The test stage runs unit tests using DTT. It ensures that your code is tested against Drupal’s testing standards.
test:
stage: test
script:
- vendor/bin/phpunit --group=dtt --configuration=phpunit.xml
artifacts:
paths:
- tests/reports
only:
- merge_requests
- main
Step 4: DrupalCS and DrupalCBF Configuration
The code_quality stage checks the code against Drupal’s coding standards using DrupalCS and optionally fixes the issues using DrupalCBF.
code_quality:
stage: code_quality
script:
- vendor/bin/phpcs --standard=Drupal,DrupalPractice web/modules/custom
- vendor/bin/phpcbf --standard=Drupal,DrupalPractice web/modules/custom
only:
- merge_requests
- main
Step 5: GrumPHP Pre-commit Hook Configuration
The pre_commit stage is crucial for ensuring that the code follows the set guidelines before it is committed to the repository.
pre_commit:
stage: pre_commit
script:
- vendor/bin/grumphp run
only:
- merge_requests
- main
Conclusion
By configuring your gitlab-ci.yaml with Docker images that include DTT, DrupalCS, DrupalCBF, and GrumPHP, you ensure a consistent and high-quality Drupal development process. This setup not only enforces coding standards but also automates testing and code formatting, making it an indispensable part of any professional Drupal project.This approach significantly reduces the chances of code defects making it into the production environment, resulting in a more robust and maintainable codebase.