Skip to content

Instantly share code, notes, and snippets.

@gillisandrew
Last active January 26, 2020 19:49
Show Gist options
  • Select an option

  • Save gillisandrew/716bc60df94b1b4ed4b1448932edc1f2 to your computer and use it in GitHub Desktop.

Select an option

Save gillisandrew/716bc60df94b1b4ed4b1448932edc1f2 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
BRANCH=$(git rev-parse --abbrev-ref HEAD)
if [[ "$3" == "0" ]]; then
# Exit gracefully when hook is called on file checkout
exit 0
fi
if [[ "$BRANCH" == "" ]]; then
# Error when the branch could not be determined
echo 'ERROR: Unable to determine the current branch'
exit 1
elif [[ "$BRANCH" == 'master' ]]; then
# Map 'master' branch to production workspace
WORKSPACE='production'
elif [[ "$BRANCH" == 'develop' ]]; then
# Map 'develop' branch to development workspace
WORKSPACE='development'
elif [[ "$BRANCH" == *\/* ]]; then
# Allow format ${workspace}/${descriptive-branch-name}
WORKSPACE=$(echo "$BRANCH" | cut -d'/' -f1)
else
# Change to default workspace if no other match is found
WORKSPACE='default'
fi
terraform workspace select $WORKSPACE 2> /dev/null || terraform workspace new $WORKSPACE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment