Skip to content

Instantly share code, notes, and snippets.

@k1sul1
Last active December 12, 2019 15:16
Show Gist options
  • Save k1sul1/745b7ee40816e12ef232bdf37ae86590 to your computer and use it in GitHub Desktop.
Save k1sul1/745b7ee40816e12ef232bdf37ae86590 to your computer and use it in GitHub Desktop.
CircleCI pipeline for WordPress projects running on Seravo/wordpress project template

CircleCI pipeline for WordPress projects running on Seravo/wordpress project template

Builds your theme, installs composer dependencies and deploys it to production / staging.

How?

Create .circleci folder in project root, and add config.yml in it. Edit the config a bit. Use search & replace to substitute $PROD_SSH_PORT to the SSH port number and $THEMENAME to the folder name of your theme.

Login to CircleCI and add a project using the menu in the sidebar. Click "Set Up Project" on the project you want to setup CI for.

Commit and push the config, and then click "Start building". Navigate to project settings. Hint: https://circleci.com/gh/$YOUR_GITHUB_USERNAME/$YOUR_REPO_NAME/edit#env-vars

Add the following environment variables:

SSHPASS=password1
TARGET=yoursite.com-p.seravo.com
USER=yoursite

Replace the values with actual credentials. The password will not be visible at any point after saving. After doing this, any subsequent pushes to master will build and deploy the site to production.

Assumptions

Only one theme. If you have multiple, you have to edit the config a bit. Same goes for any plugins you might have developed. There are no tests, but if you have some, you can easily run them in the Install WordPress section.

# Bedrock infused WordPress config file for Circle CI 2.0
# Updated from Vincit/wordpress
version: 2
jobs:
build:
working_directory: ~/repo
docker:
- image: k1sul1/circleci-wordpress:php73
environment:
- WP_TEST_URL: "http://localhost:12000"
- WP_TEST_USER: "test"
- WP_TEST_USER_PASS: "test"
- WP_ENV: "ci"
- MYSQL_ALLOW_EMPTY_PASSWORD: true,
- MYSQL_DATABASE: "circle_test"
- MYSQL_HOST: "127.0.0.1"
- DB_HOST: "127.0.0.1"
- DB_USER: "root"
- DB_PASSWORD: ""
- DB_NAME: "circle_test"
# CircleCI maintains a library of pre-built images
# documented at https://circleci.com/docs/2.0/circleci-images/
- image: circleci/mariadb:10-ram
steps:
- checkout
# Download and cache dependencies
- restore_cache:
keys:
- v1-dependencies-{{ checksum "composer.json" }}
- v1-dependencies-{{ checksum "htdocs/wp-content/themes/$THEMENAME/package.json" }}
# fallback to using the latest cache if no exact match is found
- v1-dependencies-
- run:
name: Preparations
command: |
sudo apt-get install rsync
composer install -n --prefer-dist --no-dev
- run:
name: Build
command: |
cd htdocs/wp-content/themes/$THEMENAME
npm install
# If you add `"postinstall": "npm run build"` to package.json, a build of your theme will be always created when
# `npm install` is ran. Otherwise add `npm run build` or whatever builds your theme above.
- run:
name: Install WordPress
command: |
# Get WP-cli
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
# Install WordPress
php wp-cli.phar core install \
--url=$WP_TEST_URL \
--title='Test' \
--admin_user=$WP_TEST_USER \
--admin_password=$WP_TEST_USER_PASS \
--admin_email="$WP_TEST_USER@wordpress.dev" \
--path=htdocs/wordpress
# Get the database
sshpass -e ssh -q -o StrictHostKeyChecking=no $USER@$TARGET -p $PROD_SSH_PORT 'wp db export - --single-transaction' > database.sql
mysql -h 127.0.0.1 -u root -D circle_test < database.sql
rm database.sql
# Activate all plugins
php wp-cli.phar plugin activate --all --path=htdocs/wordpress
# Get alternative router so we can do without Apache or nginx
cd ~/repo
curl -s https://raw.githubusercontent.com/Seravo/wordpress-test-template/master/lib/router.php > htdocs/router.php
cd htdocs
ls
php -S 0.0.0.0:8080 router.php &
sleep 2;
curl -i http://localhost:8080
- run:
name: Deploy
command: |
# if [ $CIRCLE_BRANCH == 'staging' ]; then
# sshpass -e rsync -av --progress --exclude='node_modules' --delete --exclude='*/wp-content/uploads/*' -e 'ssh -T -o Compression=no -o StrictHostKeyChecking=no -p $STAGING_SSH_PORT' ~/repo/ $USER@$TARGET.:/data/wordpress
# fi
if [ $CIRCLE_BRANCH == 'master' ]; then
sshpass -e rsync -av --progress --exclude='node_modules' --delete --exclude='*/wp-content/uploads/*' -e 'ssh -T -o Compression=no -o StrictHostKeyChecking=no -p $PROD_SSH_PORT' ~/repo/ $USER@$TARGET:/data/wordpress
fi
- save_cache:
key: v1-dependencies-{{ checksum "composer.json" }}
paths:
- ./vendor
- save_cache:
key: v1-dependencies-{{ checksum "htdocs/wp-content/themes/$THEMENAME/package.json" }}
paths:
- /home/circleci/repo/htdocs/wp-content/themes/$THEMENAME/node_modules
workflows:
version: 2
build-deploy:
jobs:
- build
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment