ui/scripts/build-static

171 lines
4.1 KiB
Bash
Executable File

#!/bin/bash
set -e
set -u
: ${CI_BRANCH=""}
: ${UPLOAD_LATEST=""}
# cd to app root
CWD=$(dirname $0)
if [[ `basename $(pwd)` = 'scripts' ]]; then
cd ../
else
cd `dirname $CWD`
fi
# -------------------------------------
# Execute something and exit if it fails
function runCmd() {
$@
if [[ $? -ne 0 ]]; then
echo "Command: $@ failed"
exit 2
fi
}
function printHelp() {
cat 1>&2 <<EOF
build-static Usage:
-l - Build as "latest" instead of the version in package.json
-u - Upload to GCE
-t - Tag and push tag
EOF
exit 1;
}
# -------------------------------------
# Why are you trying to do a build when there are uncommitted changes?
if [[ `git status --porcelain` ]]; then
echo "There are uncommited changes. Please check the number and try again."
exit 1;
fi
echo "Environment Variables:"
env
# Parse options
BRANCH=$CI_BRANCH
if [[ -z "$BRANCH" ]]; then
BRANCH=$(git symbolic-ref HEAD | sed -e "s/^refs\/heads\///")
fi
if [[ -z "$BRANCH" ]]; then
BRANCH="master"
fi
PKG_VERSION=$(cat package.json | grep version | cut -f4 -d'"' | sed 's/-/_/g')
LATEST=0
UPLOAD=0
TAG=0
while getopts ":lut" opt;do
case $opt in
l)
LATEST=1
;;
t)
TAG=1
;;
u)
UPLOAD=1
;;
\?)
echo "Invalid arguemnts"
printHelp
exit 1
;;
:)
echo "Option -${OPTARG} requires an argument." >&2
printHelp
exit 1
;;
esac
done
# UPLOAD_LATEST=true is set by Drone for auto upload to CDN
if [[ "${BRANCH}" == "master" ]] && [[ "${UPLOAD_LATEST}" == "true" ]]; then
UPLOAD=2
LATEST=1
fi
# Sanity checking
if [[ $LATEST -eq 1 ]] && [[ $TAG -eq 1 ]]; then
echo "You can't tag latest."
exit 1
fi
if ( [[ $TAG -eq 1 ]] || [[ $UPLOAD -ne 0 ]] ) && [[ "${BRANCH}" != "master" ]]; then
echo "You can only tag or upload the master branch (you are on '${BRANCH}')."
exit 1
fi
if [[ $LATEST -eq 1 ]]; then
VERSION="latest"
else
VERSION=${PKG_VERSION}
fi
ENVIRONMENT="production"
BUILD_DIR="dist/static/${VERSION}"
CDN="cdn.rancher.io/ui"
URL="/static"
GS_URL="gs://${CDN}"
CACHE="Cache-Control:no-cache,must-revalidate"
GZIP="html,js,css,xml,txt,map,svg,ttf,woff,woff2"
echo "Branch: ${BRANCH}"
if [[ "${VERSION}" != "${PKG_VERSION}" ]]; then
echo "Package Version: ${PKG_VERSION}"
fi
echo "Version: ${VERSION}"
echo "Build Dir: ${BUILD_DIR}"
echo "Options: Latest=${LATEST}, Tag=${TAG}, Upload=${UPLOAD}"
echo "Current Directory: $(pwd)"
echo "Testing..."
runCmd ./node_modules/.bin/ember test
echo "Done Testing."
echo "Building Static..."
RANCHER_ENDPOINT="" BASE_URL="${URL}" BASE_ASSETS="//${CDN}/${VERSION}" runCmd ./node_modules/.bin/ember build --environment=${ENVIRONMENT} --output-path=${BUILD_DIR}
# Create a file containing the version
echo "${PKG_VERSION}" > ${BUILD_DIR}/VERSION.txt
# Replace the version token in the static file that cattle serves up
sed -i.bak s/VERSION/${VERSION}/g "${BUILD_DIR}/static/index.html"
if [[ $TAG -eq 1 ]]; then
runCmd git tag v${VERSION}
runCmd git push --tags
fi
# For use on your laptop, manually
if [[ $UPLOAD -eq 1 ]]; then
echo "Uploading..."
if [[ $LATEST -eq 1 ]]; then
runCmd gsutil -h "${CACHE}" -m cp -z "${GZIP}" -R ${BUILD_DIR} "${GS_URL}/_latest"
runCmd gsutil -h "${CACHE}" -m rsync -c -r -d "${GS_URL}/_latest" "${GS_URL}/latest"
runCmd gsutil rm -a -f -R "${GS_URL}/_latest"
else
runCmd gsutil -m cp -z "${GZIP}" -R ${BUILD_DIR} ${GS_URL}
fi
fi
# For use from Drone, automatically
if [[ $UPLOAD -eq 2 ]]; then
LATEST_DIR="$(pwd)/${BUILD_DIR}"
echo "Updating latest ($LATEST_DIR) thru Docker..."
# Upload new files, with gzip headers (rsync doesn't have this)
runCmd docker run --rm -v $LATEST_DIR:/latest google/cloud-sdk gsutil -h "${CACHE}" -m cp -z "${GZIP}" -R /latest "${GS_URL}/_latest"
# Rsync new files into latest dif
runCmd docker run --rm -v $LATEST_DIR:/latest google/cloud-sdk gsutil -h "${CACHE}" -m rsync -c -r -d "${GS_URL}/_latest" "${GS_URL}/latest"
# Remove temp dir
runCmd docker run --rm -v $LATEST_DIR:/latest google/cloud-sdk gsutil rm -a -f -R "${GS_URL}/_latest"
fi