From 2fb3c32003bfd10569850a5660ca53d107ab8261 Mon Sep 17 00:00:00 2001 From: Jason Bahl Date: Mon, 19 Jul 2021 11:17:06 -0600 Subject: [PATCH] - update docker files --- bin/install-test-env.sh | 1 - docker-compose.yml | 2 +- docker/app.Dockerfile | 22 ++++++---- docker/app.entrypoint.sh | 85 ++---------------------------------- docker/app.post-setup.sh | 15 +++++++ docker/app.setup.sh | 42 ++++++++++++++++++ docker/testing.Dockerfile | 15 +++---- docker/testing.entrypoint.sh | 38 +++++++++++----- 8 files changed, 109 insertions(+), 111 deletions(-) create mode 100644 docker/app.post-setup.sh create mode 100644 docker/app.setup.sh diff --git a/bin/install-test-env.sh b/bin/install-test-env.sh index 2892aa7..495dbbd 100644 --- a/bin/install-test-env.sh +++ b/bin/install-test-env.sh @@ -161,7 +161,6 @@ install_acf_pro() { wp plugin activate advanced-custom-fields-pro } - setup_plugin() { # Add this repo as a plugin to the repo if [ ! -d $WP_CORE_DIR/wp-content/plugins/wp-graphql-acf ]; then diff --git a/docker-compose.yml b/docker-compose.yml index 03ea775..877582b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -29,7 +29,7 @@ services: local: app_db: - image: mysql:5.7 + image: mariadb:10.2 environment: MYSQL_ROOT_PASSWORD: root MYSQL_DATABASE: wordpress diff --git a/docker/app.Dockerfile b/docker/app.Dockerfile index 4e0872e..3d03c66 100644 --- a/docker/app.Dockerfile +++ b/docker/app.Dockerfile @@ -1,12 +1,19 @@ ############################################################################### -# Pre-configured WordPress Installation w/ WPGraphQL, WPGraphQL for ACF, ACF Pro # +# Pre-configured WordPress Installation w/ WPGraphQL, WPGatsby # # For testing only, use in production not recommended. # ############################################################################### + +# Use build args to get the right wordpress + php image ARG WP_VERSION ARG PHP_VERSION FROM wordpress:${WP_VERSION}-php${PHP_VERSION}-apache +# Needed to specify the build args again after the FROM command. +ARG WP_VERSION +ARG PHP_VERSION + +# Save the build args for use by the runtime environment ENV WP_VERSION=${WP_VERSION} ENV PHP_VERSION=${PHP_VERSION} @@ -39,13 +46,9 @@ RUN curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli # Set project environmental variables ENV WP_ROOT_FOLDER="/var/www/html" -ENV WORDPRESS_DB_HOST=${DB_HOST} -ENV WORDPRESS_DB_USER=${DB_USER} -ENV WORDPRESS_DB_PASSWORD=${DB_PASSWORD} -ENV WORDPRESS_DB_NAME=${DB_NAME} ENV PLUGINS_DIR="${WP_ROOT_FOLDER}/wp-content/plugins" -ENV PROJECT_DIR="${PLUGINS_DIR}/wp-graphql-acf" -ENV WPGRAPHQL_VERSION="${WPGRAPHQL_VERSION}" +ENV PROJECT_DIR="${PLUGINS_DIR}/wp-graphql" +ENV DATA_DUMP_DIR="${PROJECT_DIR}/tests/_data" # Remove exec statement from base entrypoint script. RUN sed -i '$d' /usr/local/bin/docker-entrypoint.sh @@ -66,13 +69,16 @@ RUN echo "Installing XDebug 3 (in disabled state)" \ && echo "xdebug.start_with_request=yes" >> /usr/local/etc/php/conf.d/disabled/docker-php-ext-xdebug.ini \ && echo "xdebug.client_host=host.docker.internal" >> /usr/local/etc/php/conf.d/disabled/docker-php-ext-xdebug.ini \ && echo "xdebug.client_port=9003" >> /usr/local/etc/php/conf.d/disabled/docker-php-ext-xdebug.ini \ + && echo "xdebug.max_nesting_level=512" >> /usr/local/etc/php/conf.d/disabled/docker-php-ext-xdebug.ini \ ; # Set xdebug configuration off by default. See the entrypoint.sh. -ENV USING_XDEBUG=${USING_XDEBUG} +ENV USING_XDEBUG=0 # Set up entrypoint WORKDIR /var/www/html +COPY docker/app.setup.sh /usr/local/bin/app-setup.sh +COPY docker/app.post-setup.sh /usr/local/bin/app-post-setup.sh COPY docker/app.entrypoint.sh /usr/local/bin/app-entrypoint.sh RUN chmod 755 /usr/local/bin/app-entrypoint.sh ENTRYPOINT ["app-entrypoint.sh"] diff --git a/docker/app.entrypoint.sh b/docker/app.entrypoint.sh index 1a06d4c..3af289a 100644 --- a/docker/app.entrypoint.sh +++ b/docker/app.entrypoint.sh @@ -1,86 +1,7 @@ #!/bin/bash -if [ "$USING_XDEBUG" == "1" ]; then - echo "Enabling XDebug 3" - mv /usr/local/etc/php/conf.d/disabled/docker-php-ext-xdebug.ini /usr/local/etc/php/conf.d/ -fi - -# Run WordPress docker entrypoint. -. docker-entrypoint.sh 'apache2' - -set +u - -# Ensure mysql is loaded -dockerize -wait tcp://${DB_HOST}:${DB_HOST_PORT:-3306} -timeout 1m - -# Config WordPress -if [ ! -f "${WP_ROOT_FOLDER}/wp-config.php" ]; then - wp config create \ - --path="${WP_ROOT_FOLDER}" \ - --dbname="${DB_NAME}" \ - --dbuser="${DB_USER}" \ - --dbpass="${DB_PASSWORD}" \ - --dbhost="${DB_HOST}" \ - --dbprefix="${WP_TABLE_PREFIX}" \ - --skip-check \ - --quiet \ - --allow-root -fi - -# Install WP if not yet installed -if ! $( wp core is-installed --allow-root ); then - wp core install \ - --path="${WP_ROOT_FOLDER}" \ - --url="${WP_URL}" \ - --title='Test' \ - --admin_user="${ADMIN_USERNAME}" \ - --admin_password="${ADMIN_PASSWORD}" \ - --admin_email="${ADMIN_EMAIL}" \ - --allow-root -fi - -# Install and activate WPGraphQL - -echo "wpgraphql version... ${WPGRAPHQL_VERSION}" -echo "${PLUGINS_DIR}" - -if [ ! -f "${PLUGINS_DIR}/wp-graphql/wp-graphql.php" ]; then - # WPGRAPHQL_VERSION in format like v1.2.3 - echo ${WPGRAPHQL_VERSION} - if [[ -z ${WPGRAPHQL_VERSION} ]]; then - echo "installing latest WPGraphQL from WordPress.org" - wp plugin install wp-graphql --activate --allow-root - else - echo "Installing WPGraphQL from Github" - git clone https://github.com/wp-graphql/wp-graphql.git "${PLUGINS_DIR}/wp-graphql" - cd "${PLUGINS_DIR}/wp-graphql" - echo "checking out WPGraphQL tag/${WPGRAPHQL_VERSION}" - git checkout tags/${WPGRAPHQL_VERSION} -b master - composer install --no-dev - cd ${WP_ROOT_FOLDER} - echo "activating WPGraphQL" - wp plugin activate wp-graphql --allow-root - wp plugin list --allow-root - fi -else - wp plugin activate wp-graphql --allow-root -fi - -# Install and activate ACF Pro -if [ ! -f "${PLUGINS_DIR}/advanced-custom-fields-pro/acf.php" ]; then - wp plugin install \ - https://github.com/wp-premium/advanced-custom-fields-pro/archive/master.zip \ - --activate --allow-root -else - wp plugin activate advanced-custom-fields-pro --allow-root -fi - -# Install and activate WPGatsby -wp plugin activate wp-graphql-acf --allow-root - -# Set pretty permalinks. -wp rewrite structure '/%year%/%monthnum%/%postname%/' --allow-root - -wp db export "${PROJECT_DIR}/tests/_data/dump.sql" --allow-root +# Run app setup script. +. app-setup.sh +. app-post-setup.sh exec "$@" diff --git a/docker/app.post-setup.sh b/docker/app.post-setup.sh new file mode 100644 index 0000000..f577bd8 --- /dev/null +++ b/docker/app.post-setup.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +# Activate wp-graphql +wp plugin activate wp-graphql --allow-root + +# Set pretty permalinks. +wp rewrite structure '/%year%/%monthnum%/%postname%/' --allow-root + +wp db export "${DATA_DUMP_DIR}/dump.sql" --allow-root + +# If maintenance mode is active, de-activate it +if $( wp maintenance-mode is-active --allow-root ); then + echo "Deactivating maintenance mode" + wp maintenance-mode deactivate --allow-root +fi diff --git a/docker/app.setup.sh b/docker/app.setup.sh new file mode 100644 index 0000000..93b0c9f --- /dev/null +++ b/docker/app.setup.sh @@ -0,0 +1,42 @@ +#!/bin/bash + +if [ "$USING_XDEBUG" == "1" ]; then + echo "Enabling XDebug 3" + mv /usr/local/etc/php/conf.d/disabled/docker-php-ext-xdebug.ini /usr/local/etc/php/conf.d/ +fi + +# Run WordPress docker entrypoint. +. docker-entrypoint.sh 'apache2' + +set +u + +# Ensure mysql is loaded +dockerize -wait tcp://${DB_HOST}:${DB_HOST_PORT:-3306} -timeout 1m + +# Config WordPress +if [ ! -f "${WP_ROOT_FOLDER}/wp-config.php" ]; then + wp config create \ + --path="${WP_ROOT_FOLDER}" \ + --dbname="${DB_NAME}" \ + --dbuser="${DB_USER}" \ + --dbpass="${DB_PASSWORD}" \ + --dbhost="${DB_HOST}" \ + --dbprefix="${WP_TABLE_PREFIX}" \ + --skip-check \ + --quiet \ + --allow-root +fi + +# Install WP if not yet installed +if ! $( wp core is-installed --allow-root ); then + wp core install \ + --path="${WP_ROOT_FOLDER}" \ + --url="${WP_URL}" \ + --title='Test' \ + --admin_user="${ADMIN_USERNAME}" \ + --admin_password="${ADMIN_PASSWORD}" \ + --admin_email="${ADMIN_EMAIL}" \ + --allow-root +fi + +echo "Running WordPress version: $(wp core version --allow-root) at $(wp option get home --allow-root)" diff --git a/docker/testing.Dockerfile b/docker/testing.Dockerfile index cdec274..891c22f 100644 --- a/docker/testing.Dockerfile +++ b/docker/testing.Dockerfile @@ -2,8 +2,10 @@ # Container for running Codeception tests on a WPGraphQL Docker instance. # ############################################################################ -# Using the 'DESIRED_' prefix to avoid confusion with environment variables of the same name. -FROM wpgraphql-acf-app:latest +ARG WP_VERSION +ARG PHP_VERSION + +FROM wp-graphql:latest-wp${WP_VERSION}-php${PHP_VERSION} LABEL author=jasonbahl LABEL author_uri=https://github.com/jasonbahl @@ -18,8 +20,8 @@ RUN docker-php-ext-install pdo_mysql RUN apt-get install zip unzip -y \ && pecl install pcov -ENV COVERAGE=0 -ENV SUITES=${SUITES:-zz} +ENV COVERAGE= +ENV SUITES=${SUITES:-} # Install composer ENV COMPOSER_ALLOW_SUPERUSER=1 @@ -34,11 +36,8 @@ ENV PATH "$PATH:~/.composer/vendor/bin" # Configure php RUN echo "date.timezone = UTC" >> /usr/local/etc/php/php.ini -# Remove exec statement from base entrypoint script. -RUN sed -i '$d' /usr/local/bin/app-entrypoint.sh - # Set up entrypoint -WORKDIR /var/www/html/wp-content/plugins/wp-graphql-acf +WORKDIR /var/www/html COPY docker/testing.entrypoint.sh /usr/local/bin/testing-entrypoint.sh RUN chmod 755 /usr/local/bin/testing-entrypoint.sh ENTRYPOINT ["testing-entrypoint.sh"] diff --git a/docker/testing.entrypoint.sh b/docker/testing.entrypoint.sh index d812b6b..9bd316e 100644 --- a/docker/testing.entrypoint.sh +++ b/docker/testing.entrypoint.sh @@ -1,5 +1,7 @@ #!/bin/bash +echo "WordPress: ${WP_VERSION} PHP: ${PHP_VERSION}" + # Processes parameters and runs Codeception. run_tests() { if [[ -n "$COVERAGE" ]]; then @@ -15,10 +17,16 @@ run_tests() { exit 1 fi - for suite in $suites ; do - echo "Running Test Suite $suite" - vendor/bin/codecept run -c codeception.dist.yml ${suite} ${coverage:-} ${debug:-} --no-exit - done + # If maintenance mode is active, de-activate it + if $( wp maintenance-mode is-active --allow-root ); then + echo "Deactivating maintenance mode" + wp maintenance-mode deactivate --allow-root + fi + + + # Suites is the comma separated list of suites/tests to run. + echo "Running Test Suite $suites" + vendor/bin/codecept run -c codeception.dist.yml "${suites}" ${coverage:-} ${debug:-} --no-exit } # Exits with a status of 0 (true) if provided version number is higher than proceeding numbers. @@ -40,17 +48,25 @@ RewriteRule . /index.php [L] # Move to WordPress root folder workdir="$PWD" -echo "Moving to WordPress root directory." +echo "Moving to WordPress root directory ${WP_ROOT_FOLDER}." cd ${WP_ROOT_FOLDER} -# Run app entrypoint script. -. app-entrypoint.sh +# Because we are starting apache independetly of the docker image, +# we set WORDPRESS environment variables so apache see them and used in the wp-config.php +echo "export WORDPRESS_DB_HOST=${WORDPRESS_DB_HOST}" >> /etc/apache2/envvars +echo "export WORDPRESS_DB_USER=${WORDPRESS_DB_USER}" >> /etc/apache2/envvars +echo "export WORDPRESS_DB_PASSWORD=${WORDPRESS_DB_PASSWORD}" >> /etc/apache2/envvars +echo "export WORDPRESS_DB_NAME=${WORDPRESS_DB_NAME}" >> /etc/apache2/envvars + +# Run app setup scripts. +. app-setup.sh +. app-post-setup.sh write_htaccess # Return to PWD. -echo "Moving back to project working directory." -cd ${workdir} +echo "Moving back to project working directory ${PROJECT_DIR}" +cd ${PROJECT_DIR} # Ensure Apache is running service apache2 start @@ -78,8 +94,8 @@ if version_gt $PHP_VERSION 7.0 && [[ -n "$COVERAGE" ]] && [[ -z "$USING_XDEBUG" echo "Using pcov/clobber for codecoverage" docker-php-ext-enable pcov echo "pcov.enabled=1" >> /usr/local/etc/php/conf.d/docker-php-ext-pcov.ini - echo "pcov.directory = /var/www/html/wp-content/plugins/wp-graphql" >> /usr/local/etc/php/conf.d/docker-php-ext-pcov.ini - COMPOSER_MEMORY_LIMIT=-1 composer require --dev pcov/clobber + echo "pcov.directory = ${PROJECT_DIR}" >> /usr/local/etc/php/conf.d/docker-php-ext-pcov.ini + COMPOSER_MEMORY_LIMIT=-1 composer require pcov/clobber --dev vendor/bin/pcov clobber elif [[ -n "$COVERAGE" ]] && [[ -n "$USING_XDEBUG" ]]; then echo "Using XDebug for codecoverage"