Skip to main content

Composer Build

The composer-build job is used to build PHP applications using Composer. It installs dependencies and prepares the application for deployment.

Overview

This job:

  • Installs PHP dependencies using Composer
  • Removes the .git/ directory before building
  • Caches Composer dependencies for faster subsequent builds
  • Creates build artifacts that can be used in subsequent pipeline stages

Variables

The following variables can be configured:

VariableDescriptionDefaultRequired
PHP_BUILD_PATHPath to the PHP project root directory'./'No
COMPOSER_IMAGEDocker image to use for Composer'composer:2'No
COMPOSER_BUILD_OPTSOptions passed to composer install"--verbose --ignore-platform-reqs --no-progress --no-scripts --optimize-autoloader --no-interaction"No

Variable Details

  • PHP_BUILD_PATH: The directory path where the Composer project is located. Defaults to the repository root ('./').
  • COMPOSER_IMAGE: The Docker image used to run Composer commands. Defaults to composer:2.
  • COMPOSER_BUILD_OPTS: Command-line options passed to composer install. The default options include:
    • --verbose: Show detailed output
    • --ignore-platform-reqs: Ignore platform requirements
    • --no-progress: Disable progress display
    • --no-scripts: Skip running scripts defined in composer.json
    • --optimize-autoloader: Optimize the autoloader
    • --no-interaction: Run in non-interactive mode

Usage

Basic Usage

To use the composer-build job in your pipeline, include it in your .gitlab-ci.yml:

variables:
PHP_BUILD_PATH: './'
COMPOSER_IMAGE: 'composer:2'
COMPOSER_BUILD_OPTS: "--verbose --ignore-platform-reqs --no-progress --no-scripts --optimize-autoloader --no-interaction"

.composer-build:
image: $COMPOSER_IMAGE
before_script:
- cd $PHP_BUILD_PATH
- rm -rf .git/
script:
- composer install $COMPOSER_BUILD_OPTS
cache:
key: "$PROJECT_ID_composer"
paths:
- .composer/
artifacts:
paths:
- ./
expire_in: 1 hour

Customizing Variables

You can override the default variables to customize the build:

variables:
PHP_BUILD_PATH: './src'
COMPOSER_IMAGE: 'composer:2.6'
COMPOSER_BUILD_OPTS: "--no-dev --optimize-autoloader"

.composer-build:
image: $COMPOSER_IMAGE
before_script:
- cd $PHP_BUILD_PATH
- rm -rf .git/
script:
- composer install $COMPOSER_BUILD_OPTS
cache:
key: "$PROJECT_ID_composer"
paths:
- .composer/
artifacts:
paths:
- ./
expire_in: 1 hour

Job Details

Before Script

The before_script section:

  1. Changes to the PHP_BUILD_PATH directory
  2. Removes the .git/ directory to reduce artifact size

Script

The main script runs:

composer install $COMPOSER_BUILD_OPTS

This installs all dependencies defined in composer.json and composer.lock.

Cache

The job caches the .composer/ directory using a cache key based on $PROJECT_ID. This speeds up subsequent builds by reusing downloaded packages.

Artifacts

The job creates artifacts containing:

  • The entire project directory (./)
  • Expires after 1 hour

These artifacts can be used by subsequent jobs in the pipeline (e.g., deployment jobs).

Example Pipeline

Here's an example of how to use composer-build in a complete pipeline:

variables:
PROJECT_ID: 'p222-01'
PHP_BUILD_PATH: './'
COMPOSER_IMAGE: 'composer:2'
COMPOSER_BUILD_OPTS: "--verbose --ignore-platform-reqs --no-progress --no-scripts --optimize-autoloader --no-interaction"

stages:
- build
- deploy

.composer-build:
image: $COMPOSER_IMAGE
stage: build
before_script:
- cd $PHP_BUILD_PATH
- rm -rf .git/
script:
- composer install $COMPOSER_BUILD_OPTS
cache:
key: "$PROJECT_ID_composer"
paths:
- .composer/
artifacts:
paths:
- ./
expire_in: 1 hour

build:
extends: .composer-build
only:
- main
- develop

Notes

  • The .git/ directory is removed to reduce artifact size and avoid including unnecessary files
  • The cache key uses $PROJECT_ID to ensure cache isolation between different projects
  • Artifacts expire after 1 hour to prevent storage bloat
  • The job uses the Composer image directly, so it doesn't require a custom Dockerfile