Skip to content

Instantly share code, notes, and snippets.

@Prophe1
Last active August 18, 2022 14:50
Show Gist options
  • Save Prophe1/5c1f7ce79a27a5e0eb9d43512a573111 to your computer and use it in GitHub Desktop.
Save Prophe1/5c1f7ce79a27a5e0eb9d43512a573111 to your computer and use it in GitHub Desktop.
CircleCI + WPEngine + SAGE
# Continuous Integration to a WP Engine install
# PHP CircleCI 2.0 configuration file
#
# You need to generate and add SSH key to CircleCI project SSH Permissions tab
# And then copy fingerprint and replace YOUR_SSH_KEY with it. Also same Key must be added to WPEngine's project SSH tab.
# After, You need to add environtmen varriables WP_THEME_DIR - theme directory and WPE_INSTALL -
# WPEngine install name/slug. You need to have .gitignores folder with two files (__default and __production)
# __default means that it will be used in local develoment, and __production will be used when pushing to WPEngine
version: 2
jobs:
# Compile any needed files and deploy to the WP Engine install
build_and_deploy:
docker:
# Use the PHP 7.2 container (with Node) since it's the best match to the WP Engine environment
- image: circleci/php:7.2-apache-node-browsers
steps:
# Check out the git repo open theme folder
- checkout
- add_ssh_keys:
fingerprints:
- "YOUR_SSH_KEY"
# Copy package and composer for one time use
- run:
name: Copy package.json and composer.js for one time use
command: cp ${WP_THEME_DIR}package.json package.json && cp ${WP_THEME_DIR}composer.json composer.json
# Checkout our cache (keyed to our composer and package JSON files)
- restore_cache:
keys:
- v1-dependencies-{{ checksum "package.json" }}-{{ checksum "composer.json" }}
- v1-dependencies-
# Install Node dependencies (uses Yarn)
- run:
name: Install Node Dependencies
command: cd ${WP_THEME_DIR} && yarn
# Install PHP dependencies (uses Composer)
- run:
name: Install PHP Dependencies
command: cd ${WP_THEME_DIR} && composer install --no-ansi --no-dev --no-interaction --optimize-autoloader --no-progress
# Save our build files to our cache
- save_cache:
paths:
- ${WP_THEME_DIR}./node_modules
- ${WP_THEME_DIR}./vendor
key: v1-dependencies-{{ checksum "package.json" }}-{{ checksum "composer.json" }}
# Remove temporary package and composer files
- run:
name: Remove temporary package.json and composer.js files
command: rm package.json composer.json
# Run the build script defined in our package.json file
- run:
name: Build compiled files
command: cd ${WP_THEME_DIR} && yarn build
# Run the build script defined in our package.json file
- run:
name: Build compiled files
command: cd ${WP_THEME_DIR} && yarn build:production
# Add the git.wpengine.com fingerprint to our known hosts
# We need to interact with this server. And the unknown host will trigger an interactive prompt.
# The workaround is to manually add the fingerprint ourselves.
# Note: This will need updated if and when WP Engine updates the fingerprint
- run:
name: Add deploy host to known_hosts
command: echo 'git.wpengine.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEApRVAUwjz49VKfuENfyv52Dvh3qx9nWW/3Gb7R9pwABXUNQqkipt3aB7w2W6jOaEGFmzSr/4qhstUv0lvbeZu/1uRU/b6WrqULu+9bAdt9ll09QULfMxAIFWDwDS1F6GEZT+Yau/wLUI2VTZppxSVRIPe20/mxgXk8/Q9ha5tCaz+dQZ9lHWwk9rbDF+7LSVomLGM3e9dwr6mS4p37Qkje2cFJBqQcQ+RqEOTOD/xiFU0DH8TWO4R5yibQ0KEZVACkwhaAZSl81F7YZrrLEfsFS/llgpV3YZHQGvFi0x/ELAUJMFE9umdy9EwFF7/lTpV8zOGdiLW+v8svweWJJJ00w==' >> ~/.ssh/known_hosts
# Create our WPE_ENV variable based on whether we are on the master or staging branch
# - The master branch will deploy to production
# - The staging branch will deploy to staging
# Set up the git remotes for WP Engine
- run:
name: Set up the WP Engine install git remotes
command: |
export WPE_ENV=$([ "${CIRCLE_BRANCH}" == "master" ] && echo "production" || echo "staging")
git config --global user.email "circleci@circleci.com"
git config --global user.name "CircleCI"
git remote add wpe git@git.wpengine.com:${WPE_ENV}/${WPE_INSTALL}.git
git fetch wpe
# Swap out our gitignore files, commit the build files, and push to the install
- run:
name: Commit build files and push to WPE remote
command: |
git checkout -b ${CIRCLE_BRANCH}-${CIRCLE_BUILD_NUM}
unlink .gitignore
ln -s .gitignores/__production .gitignore
git add .
git commit -m "Deployment commit"
git push wpe ${CIRCLE_BRANCH}-${CIRCLE_BUILD_NUM}
git push wpe :${CIRCLE_BRANCH}-${CIRCLE_BUILD_NUM}
workflows:
version: 2
build:
jobs:
# Deploy the code. This only fires on master and staging
- build_and_deploy:
filters:
branches:
only:
- master
- staging
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment