I'm trying to develop a WP plugin the correct way, using Gitlab pipeline to automate tests and deploy to custom package registry to be able to install it via composer and composer/installers
plugin (last 2 steps already accomplished).
Followed this official guide on plugin development, and locally everything is fine: used a bunch of docker container for php, mysql and everything needed and had no problem.
Trying to set up testing automation via pipeline I'm getting errors on running mysql command: I set up the job to use a mysql service container for the db, but running the install-wp-tests.sh
scripts always end up with both a mysql: command not found
followed by a mysqladmin: command not found
errors, as if the script is not trying to run these commands on the mysql service container but on the job container, thus not finding the commands.
Pipeline job
The usage of composer:2
image is a copy-paste leftover from the publishing job, but switching to php:8
lead to an svn: command not found
error even earlier in the script, so for now I'll stick with it:
tests:
stage: test
image: composer:2
services:
- name: mysql:8
alias: database
variables:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: wordpress_test
before_script:
- chmod +x bin/install-wp-tests.sh
- bin/install-wp-tests.sh wordpress_test root root database 6.2
script:
- ./vendor/bin/phpunit tests
Job output:
Running with gitlab-runner 15.9.0~beta.115.g598a7c91 (598a7c91)
on blue-5.shared.runners-manager.gitlab.com/default -AzERasQ, system ID: s_8a38c517a741
feature flags: FF_USE_IMPROVED_URL_MASKING:true
Preparing the "docker+machine" executor
Using Docker executor with image composer:2 ...
Starting service mysql:8 ...
Pulling docker image mysql:8 ...
Using docker image sha256:8189e588b0e8fcc95b0d764d6f7bdb55b5b41e9249157177d73781058f603ca9 for mysql:8 with digest mysql@sha256:a43f6e7e7f3a5e5b90f857fbed4e3103ece771b19f0f75880f767cf66bbb6577 ...
Waiting for services to be up and running (timeout 30 seconds)...
Pulling docker image composer:2 ...
Using docker image sha256:e76b3170123b906a562a32e2910b77ae06078a548e81a5c5dd357b8e276cf705 for composer:2 with digest composer@sha256:2cf189b2623d0e84bb648512464ae68397fa153d0c28a7fa53ec686915e6f434 ...
Preparing environment
Running on runner--azerasq-project-45500673-concurrent-0 via runner-azerasq-shared-1682586171-5d8c21ef...
Getting source from Git repository
00:01
$ eval "$CI_PRE_CLONE_SCRIPT"
Fetching changes with git depth set to 20...
Initialized empty Git repository in /builds/zerouno-srl/sharewood/plugins/api/.git/
Created fresh repository.
Checking out e4ba9ee7 as detached HEAD (ref is refs/merge-requests/4/head)...
Skipping Git submodules setup
Restoring cache
00:02
Checking cache for 0_composer-ac55ec2254802eaa965143a0698870f731efb91c-non_protected...
Downloading cache.zip from https://storage.googleapis.com/gitlab-com-runners-cache/project/45500673/0_composer-ac55ec2254802eaa965143a0698870f731efb91c-non_protected
Successfully extracted cache
Executing "step_script" stage of the job script
Using docker image sha256:e76b3170123b906a562a32e2910b77ae06078a548e81a5c5dd357b8e276cf705 for composer:2 with digest composer@sha256:2cf189b2623d0e84bb648512464ae68397fa153d0c28a7fa53ec686915e6f434 ...
$ chmod +x bin/install-wp-tests.sh
$ bin/install-wp-tests.sh wordpress_test root root database 6.2
+ install_wp
+ '[' -d /tmp/wordpress ']'
+ mkdir -p /tmp/wordpress
+ [[ 6.2 == \n\i\g\h\t\l\y ]]
+ [[ 6.2 == \t\r\u\n\k ]]
+ '[' 6.2 == latest ']'
+ [[ 6.2 =~ [0-9]+\.[0-9]+ ]]
+ download https://api.wordpress.org/core/version-check/1.7/ /tmp/wp-latest.json
++ which curl
+ '[' /usr/bin/curl ']'
+ curl -s https://api.wordpress.org/core/version-check/1.7/
+ [[ 6.2 =~ [0-9]+\.[0-9]+\.[0] ]]
++ sed 's/\./\\./g'
++ echo 6.2
+ local 'VERSION_ESCAPED=6\.2'
++ head -1
++ sed 's/"version":"//'
++ grep -o '"version":"6\.2[^"]*' /tmp/wp-latest.json
+ LATEST_VERSION=6.2
+ [[ -z 6.2 ]]
+ local ARCHIVE_NAME=wordpress-6.2
+ download https://wordpress.org/wordpress-6.2.tar.gz /tmp/wordpress.tar.gz
++ which curl
+ '[' /usr/bin/curl ']'
+ curl -s https://wordpress.org/wordpress-6.2.tar.gz
+ tar --strip-components=1 -zxmf /tmp/wordpress.tar.gz -C /tmp/wordpress
+ download https://raw.github.com/markoheijnen/wp-mysqli/master/db.php /tmp/wordpress/wp-content/db.php
++ which curl
+ '[' /usr/bin/curl ']'
+ curl -s https://raw.github.com/markoheijnen/wp-mysqli/master/db.php
+ install_test_suite
++ uname -s
+ [[ Linux == \D\a\r\w\i\n ]]
+ local ioption=-i
+ '[' '!' -d /tmp/wordpress-tests-lib ']'
+ mkdir -p /tmp/wordpress-tests-lib
+ rm -rf /tmp/wordpress-tests-lib/includes /tmp/wordpress-tests-lib/data
+ svn export --quiet --ignore-externals https://develop.svn.wordpress.org/branches/6.2/tests/phpunit/includes/ /tmp/wordpress-tests-lib/includes
+ svn export --quiet --ignore-externals https://develop.svn.wordpress.org/branches/6.2/tests/phpunit/data/ /tmp/wordpress-tests-lib/data
+ '[' '!' -f wp-tests-config.php ']'
+ download https://develop.svn.wordpress.org/branches/6.2/wp-tests-config-sample.php /tmp/wordpress-tests-lib/wp-tests-config.php
++ which curl
+ '[' /usr/bin/curl ']'
+ curl -s https://develop.svn.wordpress.org/branches/6.2/wp-tests-config-sample.php
++ sed 's:/\+$::'
++ echo /tmp/wordpress
+ WP_CORE_DIR=/tmp/wordpress
+ sed -i 's:dirname( __FILE__ ) . '\''/src/'\'':'\''/tmp/wordpress/'\'':' /tmp/wordpress-tests-lib/wp-tests-config.php
+ sed -i 's:__DIR__ . '\''/src/'\'':'\''/tmp/wordpress/'\'':' /tmp/wordpress-tests-lib/wp-tests-config.php
+ sed -i s/youremptytestdbnamehere/wordpress_test/ /tmp/wordpress-tests-lib/wp-tests-config.php
+ sed -i s/yourusernamehere/root/ /tmp/wordpress-tests-lib/wp-tests-config.php
+ sed -i s/yourpasswordhere/root/ /tmp/wordpress-tests-lib/wp-tests-config.php
+ sed -i 's|localhost|database|' /tmp/wordpress-tests-lib/wp-tests-config.php
+ install_db
+ '[' false = true ']'
+ PARTS=('database')
+ local PARTS
+ local DB_HOSTNAME=database
+ local DB_SOCK_OR_PORT=
+ local EXTRA=
+ '[' -z database ']'
++ grep -e '^[0-9]\{1,\}$'
++ echo
+ '[' ']'
+ '[' -z ']'
+ '[' -z database ']'
+ EXTRA=' --host=database --protocol=tcp'
++ grep '^wordpress_test$'
++ mysql --user=root --password=root --host=database --protocol=tcp '--execute=show databases;'
bin/install-wp-tests.sh: line 169: mysql: command not found
+ '[' ']'
+ create_db
+ mysqladmin create wordpress_test --user=root --password=root --host=database --protocol=tcp
bin/install-wp-tests.sh: line 143: mysqladmin: command not found
Cleaning up project directory and file based variables
00:01
ERROR: Job failed: exit code 1
Conclusions
I tried to run the testing job with
mysql:8
mysql:8.0
mysql:8.0.33
mysql:8-debian
mysql-server:8.0
but none worked correctly, so I'm not sure if the problem is either
- the script being bugged and not executing commands in the service container, but running locally with containers is ok
- my job configuration being wrong, but checking on the gitlab services docs seems I'm doing right
Update #1
Ok, find out I had to install mysql-client
and set default-authentication-plugin
flag, so I added a command to the mysql service container and the before_script
job, now it looks like this:
tests:
stage: test
image: composer:2
services:
- name: mysql:8
alias: database
command:
- mysqld --default-authentication-plugin=mysql_native_password
variables:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: wordpress_test
before_script:
- apk update && apk add mysql-client
- chmod +x bin/install-wp-tests.sh
- bin/install-wp-tests.sh wordpress_test root root database 6.2
script:
- ./vendor/bin/phpunit tests
But now the job fails with an explicit error on not being able to reach database
server host:
...
+ install_db
+ '[' false = true ']'
+ PARTS=('database')
+ local PARTS
+ local DB_HOSTNAME=database
+ local DB_SOCK_OR_PORT=
+ local EXTRA=
+ '[' -z database ']'
++ grep -e '^[0-9]\{1,\}$'
++ echo
+ '[' ']'
+ '[' -z ']'
+ '[' -z database ']'
+ EXTRA=' --host=database --protocol=tcp'
++ grep '^wordpress_test$'
++ mysql --user=root --password=root --host=database --protocol=tcp '--execute=show databases;'
ERROR 2005 (HY000): Unknown server host 'database' (-2)
+ '[' ']'
+ create_db
+ mysqladmin create wordpress_test --user=root --password=root --host=database --protocol=tcp
mysqladmin: connect to server at 'database' failed
error: 'Unknown server host 'database' (-2)'
Check that mariadbd is running on database and that the port is 3306.
You can check this by doing 'telnet database 3306'
Cleaning up project directory and file based variables
00:01
ERROR: Job failed: exit code 1