Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
213 changes: 213 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
# GitLab CI for Moodle β€” mirrors typical GitHub Actions pipeline
# - Runs PHP unit tests against MySQL and PostgreSQL
# - Runs Behat features (headless Chrome)
# - Uses Node.js version from .nvmrc when present, otherwise falls back to latest LTS
#
# Notes:
# 1) Set the following CI/CD variables in your project/group for reliable runs:
# - APP_BASE_URL β†’ e.g. http://localhost:8000 (used by Behat)
# - PHP_VERSION β†’ optional, e.g. 8.2 (selects image tag below if you change it)
# - MYSQL_DATABASE, MYSQL_USER, MYSQL_PASSWORD (optional; defaults used below)
# - POSTGRES_DB, POSTGRES_USER, POSTGRES_PASSWORD (optional; defaults used below)
# 2) This pipeline starts a PHP built‑in server for Behat. For large suites you may prefer Apache/Nginx containers.
# 3) If you have a .nvmrc at repo root, that version will be used. Otherwise latest LTS Node is installed.
#
# You can further tune PHP extensions/images to match your production matrix.

stages:
- prepare
- phpunit
- behat

.default-variables: &default-variables
# Keep Composer non‑interactive
COMPOSER_NO_INTERACTION: "1"
COMPOSER_CACHE_DIR: "$CI_PROJECT_DIR/.cache/composer"
NPM_CONFIG_CACHE: "$CI_PROJECT_DIR/.cache/npm"
APP_BASE_URL: "http://127.0.0.1:8000"
# Default DB creds for MySQL service
MYSQL_DATABASE: "moodle"
MYSQL_USER: "moodle"
MYSQL_PASSWORD: "moodle"
# Default DB creds for Postgres service
POSTGRES_DB: "moodle"
POSTGRES_USER: "moodle"
POSTGRES_PASSWORD: "moodle"
# PHPUnit parallelism (tune for your runners)
PHPUNIT_PARALLEL: "2"

.default-image: &default-image
image: "edbizarro/gitlab-ci-pipeline-php:8.2"

.caches: &caches
cache:
key: "$CI_JOB_NAME"
paths:
- .cache/composer/
- vendor/
- node_modules/
policy: pull-push

.before-script-base: &before-script-base
- php -v
- composer --version
# --- Install nvm and select Node from .nvmrc (or latest LTS if missing) ---
- export NVM_DIR="$CI_PROJECT_DIR/.nvm"
- curl -fsSL https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
- . "$NVM_DIR/nvm.sh"
- |
if [ -f .nvmrc ]; then
echo "Using Node version from .nvmrc: $(cat .nvmrc)"
nvm install
nvm use
else
echo "No .nvmrc found; using latest LTS Node"
nvm install --lts
nvm use --lts
fi
- node -v && npm -v
# --- Install PHP and JS deps ---
- composer install --prefer-dist --no-progress --no-interaction
- |
if [ -f package-lock.json ] || [ -f npm-shrinkwrap.json ]; then
npm ci
else
npm install
fi
# Create writable dirs commonly needed by Moodle
- mkdir -p "$CI_PROJECT_DIR"/{moodledata,report}

# Helper to generate a minimal config.php suitable for tests.
# Adjust CFG settings if your project requires special paths/plugins.
.make-config-php: &make-config-php |
cat > config.php <<'PHP'
<?php
// Auto-generated by CI for testing only.
if (!getenv('DBTYPE')) { $_ENV['DBTYPE'] = 'mysqli'; }
$dbtype = getenv('DBTYPE') ?: 'mysqli';
$dblibrary = 'native';
$dbhost = getenv('DBHOST') ?: ($dbtype === 'pgsql' ? 'postgres' : 'mysql');
$dbname = getenv('DBNAME') ?: 'moodle';
$dbuser = getenv('DBUSER') ?: 'moodle';
$dbpass = getenv('DBPASS') ?: 'moodle';
$dbprefix = 'mdl_';
$wwwroot = getenv('APP_BASE_URL') ?: 'http://127.0.0.1:8000';
$dataroot = __DIR__ . '/moodledata';
$directorypermissions = 02777;
$CFG = new stdClass();
$CFG->dbtype = $dbtype;
$CFG->dblibrary = $dblibrary;
$CFG->dbhost = $dbhost;
$CFG->dbname = $dbname;
$CFG->dbuser = $dbuser;
$CFG->dbpass = $dbpass;
$CFG->prefix = $dbprefix;
$CFG->wwwroot = $wwwroot;
$CFG->dataroot = $dataroot;
$CFG->directorypermissions = $directorypermissions;
$CFG->debug = E_ALL | E_STRICT;
$CFG->debugdisplay = 1;
require_once(__DIR__ . '/lib/setup.php');
PHP

prepare:
stage: prepare
<<: *default-image
variables:
<<: *default-variables
<<: *caches
script:
- *before-script-base
rules:
- if: '$CI_PIPELINE_SOURCE == "push" || $CI_PIPELINE_SOURCE == "merge_request_event"'

phpunit:mysql:
stage: phpunit
<<: *default-image
variables:
<<: *default-variables
DBTYPE: "mysqli"
DBHOST: "mysql"
DBNAME: "$MYSQL_DATABASE"
DBUSER: "$MYSQL_USER"
DBPASS: "$MYSQL_PASSWORD"
MYSQL_ROOT_PASSWORD: "root"
services:
- name: mysql:8
alias: mysql
command: ["--default-authentication-plugin=mysql_native_password", "--character-set-server=utf8mb4", "--collation-server=utf8mb4_unicode_ci"]
<<: *caches
script:
- *before-script-base
- *make-config-php
# Init PHPUnit (creates test tables etc.)
- php admin/tool/phpunit/cli/init.php
# Run tests in parallel if supported
- vendor/bin/phpunit --configuration=phpunit.xml.dist --testsuite core --colors=always -v

phpunit:pgsql:
stage: phpunit
<<: *default-image
variables:
<<: *default-variables
DBTYPE: "pgsql"
DBHOST: "postgres"
DBNAME: "$POSTGRES_DB"
DBUSER: "$POSTGRES_USER"
DBPASS: "$POSTGRES_PASSWORD"
services:
- name: postgres:16
alias: postgres
variables:
POSTGRES_DB: "$POSTGRES_DB"
POSTGRES_USER: "$POSTGRES_USER"
POSTGRES_PASSWORD: "$POSTGRES_PASSWORD"
<<: *caches
script:
- *before-script-base
- *make-config-php
- php admin/tool/phpunit/cli/init.php
- vendor/bin/phpunit --configuration=phpunit.xml.dist --testsuite core --colors=always -v

behat:
stage: behat
<<: *default-image
variables:
<<: *default-variables
DBTYPE: "mysqli"
DBHOST: "mysql"
DBNAME: "$MYSQL_DATABASE"
DBUSER: "$MYSQL_USER"
DBPASS: "$MYSQL_PASSWORD"
MOODLE_WWWROOT: "$APP_BASE_URL"
BEHAT_FEATURE_RUNNER: "vendor/bin/behat"
# Behat integration config
BEHAT_FAILDIFF: "$CI_PROJECT_DIR/report/behat-fails"
services:
- name: mysql:8
alias: mysql
command: ["--default-authentication-plugin=mysql_native_password", "--character-set-server=utf8mb4", "--collation-server=utf8mb4_unicode_ci"]
- name: selenium/standalone-chrome:4.25.0
alias: selenium
<<: *caches
script:
- *before-script-base
- *make-config-php
# Start PHP built-in server to serve Moodle to Selenium
- php -S 0.0.0.0:8000 -t public >/dev/null 2>&1 &
- sleep 5
# Prepare Behat (updates database and config for Behat testing)
- php admin/tool/behat/cli/init.php
# Variables for Behat/Moodle integration
- export BEHAT_WD_HOST=http://selenium:4444/wd/hub
- export BEHAT_SELENIUM2_URL=$BEHAT_WD_HOST
# Run Behat; adjust profiles or tags as needed for your project
- vendor/bin/behat --colors -vv --config "${CI_PROJECT_DIR}/behat.yml" || vendor/bin/behat --colors -vv --rerun
artifacts:
when: always
paths:
- report/
- moodledata/behat/*/faildump/
expire_in: 7 days
rules:
- if: '$CI_PIPELINE_SOURCE == "push" || $CI_PIPELINE_SOURCE == "merge_request_event"'