2020-05-16 03:04:51 +03:00
|
|
|
#!/bin/bash -ex
|
|
|
|
|
2020-05-23 02:28:40 +03:00
|
|
|
# Bail if we don't have a Github token - witheld for PRs from personal forks
|
|
|
|
if [ -z $STORYBOOKS_GITHUB_TOKEN ]; then
|
|
|
|
echo "Github Personal Access Token required, skipping Storybook build"
|
|
|
|
exit;
|
|
|
|
fi
|
2020-05-16 03:04:51 +03:00
|
|
|
|
|
|
|
# Config from env vars with defaults
|
|
|
|
SKIP_STORYBOOK_BUILD="${SKIP_STORYBOOK_BUILD:-false}"
|
|
|
|
PROJECT_REPO="${PROJECT_REPO:-mozilla/fxa}"
|
2020-05-23 02:28:40 +03:00
|
|
|
STORYBOOKS_REPO="${STORYBOOKS_REPO:-mozilla-fxa/storybooks}"
|
|
|
|
STORYBOOKS_URL="${STORYBOOKS_URL:-https://mozilla-fxa.github.io/storybooks}"
|
2020-05-16 03:04:51 +03:00
|
|
|
STORYBOOKS_BRANCH="${STORYBOOKS_BRANCH:-gh-pages}"
|
|
|
|
PUBLISH_ROOT="${PUBLISH_ROOT:-storybooks-publish}"
|
2020-08-14 02:14:31 +03:00
|
|
|
MAX_STORYBOOK_AGE="${MAX_STORYBOOK_AGE:-3 weeks ago}"
|
2020-05-16 03:04:51 +03:00
|
|
|
|
2020-05-23 02:28:40 +03:00
|
|
|
STORYBOOKS_REPO_URL="https://$STORYBOOKS_GITHUB_TOKEN@github.com/$STORYBOOKS_REPO.git"
|
|
|
|
|
2020-05-16 03:04:51 +03:00
|
|
|
# Context for current build
|
|
|
|
COMMIT_HASH=$(git rev-parse HEAD)
|
|
|
|
|
2020-05-23 02:28:40 +03:00
|
|
|
# Build all the available storybooks relevant to packages/test.list
|
2020-05-16 03:04:51 +03:00
|
|
|
if [ "$SKIP_STORYBOOK_BUILD" == false ]; then
|
|
|
|
STORYBOOKS=$(find packages -maxdepth 2 -type d -name '.storybook');
|
|
|
|
for STORYBOOK_PATH in $STORYBOOKS; do
|
2020-05-23 02:28:40 +03:00
|
|
|
PACKAGE_DIR=$(dirname $STORYBOOK_PATH)
|
|
|
|
PACKAGE_NAME=$(basename $PACKAGE_DIR)
|
|
|
|
if grep -e "$PACKAGE_NAME" -e 'all' "./packages/test.list" > /dev/null; then
|
|
|
|
pushd $PACKAGE_DIR;
|
|
|
|
rm -rf storybook-static;
|
|
|
|
yarn workspaces focus $PACKAGE_NAME
|
|
|
|
yarn run build-storybook;
|
|
|
|
popd
|
|
|
|
fi;
|
2020-05-16 03:04:51 +03:00
|
|
|
done;
|
|
|
|
fi
|
|
|
|
|
2020-05-23 02:28:40 +03:00
|
|
|
# Check if any storybooks were built, bail if not
|
|
|
|
if [[ -z $(find ./packages -type d -name 'storybook-static') ]]; then
|
|
|
|
echo "No Storybooks built, skipping the rest of deployment."
|
|
|
|
exit 0;
|
|
|
|
fi
|
|
|
|
|
2020-05-16 03:04:51 +03:00
|
|
|
# Get a fresh checkout of the repo to which we'll publish
|
|
|
|
rm -rf $PUBLISH_ROOT
|
2020-05-23 02:28:40 +03:00
|
|
|
git clone --branch $STORYBOOKS_BRANCH $STORYBOOKS_REPO_URL $PUBLISH_ROOT
|
2020-05-16 03:04:51 +03:00
|
|
|
mkdir -p $PUBLISH_ROOT/{commits,pulls}
|
|
|
|
|
|
|
|
# Copy all the storybooks into the publish directory for commit.
|
|
|
|
COMMIT_PATH="$PUBLISH_ROOT/commits/$COMMIT_HASH"
|
|
|
|
rm -rf $COMMIT_PATH
|
|
|
|
mkdir -p $COMMIT_PATH
|
2020-05-23 02:28:40 +03:00
|
|
|
for STORYBOOK_DIR in $(find ./packages -type d -name 'storybook-static'); do
|
2020-05-16 03:04:51 +03:00
|
|
|
PACKAGE_NAME=$(basename $(dirname $STORYBOOK_DIR));
|
|
|
|
BUILD_PATH="$COMMIT_PATH/$PACKAGE_NAME";
|
|
|
|
cp -r $STORYBOOK_DIR $BUILD_PATH
|
|
|
|
done
|
|
|
|
|
|
|
|
# HACK: fixup some generated CSS paths to static media that break since the
|
|
|
|
# storybooks are no longer at the root of the site. Would be better to
|
|
|
|
# figure out how to reconfigure storybook to do this, but I got here faster
|
|
|
|
for CSS_FN in $(find $COMMIT_PATH -type f -name 'main*.css'); do
|
|
|
|
sed --in-place 's:url(static/:url(../../static/:g' $CSS_FN
|
|
|
|
done
|
|
|
|
|
|
|
|
# Capture git logs for current commit as summary & description.
|
|
|
|
git log -n 1 --no-color --pretty='%s' > $COMMIT_PATH/summary.txt
|
|
|
|
git log -n 1 --no-color --pretty=medium > $COMMIT_PATH/description.txt
|
|
|
|
|
|
|
|
# If this is a pull request, note a reference to the commit hash for the PR
|
|
|
|
if [[ ! -z $CI_PULL_REQUEST ]]; then
|
|
|
|
PR_NUMBER=$(echo $CI_PULL_REQUEST | cut -d/ -f7);
|
|
|
|
PULL_PATH="$PUBLISH_ROOT/pulls/$PR_NUMBER";
|
|
|
|
mkdir -p $PULL_PATH;
|
|
|
|
cp $COMMIT_PATH/*.txt $PULL_PATH/
|
|
|
|
echo $COMMIT_HASH > $PULL_PATH/commit.txt;
|
|
|
|
fi;
|
|
|
|
|
|
|
|
# Delete storybooks older than 90 days
|
2020-08-14 02:14:31 +03:00
|
|
|
pushd $PUBLISH_ROOT
|
|
|
|
DELETE_BEFORE=$(date --date="$MAX_STORYBOOK_AGE" '+%Y-%m-%d')
|
|
|
|
git log --before $DELETE_BEFORE --name-only commits/ | grep '^commits/.*/' | \
|
|
|
|
cut -d/ -f1-2 | sort | uniq | xargs rm -rf
|
|
|
|
git log --before $DELETE_BEFORE --name-only pulls/ | grep '^pulls/.*/' | \
|
|
|
|
cut -d/ -f1-2 | sort | uniq | xargs rm -rf
|
|
|
|
popd
|
2020-05-16 03:04:51 +03:00
|
|
|
|
2020-05-23 02:28:40 +03:00
|
|
|
# Build index.html for root, commits, and pulls
|
2020-05-16 03:04:51 +03:00
|
|
|
./_scripts/build-storybooks-indexes.js $PUBLISH_ROOT
|
|
|
|
|
|
|
|
# Check for changes
|
|
|
|
cd $PUBLISH_ROOT
|
|
|
|
CHANGES=$(git status --porcelain)
|
|
|
|
if [ "$CHANGES" = "" ]; then
|
|
|
|
echo "Storybooks are unchanged, not deploying to GitHub Pages."
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Finally, deploy the publish directory to gh-pages.
|
|
|
|
echo "Deploying Storybooks to GitHub Pages."
|
|
|
|
git config user.name "fxa-devs"
|
|
|
|
git config user.email "fxa-core@mozilla.com"
|
|
|
|
git add -A .
|
|
|
|
git commit -qm "chore(docs): rebuild storybooks for ${COMMIT_HASH} [skip ci]"
|
|
|
|
git push -q origin gh-pages
|
|
|
|
|
2020-05-23 02:28:40 +03:00
|
|
|
# If this is a pull request, post a comment to the PR linking to the storybooks
|
|
|
|
if [[ ! -z $CI_PULL_REQUEST ]]; then
|
|
|
|
COMMIT_URL="$STORYBOOKS_URL/commits/$COMMIT_HASH/";
|
|
|
|
curl \
|
|
|
|
-H'Content-Type: application/json' \
|
|
|
|
-H"Authorization: token $STORYBOOKS_GITHUB_TOKEN" \
|
2020-06-17 01:59:27 +03:00
|
|
|
--data "{\"state\":\"success\",\"target_url\":\"$COMMIT_URL\",\"context\":\"storybooks: pull request\",\"description\":\"Storybook deployment for $COMMIT_HASH\"}" \
|
2020-05-27 23:04:06 +03:00
|
|
|
https://api.github.com/repos/${PROJECT_REPO}/statuses/${COMMIT_HASH};
|
2020-05-23 02:28:40 +03:00
|
|
|
fi
|
|
|
|
|
2020-05-16 03:04:51 +03:00
|
|
|
echo "Cleaning up $PUBLISH_ROOT"
|
|
|
|
cd ..
|
|
|
|
rm -rf $PUBLISH_ROOT
|