Last active
February 19, 2020 19:25
-
-
Save q0rban/3912a13f191029cbd00c2ca6ffb6499e to your computer and use it in GitHub Desktop.
Script to periodically create a new Tugboat Base Preview and an example circle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# An example CircleCI config.yml to periodically build a new Tugboat base preview. | |
version: 2 | |
jobs: | |
rebuild-tugboat: | |
docker: | |
- image: circleci/node:10-stretch | |
steps: | |
- run: | |
name: Install Tugboat | |
command: | | |
wget https://dashboard.tugboat.qa/cli/linux/tugboat.tar.gz | |
tar -xvzf tugboat.tar.gz | |
sudo mv tugboat /usr/local/bin | |
rm tugboat.tar.gz | |
- checkout | |
- run: | |
name: Rebuild the Tugboat base preview | |
command: '~/project/.tugboat/rebuild-base.sh' | |
no_output_timeout: 10800 | |
# Declare a workflow that runs all of our jobs in parallel. | |
workflows: | |
version: 2 | |
rebuild-tugboat: | |
triggers: | |
- schedule: | |
# 3am every Sunday | |
cron: "0 3 * * 0" | |
filters: | |
branches: | |
only: master | |
jobs: | |
- rebuild-tugboat | |
manual-rebuild-tugboat: | |
jobs: | |
- hold: | |
type: approval | |
filters: | |
branches: | |
only: | |
- master | |
- /.*tugboat.*/ | |
- rebuild-tugboat: | |
requires: | |
- hold |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -euo pipefail | |
####### | |
## This script is used as an alternate means for rebuilding or refreshing your | |
## base preview. You might want to use this script if: | |
## | |
## 1. You are using Docker images in your Tugboat config that need to be | |
## refreshed as well. | |
## 2. You have lots and lots of Previews, and if a refresh or rebuild fails, | |
## you end up with many orphaned Previews and / or no disk space to fix the | |
## situation. | |
## 3. You have many locked previews that get orphaned after a rebuild / refresh | |
## which can tend to blow up your disk usage. | |
## 4. You would like to choose a different regularity for when your Base preview | |
## is refreshed or rebuilt, and trigger it in another CI service such as | |
## Jenkins, CircleCI, et.al. | |
## | |
## If any of the above are true, this script is for you! | |
## | |
## To use it: | |
## 1. Modify the the variables below to match your project needs. | |
## 2. Commit this script to your repo at ./.tugboat/rebuild-base.sh. | |
## 3. Create a job on your CI service of choice to run this script. You'll need | |
## that job to have the jq and tugboat command-line tools available. For an | |
## example script that would work in CircleCI, see the following gist: | |
## https://gist.github.com/q0rban/3912a13f191029cbd00c2ca6ffb6499e | |
## 4. Generate a Tugboat CLI token and inject it as an environment variable | |
## named TUGBOAT_TOKEN to your job above: | |
## https://docs.tugboat.qa/tugboat-cli/set-an-access-token/ | |
## 5. Test the new CI job to ensure it works properly. | |
## 6. Disable the "Refresh Base Previews Automatically" checkbox on your | |
## Tugboat repo settings page: | |
## https://docs.tugboat.qa/setting-up-tugboat/select-repo-settings | |
####### | |
# Specify the Tugboat repo ID. https://docs.tugboat.qa/faq/find-tugboat-ids/ | |
TUGBOAT_REPO="[your-repo-id]" | |
# Specify the branch you'd like your Base preview built from. | |
BASE_PREVIEW_BRANCH="master" | |
# This DATE variable will be in the name of the new Base Preview, so you can | |
# differentiate between it and the old ones. No need to change this if you're | |
# happy with YYYY-MM-DD format. | |
DATE=`date "+%Y-%m-%d"` | |
## You probably don't need to modify anything below. | |
_jq() { | |
if [[ "$#" -gt 1 ]]; then | |
COMMAND=`printf "$1" "${@:2}"` | |
else | |
COMMAND="$@" | |
fi | |
echo "$ALL_PREVIEWS" | jq --raw-output "$COMMAND" | |
} | |
_tugboat() { | |
tugboat -t "$TUGBOAT_TOKEN" "$@" | |
} | |
# Helper function to output a string to stderr and exit. | |
echoerr() { | |
echo "$@" 1>&2 | |
exit 23 | |
} | |
ALL_PREVIEWS=`_tugboat ls previews output=json repo="$TUGBOAT_REPO"` | |
CURRENT_BASE=`_jq '.[] | select(.anchor == true) | .id'` | |
if [[ -z "$CURRENT_BASE" ]]; then | |
echoerr "Unable to detect a current base preview." | |
fi | |
printf "Found current base preview %s.\n" "$CURRENT_BASE" | |
printf "Commencing build of new base preview." | |
NEW_BASE=`_tugboat create preview $BASE_PREVIEW_BRANCH \ | |
base=false \ | |
repo="$TUGBOAT_REPO" \ | |
label="$BASE_PREVIEW_BRANCH $DATE" \ | |
output=json \ | |
| jq --raw-output '.id'` | |
_tugboat update "$NEW_BASE" anchor=true | |
printf "Created new base preview %s.\n" "$NEW_BASE" | |
_tugboat update "$CURRENT_BASE" anchor=false | |
printf "Removed %s as a base preview.\n" "$CURRENT_BASE" | |
PREVIEWS_TO_REBUILD=`_jq '.[] | select(.base == "%s") | select(.locked == false) | .id' "$CURRENT_BASE" || true` | |
if [[ -n "$PREVIEWS_TO_REBUILD" ]]; then | |
for PREVIEW_TO_REBUILD in $PREVIEWS_TO_REBUILD; do | |
REF=`_jq '.[] | select(.id == "%s") | .ref' "$PREVIEW_TO_REBUILD"` | |
printf "Rebuilding %s...\n" $REF | |
# Create a new preview. Then delete the old one. | |
# Continue to the next if anything fails. | |
_tugboat rebuild $PREVIEW_TO_REBUILD base=$NEW_BASE || continue | |
printf 'Done.\n\n' | |
done | |
fi | |
echo "Rebuild of Tugboat base preview complete." | |
REMAINING_PREVIEWS=`_tugboat ls previews output=json repo="$TUGBOAT_REPO" | jq ".[] | select(.base == \"$CURRENT_BASE\")"` | |
if [[ -z "$REMAINING_PREVIEWS" ]]; then | |
_tugboat delete $CURRENT_BASE | |
echo "Deleted old base preview." | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment