Integrating Composer with Continuous Integration (CI) on Ubuntu
Composer is a dependency management tool for PHP, and integrating it with a CI/CD pipeline on Ubuntu ensures that your project’s dependencies are consistently installed, tested, and deployed. Below is a structured guide to setting up Composer-based CI, covering environment setup, configuration, and integration with popular CI tools.
Before configuring CI, ensure your Ubuntu environment has the necessary tools:
sudo apt update && sudo apt upgrade -y to get the latest system updates.sudo apt install php php-cli php-mbstring php-xml php-zip -y. Verify with php -v.php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php
php -r "unlink('composer-setup.php');"
sudo mv composer.phar /usr/local/bin/composer
Validate installation with composer --version.Optimize Composer for CI environments to improve speed and reliability:
composer.json:{
"repositories": {
"packagist": {
"type": "composer",
"url": "https://mirrors.aliyun.com/composer/"
}
}
}
Alternatively, set this globally via composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/."prefer-dist": true to composer.json to install stable, archived versions of packages (faster and more reliable than source builds).composer.json:{
"config": {
"process-timeout": 1800
}
}
vendor directory between runs to avoid reinstalling unchanged dependencies (covered in CI tool sections below).Choose a CI tool based on your team’s needs (e.g., GitHub Actions for GitHub repositories, GitLab CI for GitLab). Below are configurations for popular tools:
GitHub Actions integrates seamlessly with GitHub repositories. Create a .github/workflows/ci.yml file in your project root:
name: PHP CI
on:
push:
branches: [ main ] # Trigger on pushes to the main branch
pull_request:
branches: [ main ] # Trigger on pull requests to the main branch
jobs:
build:
runs-on: ubuntu-latest # Use the latest Ubuntu environment
steps:
- name: Checkout Code
uses: actions/checkout@v3 # Checkout the repository code
- name: Setup PHP
uses: shivammathur/setup-php@v2 # Set up PHP with required extensions
with:
php-version: '8.1' # Specify your PHP version
extensions: xdebug # Add extensions if needed (e.g., xdebug for debugging)
- name: Install Dependencies
run: composer install --prefer-dist --no-progress --no-interaction # Install dependencies without progress output
- name: Run Tests
run: vendor/bin/phpunit # Execute unit tests (assuming PHPUnit is configured)
This workflow triggers on code pushes/pull requests, installs dependencies via Composer, and runs tests.
For GitLab repositories, create a .gitlab-ci.yml file:
stages:
- build
- test
build_job:
stage: build
image: php:8.1 # Use a PHP Docker image
script:
- apt-get update && apt-get install -y unzip git # Install dependencies
- curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer # Install Composer
- composer install --prefer-dist --no-progress --no-interaction # Install dependencies
artifacts:
paths:
- vendor/ # Cache vendor directory for subsequent jobs
test_job:
stage: test
image: php:8.1
script:
- vendor/bin/phpunit # Run tests
needs: [build_job] # Depend on the build job to ensure dependencies are installed
This pipeline installs Composer, caches dependencies, and runs tests in separate stages.
For Jenkins, create a pipeline script (Jenkinsfile) using the Declarative Pipeline syntax:
pipeline {
agent any
stages {
stage('Checkout') {
steps {
git branch: 'main', url: 'https://github.com/your-repo/your-project.git' // Checkout code from GitHub
}
}
stage('Setup PHP & Composer') {
steps {
sh 'sudo apt-get update && sudo apt-get install -y php php-cli php-mbstring php-xml php-zip unzip git' // Install PHP and dependencies
sh 'curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer' // Install Composer
}
}
stage('Install Dependencies') {
steps {
sh 'composer install --prefer-dist --no-progress --no-interaction' // Install dependencies
}
}
stage('Run Tests') {
steps {
sh 'vendor/bin/phpunit' // Run tests
}
}
}
}
This pipeline checks out code, sets up PHP/Composer, installs dependencies, and runs tests.
To enforce code quality before CI execution, integrate GrumPHP (a PHP task runner). Install it via Composer:
composer require --dev phpro/grumphp
Configure tasks in grumphp.yml (e.g., PHPUnit, PHP_CodeSniffer):
parameters:
tasks:
phpunit:
config: phpunit.xml # Path to PHPUnit config
triggered_by: [php] # Run when PHP files change
fail_on_error: true # Fail CI if tests fail
phpcs:
standard: PSR12 # Enforce PSR12 coding standards
exclude: [Tests/, vendor/] # Ignore test and vendor directories
triggered_by: [php]
Integrate GrumPHP with CI by adding a step to run vendor/bin/grumphp run (e.g., in GitHub Actions):
- name: Run GrumPHP
run: vendor/bin/grumphp run
This ensures code quality checks pass before proceeding to dependency installation or testing.
By following these steps, you can integrate Composer into your Ubuntu-based CI pipeline, ensuring consistent dependency management and code quality across your PHP projects.