Skip to content

Instantly share code, notes, and snippets.

@jameswilson
Created October 8, 2024 20:33
Show Gist options
  • Save jameswilson/9c57675a63f4a4dc436d0f689691fcb8 to your computer and use it in GitHub Desktop.
Save jameswilson/9c57675a63f4a4dc436d0f689691fcb8 to your computer and use it in GitHub Desktop.
Clear Varnish on Acquia Cloud after code push
#!/bin/sh
#
# Acquia Cloud Hook: post-code-deploy
#
# See https://github.com/acquia/cloud-hooks/blob/master/samples/post-code-deploy.tmpl
# See https://docs.acquia.com/acquia-cloud-platform/develop-apps/api/cloud-hooks
#
# Installation:
#
# 1. Place this file at `hooks/common/post-code-deploy/acquia-cloud-clear-varnish.sh` in your repository.
#
# 2. Generate Acquia Cloud API key & secret.
#
# https://docs.acquia.com/acquia-cloud-platform/develop-apps/api/auth#section-generating-an-api-token
#
# 3. Store the API credentials in a file available on each remote Acquia environment:
#
# # @file /mnt/gfs/$AH_SITE_GROUP.$AH_SITE_ENVIRONMENT/nobackup/acquia-cloud.env
# export ACQUIA_CLIENT_KEY=XXXXXXXXXXX
# export ACQUIA_CLIENT_SECRET=XXXXXXXXXXXXXXXXXXXXXXX
site="$1"
target_env="$2"
# Fallback to the dev target environment if target_env isn't set to an expected environment
if [[ "$target_env" != 'dev' && "$target_env" != 'test' && "$target_env" != 'prod' ]]; then
target_env="dev"
fi
# Load the Acquia Cloud API client key and secret.
source /mnt/gfs/$site.$target_env/nobackup/acquia-cloud.env
client_key="$ACQUIA_CLIENT_KEY"
client_secret="$ACQUIA_CLIENT_SECRET"
# Generate an access token.
access_token=$(curl https://accounts.acquia.com/api/auth/oauth/token --data-urlencode "client_id=$client_key" --data-urlencode "client_secret=$client_secret" --data-urlencode "grant_type=client_credentials" | grep -Po '"'"access_token"'"\s*:\s*"\K([^"]*)')
# Get the application uuid filtering where the name field is "Example".
application_uuid=$(curl -X GET --header "Authorization: Bearer $access_token" --header "Content-Type: application/json" "https://cloud.acquia.com/api/applications?filter=name%3DExample" | grep -Po '"'"uuid"'"\s*:\s*"\K([^"]*)' | head -1)
# Get the environment id for the target environment.
environment_id=$(curl -X GET --header "Authorization: Bearer $access_token" --header "Content-Type: application/json" "https://cloud.acquia.com/api/applications/$application_uuid/environments?filter=name%3D$target_env" | grep -Po '"'"id"'"\s*:\s*"\K([^"]*)')
# Parses the hostname from domains json response, loops through the number of domains,
# and wraps them in valid json leaving the comma off the last item.
domains_json="{\"domains\": ["
domains_json+=$(curl -X GET --header "Authorization: Bearer $access_token" --header "Content-Type: application/json" "https://cloud.acquia.com/api/environments/$environment_id/domains" | grep -Po '"'"hostname"'"\s*:\s*"\K([^"]*)' | sed 's/^/\"/;s/$/\"/;$!s/$/\,/')
domains_json+="]}"
# Clear Varnish cache for the specified domains attached to this environment.
curl -X POST --header "Authorization: Bearer $access_token" --header "Content-Type: application/json" --data "$domains_json" "https://cloud.acquia.com/api/environments/$environment_id/domains/actions/clear-varnish"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment