From 298ef21457112ff3533c710cfd98e1bcea9f602b Mon Sep 17 00:00:00 2001 From: "jetbrains-junie[bot]" <201638009+jetbrains-junie[bot]@users.noreply.github.com> Date: Thu, 16 Oct 2025 19:32:54 +0000 Subject: [PATCH] feat(ci): add GitLab CI pipeline mirroring GitHub Actions Added a GitLab CI pipeline mirroring GitHub Actions with phpunit and behat jobs. Node.js version is read from .nvmrc with fallback. Provided configuration guidance and environment variable setup. --- .gitlab-ci.yml | 213 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 213 insertions(+) create mode 100644 .gitlab-ci.yml diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000000000..5825a1a5cda92 --- /dev/null +++ b/.gitlab-ci.yml @@ -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' + 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"' \ No newline at end of file