From dc98e87cdf8dd23772ab265768dac93efaddaea6 Mon Sep 17 00:00:00 2001 From: Luke Eversfield Date: Mon, 2 Oct 2023 10:54:09 +0200 Subject: [PATCH] add docker-compose based CI add test-coverage script to composer.json Add github-action (ci.yml) that runs phpunit and phpunit-coverage --- .github/workflows/ci.yml | 101 +++++++++++++++++++++++ .gitignore | 2 + .vscode/settings.json | 7 ++ Dockerfile | 29 +++++++ Makefile | 168 +++++++++++++++++++++++++++++++++++++++ composer.json | 1 + docker-compose-ci.yml | 4 + docker-compose-dev.yml | 8 ++ docker-compose.yml | 29 +++++++ env.template | 10 +++ 10 files changed, 359 insertions(+) create mode 100644 .github/workflows/ci.yml create mode 100644 .vscode/settings.json create mode 100644 Dockerfile create mode 100644 Makefile create mode 100644 docker-compose-ci.yml create mode 100644 docker-compose-dev.yml create mode 100644 docker-compose.yml create mode 100644 env.template diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..1323617 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,101 @@ +name: CI + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + workflow_dispatch: + +jobs: + + test: + + runs-on: ubuntu-20.04 + continue-on-error: ${{ matrix.experimental }} + + strategy: + matrix: + include: + - mediawiki_version: '1.35' + smw_version: '4.1.0' + php_version: 7.4 + database_type: mysql + database_image: "mysql:5.7" + coverage: true + experimental: false + - mediawiki_version: '1.35' + smw_version: '4.1.2' + php_version: 7.4 + database_type: sqlite + database_image: "mysql:5.7" + coverage: true + experimental: false + - mediawiki_version: '1.35' + smw_version: dev-master + php_version: 7.4 + database_type: mysql + database_image: "mysql:5.7" + coverage: false + experimental: false + - mediawiki_version: '1.39' + smw_version: dev-master + php_version: 8.1 + database_type: mysql + database_image: "mysql:5.7" + coverage: false + experimental: true + - mediawiki_version: '1.39' + smw_version: dev-master + php_version: 8.1 + database_type: mysql + database_image: "mysql:8" + coverage: false + experimental: true + - mediawiki_version: '1.40' + smw_version: dev-master + php_version: 8.1 + database_type: mysql + database_image: "mysql:8" + coverage: false + experimental: true + - mediawiki_version: '1.40' + smw_version: dev-master + php_version: 8.1 + database_type: mysql + database_image: "mariadb:latest" + coverage: false + experimental: true + - mediawiki_version: '1.40' + smw_version: dev-master + php_version: 8.1 + database_type: postgres + database_image: "postgres:14" + coverage: false + experimental: true + + env: + MW_VERSION: ${{ matrix.mediawiki_version }} + SMW_VERSION: ${{ matrix.smw_version }} + PHP_VERSION: ${{ matrix.php_version }} + DB_TYPE: ${{ matrix.database_type }} + DB_IMAGE: ${{ matrix.database_image }} + + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Run tests + run: make ci + if: matrix.coverage == false + + - name: Run tests with coverage + run: make ci-coverage + if: matrix.coverage == true + + - name: Upload code coverage + uses: codecov/codecov-action@v3 + with: + files: coverage/php/coverage.xml + if: matrix.coverage == true diff --git a/.gitignore b/.gitignore index 98b092a..08b839f 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ *~ *.kate-swp .*.swp +coverage +.env \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..95e1ba7 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "workbench.colorCustomizations": { + "activityBar.background": "#173325", + "titleBar.activeBackground": "#204833", + "titleBar.activeForeground": "#F8FCFA" + } +} \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..5f64248 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,29 @@ +ARG MW_VERSION +ARG PHP_VERSION +FROM gesinn/mediawiki-ci:${MW_VERSION}-php${PHP_VERSION} + +ARG MW_VERSION +ARG SMW_VERSION +ARG PHP_VERSION + +# get needed dependencies for this extension +RUN sed -i s/80/8080/g /etc/apache2/sites-available/000-default.conf /etc/apache2/ports.conf + +RUN COMPOSER=composer.local.json composer require --no-update mediawiki/semantic-media-wiki ${SMW_VERSION} +RUN composer update + + +RUN chown -R www-data:www-data /var/www/html/extensions/SemanticMediaWiki/ + +ENV EXTENSION=SemanticCompoundQueries +COPY composer*.json package*.json /var/www/html/extensions/$EXTENSION/ + +RUN cd extensions/$EXTENSION && composer update + +COPY . /var/www/html/extensions/$EXTENSION + +RUN echo \ + "wfLoadExtension( 'SemanticMediaWiki' );\n" \ + "enableSemantics( 'localhost' );\n" \ + "wfLoadExtension( '$EXTENSION' );\n" \ + >> __setup_extension__ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b2412b3 --- /dev/null +++ b/Makefile @@ -0,0 +1,168 @@ +-include .env +export + +# ======== Naming ======== +EXTENSION := SemanticCompoundQueries +EXTENSION_FOLDER := /var/www/html/extensions/$(EXTENSION) +extension := $(shell echo $(EXTENSION) | tr A-Z a-z}) +IMAGE_NAME := $(extension):test-$(MW_VERSION)-$(SMW_VERSION)-$(PS_VERSION)-$(AL_VERSION)-$(MAPS_VERSION)-$(SRF_VERSION) + + +# ======== CI ENV Variables ======== +MW_VERSION ?= 1.35 +SMW_VERSION ?= 4.1.0 +PHP_VERSION ?= 7.4 +DB_TYPE ?= sqlite +DB_IMAGE ?= "" + + +environment = IMAGE_NAME=$(IMAGE_NAME) \ +MW_VERSION=$(MW_VERSION) \ +SMW_VERSION=$(SMW_VERSION) \ +PHP_VERSION=$(PHP_VERSION) \ +DB_TYPE=$(DB_TYPE) \ +DB_IMAGE=$(DB_IMAGE) \ +EXTENSION_FOLDER=$(EXTENSION_FOLDER) + + +ifneq (,$(wildcard ./docker-compose.override.yml)) + COMPOSE_OVERRIDE=-f docker-compose.override.yml +endif + + +compose = $(environment) docker-compose $(COMPOSE_OVERRIDE) $(COMPOSE_ARGS) +compose-ci = $(environment) docker-compose -f docker-compose.yml -f docker-compose-ci.yml $(COMPOSE_OVERRIDE) $(COMPOSE_ARGS) +compose-dev = $(environment) docker-compose -f docker-compose.yml -f docker-compose-dev.yml $(COMPOSE_OVERRIDE) $(COMPOSE_ARGS) + +compose-run = $(compose) run -T --rm +compose-exec-wiki = $(compose) exec -T wiki + +show-current-target = @echo; echo "======= $@ ========" + +# ======== CI ======== +# ======== Global Targets ======== + +.PHONY: ci +ci: install composer-test + +.PHONY: ci-coverage +ci-coverage: install composer-test-coverage + +.PHONY: install +install: destroy up .install + +.PHONY: up +up: .init .build .up + +.PHONY: down +down: .init .down + +.PHONY: destroy +destroy: .init .destroy + +.PHONY: bash +bash: up .bash + +# ======== General Docker-Compose Helper Targets ======== + +.PHONY: .build +.build: + $(show-current-target) + $(compose-ci) build wiki +.PHONY: .up +.up: + $(show-current-target) + $(compose-ci) up -d + +.PHONY: .install +.install: .wait-for-db + $(show-current-target) + $(compose-exec-wiki) bash -c "sudo -u www-data \ + php maintenance/install.php \ + --pass=wiki4everyone --server=http://localhost:8080 --scriptpath='' \ + --dbname=wiki --dbuser=wiki --dbpass=wiki $(WIKI_DB_CONFIG) wiki WikiSysop && \ + cat __setup_extension__ >> LocalSettings.php && \ + sudo -u www-data php maintenance/update.php --skip-external-dependencies --quick \ + " + +.PHONY: .down +.down: + $(show-current-target) + $(compose-ci) down + +.PHONY: .destroy +.destroy: + $(show-current-target) + $(compose-ci) down -v + +.PHONY: .bash +.bash: .init + $(show-current-target) + $(compose-exec-wiki) bash -c "cd $(EXTENSION_FOLDER) && bash" + +# ======== Test Targets ======== + +.PHONY: composer-test +composer-test: + $(show-current-target) + $(compose-exec-wiki) bash -c "cd $(EXTENSION_FOLDER) && composer phpunit" + +.PHONY: composer-test-coverage +composer-test-coverage: + $(show-current-target) + $(compose-exec-wiki) bash -c "cd $(EXTENSION_FOLDER) && composer phpunit-coverage" + +# ======== Dev Targets ======== + +.PHONY: dev-bash +dev-bash: .init + $(compose-dev) run -it wiki bash -c 'service apache2 start && bash' + +.PHONY: run +run: + $(compose-dev) -f docker-compose-dev.yml run -it wiki + +# ======== Releasing ======== +VERSION = `node -e 'console.log(require("./extension.json").version)'` + +.PHONY: release +release: ci git-push gh-login + gh release create $(VERSION) + +.PHONY: git-push +git-push: + git diff --quiet || (echo 'git directory has changes'; exit 1) + git push + +.PHONY: gh-login +gh-login: require-GH_API_TOKEN + gh config set prompt disabled + @echo $(GH_API_TOKEN) | gh auth login --with-token + +.PHONY: require-GH_API_TOKEN +require-GH_API_TOKEN: +ifndef GH_API_TOKEN + $(error GH_API_TOKEN is not set) +endif + + +# ======== Helpers ======== +.PHONY: .init +.init: + $(show-current-target) + $(eval COMPOSE_ARGS = --project-name ${extension}-$(DB_TYPE) --profile $(DB_TYPE)) +ifeq ($(DB_TYPE), sqlite) + $(eval WIKI_DB_CONFIG = --dbtype=$(DB_TYPE) --dbpath=/tmp/sqlite) +else + $(eval WIKI_DB_CONFIG = --dbtype=$(DB_TYPE) --dbserver=$(DB_TYPE) --installdbuser=root --installdbpass=database) +endif + @echo "COMPOSE_ARGS: $(COMPOSE_ARGS)" + +.PHONY: .wait-for-db +.wait-for-db: + $(show-current-target) +ifeq ($(DB_TYPE), mysql) + $(compose-run) wait-for $(DB_TYPE):3306 -t 120 +else ifeq ($(DB_TYPE), postgres) + $(compose-run) wait-for $(DB_TYPE):5432 -t 120 +endif \ No newline at end of file diff --git a/composer.json b/composer.json index 86056dc..9d8a4db 100644 --- a/composer.json +++ b/composer.json @@ -52,6 +52,7 @@ }, "scripts": { "phpunit": "php ../../tests/phpunit/phpunit.php -c phpunit.xml.dist", + "phpunit-coverage": "php ../../tests/phpunit/phpunit.php -c phpunit.xml.dist --testdox --coverage-text --coverage-html coverage/php --coverage-clover coverage/php/coverage.xml", "unit": [ "composer dump-autoload", "php ../../tests/phpunit/phpunit.php -c phpunit.xml.dist --testsuite=semantic-compound-queries-unit" diff --git a/docker-compose-ci.yml b/docker-compose-ci.yml new file mode 100644 index 0000000..9f4a0e0 --- /dev/null +++ b/docker-compose-ci.yml @@ -0,0 +1,4 @@ +services: + wiki: + volumes: + - ./coverage:${EXTENSION_FOLDER}/coverage diff --git a/docker-compose-dev.yml b/docker-compose-dev.yml new file mode 100644 index 0000000..e09dcb6 --- /dev/null +++ b/docker-compose-dev.yml @@ -0,0 +1,8 @@ +services: + wiki: + volumes: + - ./:${EXTENSION_FOLDER} + - ${EXTENSION_FOLDER}/vendor/ + ports: + - 8080:8080 + \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..06eeeee --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,29 @@ +services: + wiki: + build: + context: . + args: + MW_VERSION: ${MW_VERSION} + SMW_VERSION: ${SMW_VERSION} + PHP_VERSION: ${PHP_VERSION} + image: ${IMAGE_NAME} + + mysql: + image: ${DB_IMAGE:-mysql:5} + environment: + - MYSQL_ROOT_PASSWORD=database + profiles: + - mysql + + postgres: + image: ${DB_IMAGE:-postgres:15} + environment: + - POSTGRES_USER=root + - POSTGRES_PASSWORD=database + profiles: + - postgres + + wait-for: + image: mintel/docker-wait-for-it + profiles: + - no-up diff --git a/env.template b/env.template new file mode 100644 index 0000000..af0b7c6 --- /dev/null +++ b/env.template @@ -0,0 +1,10 @@ +# copy this to .env to easily modify local CI ENV variables + +# cp env.template .env + +MW_VERSION=1.35 +SMW_VERSION=4.1.0 + +PHP_VERSION=7.4 +DB_TYPE=mysql +DB_IMAGE="mysql:5.7" \ No newline at end of file