Using docker-compose-ci: Difference between revisions

From Freephile Wiki
No edit summary
No edit summary
Line 1: Line 1:
These notes supplement [[Automated deployment of MediaWiki]] and also [[PHPUnit/VSCode]]
These notes supplement [[Automated deployment of MediaWiki]] and also [[PHPUnit/VSCode]]


I added '''docker-compose-ci''' to the CrawlerProtection extension, and here is how to do it.
I added the '''Gesinn IT''' [https://github.com/gesinn-it-pub/docker-compose-ci docker compose CI] system to the CrawlerProtection extension, and here is how to do it.


Initial setup
Initial setup
Line 95: Line 95:


=== Direct usage ===
=== Direct usage ===
This is kinda long and difficult.
<code>./vendor/bin/phpcbf -d memory_limit=512M --standard=MediaWiki --ignore=*/build/*,*/vendor/* /var/www/html/extensions/CrawlerProtection/</code>
<code>./vendor/bin/phpcbf -d memory_limit=512M --standard=MediaWiki --ignore=*/build/*,*/vendor/* /var/www/html/extensions/CrawlerProtection/</code>


Line 103: Line 105:
=== Fully-Configured usage ===
=== Fully-Configured usage ===
<code>composer phpcbf</code>
<code>composer phpcbf</code>
'''In the Docker container:'''
* Extension is at <code>/var/www/html/extensions/CrawlerProtection/</code>
* MediaWiki vendor is at <code>/var/www/html/vendor/</code>
* So <code>../../vendor/bin/phpcs</code> resolves correctly
'''In GitHub Actions:'''
* Extension is at <code>$GITHUB_WORKSPACE/extensions/CrawlerProtection/</code>
* MediaWiki vendor is at <code>$GITHUB_WORKSPACE/vendor/</code>
* Same <code>../../vendor/bin/phpcs</code> path works
'''Testing:'''


With everything in place, we should be able to <code>make bash</code> to get a command line in the container, and <code>composer phpcs</code> or <code>composer phpcbf</code> to run the tools.  Plus, fixes in the container can be viewed/edited/committed from VSCode on the host thanks to the <code>docker-compose-override.yml</code> filesystem mounts.
With everything in place, we should be able to <code>make bash</code> to get a command line in the container, and <code>composer phpcs</code> or <code>composer phpcbf</code> to run the tools.  Plus, fixes in the container can be viewed/edited/committed from VSCode on the host thanks to the <code>docker-compose-override.yml</code> filesystem mounts.
Line 165: Line 153:
         "phpunit": "../../vendor/bin/phpunit tests/phpunit"
         "phpunit": "../../vendor/bin/phpunit tests/phpunit"
     }
     }
</syntaxhighlight>
</syntaxhighlight>Note about path resolution
'''In the Docker container:'''
* Extension is at <code>/var/www/html/extensions/CrawlerProtection/</code>
* MediaWiki vendor is at <code>/var/www/html/vendor/</code>
* So <code>../../vendor/bin/phpcs</code> resolves correctly
 
'''In GitHub Actions:'''


* Extension is at <code>$GITHUB_WORKSPACE/extensions/CrawlerProtection/</code>
* MediaWiki vendor is at <code>$GITHUB_WORKSPACE/vendor/</code>
* Same <code>../../vendor/bin/phpcs</code> path works
{{References}}
{{References}}

Revision as of 16:53, 26 November 2025

These notes supplement Automated deployment of MediaWiki and also PHPUnit/VSCode

I added the Gesinn IT docker compose CI system to the CrawlerProtection extension, and here is how to do it.

Initial setup

  1. create a Makefile in the project root
  2. create an .env file in the project root
  3. create a docker-compose-override.yml file in the 'build' directory

Fine-tuning

  1. add 'scripts' to composer.json
  2. add a .phpcs.xml

The neccessary files are shown below.

Results[edit]

Bottom line up front: After this is setup, you can effortlessly test and check your code from your laptop/workstation before even making a commit, much less a pull request. You can avoid the work of creating GitHub Actions on your repo fork - which you can still do as an intermediary quality filter.

# From your laptop/workstation (host)
make bash

# Inside container:
composer phpcs      # Check code style
composer phpcbf     # Fix code style
composer phpunit    # Run tests
composer test       # Run phpcs + phpunit

Makefile[edit]

do not specify COMPOSER_EXT=false or NODE_JS=false Any value will cause that code branch to run, potentially causing a build failure.

-include .env
export

# setup for docker-compose-ci build directory
ifeq (,$(wildcard ./build/))
    $(shell git submodule update --init --remote)
endif

EXTENSION=CrawlerProtection

# docker images
MW_VERSION?=1.43
PHP_VERSION?=8.2
DB_TYPE?=mysql
DB_IMAGE?="mariadb:11.2"

# composer
# Enables "composer update" inside of extension
# Leave empty/unset to disable, set to "true" to enable
COMPOSER_EXT?=

# nodejs
# Enables node.js related tests and "npm install"
# Leave empty/unset to disable, set to "true" to enable
NODE_JS?=

include build/Makefile

.env[edit]

Here's the .env file I used to set key variables[1] relevant to my extension.

# .env file example
MW_VERSION=1.43
PHP_VERSION=8.1
DB_TYPE=mysql
SMW_VERSION=6.0.1
PF_VERSION=5.7 # compatible with MW 1.43
# COMPOSER_EXT=true  # Uncomment to enable "composer update" inside extension
# NODE_JS=true  # Uncomment to enable Node.js and "npm install"

docker-compose-override.yml[edit]

This file is critical to making life easier. Without it, you would have to resort to hacks like connecting VSCode to the container using remote explorer, configuring git for 'root' and adding roots .ssh key to your github repo. That's a lot of wasted effort when you can simply mount the host directory into the container and edit/commit using VSCode as usual.

services:
  wiki:
    volumes:
      # Mount your local extension directory into the container
      # Changes in container will reflect on host and vice versa
      - ../:/var/www/html/extensions/CrawlerProtection
    ports:
      # Optional: expose wiki on localhost:8080
      - "8080:8080"

Beautiful Code[edit]

Using code beautifier to enforce MediaWiki coding standards.

If you haven't yet installed MediaWiki in the container, all the code will be there, but there will not be any LocalSettings.php file, and some tools won't work like they would on an installed wiki.

You can either cd /var/www/html and issue phpcbf commands directly rather than through the 'scripts' definition of MediaWiki's composer.json - which defines e.g. composer fix instead.

Direct usage[edit]

This is kinda long and difficult.

./vendor/bin/phpcbf -d memory_limit=512M --standard=MediaWiki --ignore=*/build/*,*/vendor/* /var/www/html/extensions/CrawlerProtection/

Or, you can create a .phpcs.xml configuration file for phpcbf that points to the vendored sniffs plus modify the extension's composer.json file to include a 'scripts' stanza that also points to MediaWiki's vendored binaries.

With the .phpcs.xml in place, you point to that as the 'standard' in your 'scripts' configuration, or on the command-line.

Fully-Configured usage[edit]

composer phpcbf

With everything in place, we should be able to make bash to get a command line in the container, and composer phpcs or composer phpcbf to run the tools. Plus, fixes in the container can be viewed/edited/committed from VSCode on the host thanks to the docker-compose-override.yml filesystem mounts.

.phpcs.xml[edit]

<?xml version="1.0"?>
<ruleset name="CrawlerProtection">
	<description>MediaWiki coding standards for CrawlerProtection extension</description>

	<rule ref="../../vendor/mediawiki/mediawiki-codesniffer/MediaWiki">
		<exclude name="MediaWiki.Commenting.FunctionComment.MissingDocumentationProtected" />
		<exclude name="MediaWiki.Commenting.FunctionComment.MissingDocumentationPublic" />
	</rule>

	<!-- Paths to check -->
	<file>.</file>

	<!-- Paths to ignore -->
	<exclude-pattern>*/build/*</exclude-pattern>
	<exclude-pattern>*/vendor/*</exclude-pattern>
	<exclude-pattern>*/node_modules/*</exclude-pattern>
	<exclude-pattern>*.phan/*</exclude-pattern>
	<exclude-pattern>*/tests/phpunit/stubs.php</exclude-pattern>
	<exclude-pattern>*/tests/phpunit/namespaced-stubs.php</exclude-pattern>

	<!-- Show progress -->
	<arg name="colors"/>
	<arg name="encoding" value="UTF-8"/>
	<arg name="extensions" value="php"/>
	<arg value="sp"/>
	
	<!-- Memory limit -->
	<ini name="memory_limit" value="512M"/>
</ruleset>

scripts[edit]

Here is the section of code to add to your Extension's composer.json to act like MediaWiki core for Continuous Integration

    "scripts": {
        "test": [
            "@phpcs",
            "@phpunit"
        ],
        "phpcs": "../../vendor/bin/phpcs -sp --standard=.phpcs.xml",
        "phpcbf": "../../vendor/bin/phpcbf --standard=.phpcs.xml",
        "phpunit": "../../vendor/bin/phpunit tests/phpunit"
    }

Note about path resolution

In the Docker container:

  • Extension is at /var/www/html/extensions/CrawlerProtection/
  • MediaWiki vendor is at /var/www/html/vendor/
  • So ../../vendor/bin/phpcs resolves correctly

In GitHub Actions:

  • Extension is at $GITHUB_WORKSPACE/extensions/CrawlerProtection/
  • MediaWiki vendor is at $GITHUB_WORKSPACE/vendor/
  • Same ../../vendor/bin/phpcs path works

References[edit]

  1. See the project README for all the variables you can use.