Abstracting some of the complexity of Apache virtual host setup for WordPress Travis CI tests into a re-usable script.
The need for an Apache web server
In my first post about setting up Travis CI to run all the levels of testing the Codeception and wp-browser suites allow I’ve detailed the need, when running acceptance tests, for a full blown web-server.
travis ci will scaffold a ubuntu 12.04 based machine to run php tests and apache it’s a logic choice; this is current to the post and might change in the future.
Slimming down Travis CI setup script
In a prior version of the Travis configuration file I set up the Apache web server in the before_script
itself:
sudo: required
language: php
notifications:
email: false
php:
- '5.6'
- '7.0'
services:
- mysql
cache:
directories:
- vendor
- $HOME/.composer/cache
env:
- wpDbName=test wpLoaderDbName=wploader wpDbPrefix=wp_ wpUrl=wordpress.dev wpAdminUsername=admin wpAdminPassword=admin wpSubdomain1=test1 wpSubdomain1Title="Test Subdomain 1" wpSubdomain2=test2 wpSubdomain2Title="Test Subdomain 2"
before_install:
- mysql -e "create database IF NOT EXISTS $wpDbName;" -uroot
- mysql -e "create database IF NOT EXISTS $wpLoaderDbName;" -uroot
install:
- composer update --prefer-dist
before_script:
# set up folders
- mkdir -p $HOME/tools /tmp/wordpress
# install wp-cli
- wget https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar -P /tmp/tools/
- chmod +x /tmp/tools/wp-cli.phar && mv /tmp/tools/wp-cli.phar /tmp/tools/wp
- export PATH=$PATH:/tmp/tools:vendor/bin
# download wordpress
- cd /tmp/wordpress && wp core download
# install WordPress
- wp core config --dbname=$wpDbName --dbuser=root --dbpass='' --dbhost=localhost --dbprefix=$wpDbPrefix --skip-salts
- wp core multisite-install --url=$wpUrl --base=/ --title=Test --admin_user=$wpAdminUsername --admin_password=$wpAdminPassword --admin_email=admin@$wpUrl --subdomains --skip-email
- wp site create --slug=$wpSubdomain1 --title="$wpSubdomain1Title"
- wp site create --slug=$wpSubdomain2 --title="$wpSubdomain2Title"
- wp site empty --yes
- wp plugin delete $(wp plugin list --field=name)
- wp theme activate twentysixteen && wp theme delete $(wp theme list --field=name --status=inactive)
- wp core version
# export a dump of the just installed database to the _data folder
- wp db export $TRAVIS_BUILD_DIR/tests/_data/dump.sql
# update and install apache2 packages
- sudo apt-get update
- sudo apt-get install -y --force-yes apache2 libapache2-mod-fastcgi make
- sudo apt-get install -y --force-yes php5-dev php-pear php5-mysql php5-curl php5-gd php5-json php5-sqlite php5-pgsql
- sudo a2enmod headers
# Enable php-fpm
# credit: https://www.marcus-povey.co.uk/2016/02/16/travisci-with-php-7-on-apache-php-fpm/
- if [[ ${TRAVIS_PHP_VERSION:0:3} == "7.0" ]]; then sudo cp $TRAVIS_BUILD_DIR/build/www.conf ~/.phpenv/versions/$(phpenv version-name)/etc/php-fpm.d/; fi
- sudo cp ~/.phpenv/versions/$(phpenv version-name)/etc/php-fpm.conf.default ~/.phpenv/versions/$(phpenv version-name)/etc/php-fpm.conf
- sudo a2enmod rewrite actions fastcgi alias
- sudo echo "cgi.fix_pathinfo = 1" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
- ~/.phpenv/versions/$(phpenv version-name)/sbin/php-fpm
# Configure apache virtual hosts
- sudo cp -f $TRAVIS_BUILD_DIR/build/travis-ci-apache /etc/apache2/sites-available/default
- sudo sed -e "s?%WP_DIR%?/tmp/wordpress?g" --in-place /etc/apache2/sites-available/default
- sudo sed -e "s?%WP_URL%?$wpUrl?g" --in-place /etc/apache2/sites-available/default
- echo '127.0.0.1 wordpress.dev' | sudo tee --append /etc/hosts > /dev/null
# Restart services
- sudo service apache2 restart
# Get back to Travis build dir
- cd $TRAVIS_BUILD_DIR
script:
- codecept run
I’ve since then moved the section taking care of setting up the Apache web server in a separate repository that I’m git clone
ing in the updated .travis.yml
file and using in a more readable one liner:
sudo: required
language: php
notifications:
email: false
php:
- '5.6'
- '7.0'
services:
- mysql
cache:
directories:
- vendor
- $HOME/.composer/cache
env:
- wpDbName=test wpLoaderDbName=wploader wpDbPrefix=wp_ wpUrl=wordpress.dev wpAdminUsername=admin wpAdminPassword=admin wpSubdomain1=test1 wpSubdomain1Title="Test Subdomain 1" wpSubdomain2=test2 wpSubdomain2Title="Test Subdomain 2"
before_install:
- mysql -e "create database IF NOT EXISTS $wpDbName;" -uroot
- mysql -e "create database IF NOT EXISTS $wpLoaderDbName;" -uroot
install:
- composer update --prefer-dist
before_script:
# set up folders
- mkdir -p $HOME/tools /tmp/wordpress
# install wp-cli
- wget https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar -P /tmp/tools/
- chmod +x /tmp/tools/wp-cli.phar && mv /tmp/tools/wp-cli.phar /tmp/tools/wp
- export PATH=$PATH:/tmp/tools:vendor/bin
# install Apache setup script and symlink it
- git clone https://github.com/lucatume/travis-apache-setup.git /tmp/tools/travis-apache-setup
- chmod +x /tmp/tools/travis-apache-setup/apache-setup.sh
- ln -s /tmp/tools/travis-apache-setup/apache-setup.sh /tmp/tools/apache-setup
# download wordpress
- cd /tmp/wordpress && wp core download
# install WordPress as multisite
- wp core config --dbname=$wpDbName --dbuser=root --dbpass='' --dbhost=localhost --dbprefix=$wpDbPrefix --skip-salts
- wp core multisite-install --url=$wpUrl --base=/ --title=Test --admin_user=$wpAdminUsername --admin_password=$wpAdminPassword --admin_email=admin@$wpUrl --subdomains --skip-email
- wp site create --slug=$wpSubdomain1 --title="$wpSubdomain1Title"
- wp site create --slug=$wpSubdomain2 --title="$wpSubdomain2Title"
- wp site empty --yes
- wp plugin delete $(wp plugin list --field=name)
- wp theme activate twentysixteen && wp theme delete $(wp theme list --field=name --status=inactive)
- wp core version
# export a dump of the just installed database to the _data folder
- wp db export $TRAVIS_BUILD_DIR/tests/_data/dump.sql
# set up Apache virtual host with sudo access rights
- sudo env "PATH=$PATH" apache-setup -h "127.0.0.1" -u "$wpUrl" -f "/tmp/wordpress"
# Get back to Travis build dir
- cd $TRAVIS_BUILD_DIR
script:
- codecept run
This will not be the end of it but it’s a good reusable start.
Next
I will tweak the process a little more and create a WordPress installation script to handle common operations there; after that it’s time to reuse what I’ve learnt so far to set up Travis CI for the [“I’d like this” plugin](https://github.com/lucatume/idlikethis ‘“I’d like this” plugin’).