Skip to content

Instantly share code, notes, and snippets.

@dgomesbr
Last active February 24, 2019 15:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dgomesbr/5f50a453c6e4ff14a2bb1d4d58a2e452 to your computer and use it in GitHub Desktop.
Save dgomesbr/5f50a453c6e4ff14a2bb1d4d58a2e452 to your computer and use it in GitHub Desktop.
WP2Static S3 + Cloudfront invalidation
#!/bin/bash
#######################################################################################
#
# On WP2Static, targetdir = /opt/bitnami/wordpress/wp-content/static-version/deploy
# the script is executed outside the deploy dir in case you need to do any transfomations
# before the actual sync.
#
# Earlier version contained for example, hash of the deploy dir and its files for
# comparison, but since we're using s3 sync, no need to keep that.
# You can see that in revisions;
#
# NOTE: Since I'm using Wordpress inside bitnami containers, the Volumes are persisted
# on a folder outside the docker container, so as long we export the static version to
# anywhere /opt/bitnami/wordpress/wp-content, this will be visible by the host OS.
# see docker-compose.yml bellow for more info.
#
#######################################################################################
## Variables
INITIAL_DIR=$(pwd)
## Please configure
AWS_CF_DISTRO_ID=<Cloudfront distro id>
AWS_PROFILE_IN_USE=<your aws profile, if you don't use profiles, just fill with default>
AWS_S3_BUCKET_NAME=<BUCKETNAME>
cd <YOUR EXPORT DIR, ONE LEVEL BEFORE deploy> >/dev/null
## call docker wpcli to generate the static version copy
echo "Generating static site archive ..."
docker exec -ti $(docker ps | grep wordpress | cut -d' ' -f1) wp wp2static generate --allow-root
echo "Updating AWS version"
# sync from local
# * --acl > For public access to the new files, you can leave private and accessible only from CF, good for testing
# * --storage-class=ONEZONE_IA > Cheapest, since the source of truth is our wordpress blog, we don't care about durability
# * --delete > Will delete remote files that are not in local folder (eliminates the need of a rm before sync)
aws s3 sync deploy/ s3://$AWS_S3_BUCKET_NAME --acl bucket-owner-full-control --acl public-read --profile=$AWS_PROFILE_IN_USE --storage-class=ONEZONE_IA --delete>/dev/null
# update the CF distro
# * --paths '/*' will update the whole CF content
aws cloudfront create-invalidation --distribution-id $AWS_CF_DISTRO_ID --paths '/*' --profile=$AWS_PROFILE_IN_USE>/dev/null
# return to the original dir
cd $INITIAL_DIR >/dev/null
echo "Update finished!"
exit
version: '2'
services:
mariadb:
image: 'bitnami/mariadb:latest'
mem_limit: 1G
volumes:
- /home/dev/diegomagalhaes.com/docker/volumes/mariadb:/bitnami
environment:
- MARIADB_USER=bn_wordpress
- MARIADB_DATABASE=bitnami_wordpress
- ALLOW_EMPTY_PASSWORD=yes
wordpress:
image: 'bitnami/wordpress:latest'
mem_limit: 2G
labels:
kompose.service.type: nodeport
ports:
- '80:80'
- '443:443'
volumes:
- /home/dev/diegomagalhaes.com/docker/volumes/wordpress:/bitnami
depends_on:
- mariadb
environment:
- MARIADB_HOST=mariadb
- MARIADB_PORT_NUMBER=3306
- WORDPRESS_DATABASE_USER=bn_wordpress
- WORDPRESS_DATABASE_NAME=bitnami_wordpress
- ALLOW_EMPTY_PASSWORD=yes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment