gymnastjs/gymnast

View on GitHub
scripts/deploystorybook.sh

Summary

Maintainability
Test Coverage

#!/usr/bin/env bash

set -eux

# Deploy the built story book bundles when running on CI
#
# When on the master branch the deployment is at `/`, any other branches and forks use:
#   `/branch/[branch-name]`
# To prevent your feature branch from being overwritten by other forks with the same name, you can
# use the format:
#   `[username]/[feature]`
#

# Skip if not on CI

if [ "$CI" != true ]; then
  echo "This script is only meant to run on CI. Set 'CI' global to true to override."
  exit 1
fi

if [ "$CIRCLE_NODE_INDEX" -gt "0" ]; then
  echo "This script runs on the first CI node. Skipping to prevent multiple deploys to gh-pages"
  exit 0
fi

# Ensure all required variables are defined
DEFAULT_USERNAME='[unknown user]'
DEFAULT_BRANCH='default'
USER=${CIRCLE_USERNAME:-$DEFAULT_USERNAME}
BRANCH=${CIRCLE_BRANCH:-$DEFAULT_BRANCH}
TEMP_PATH="$PWD/../temp"

if [ "$CIRCLE_BRANCH" = 'master' ]; then
  TARGET_PATH='.'
else
  TARGET_PATH="./branch/$BRANCH"
fi

# Set git user
git config user.email "$USER@gymnast.ci" && git config user.name "gymnast CI ($BRANCH)"

# Build StoryBook in the `../temp` folder
yarn --ignore-engines
yarn build:dev
yarn build-storybook -c storybook -o $TEMP_PATH

# Copy circleci to ensure last config is used
cp -R .circleci $TEMP_PATH
# Copy .gitignore to avoid commiting unnecessary files
cp .gitignore $TEMP_PATH

# Switch to gh-pages branch
git reset --hard HEAD
git fetch origin gh-pages
git checkout gh-pages
git reset --hard origin/gh-pages
git clean -xdfq

# Remove current content (except for branch folders)
if [ "$CIRCLE_BRANCH" = 'master' ]; then
  mv branch $TEMP_PATH
  rm -rf *
else
  rm -rf $TARGET_PATH
  mkdir -p $TARGET_PATH
fi

# Copy prior content to a folder with the current branch name
mv ../temp/* $TARGET_PATH

# Cleanup temp folder
rm -rf $TEMP_PATH

# Commit changes
if [ -z "$(git status --porcelain)" ]; then
  echo "Nothing to commit, everything is clean ✨"
else
  git add -A -f
  git commit -m "docs(storybook): $USER updated '$TARGET_PATH'" --no-verify
  git fetch origin gh-pages
  git rebase origin/gh-pages
  git push origin gh-pages
fi