ui/scripts/build-static

205 lines
5.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:
-d - Build debug instead of production build
-f - Force: Turn off checks that prevent you from doing bad things
-l - Build as "latest" instead of the version in package.json
-t - Tag and push tag
-u - Upload to GCE
-v=VERSION - Force version to be VERSION instead of what is in package.json
EOF
exit 1;
}
# -------------------------------------
# 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 | head -n 1 | cut -f4 -d'"')
FORCE=0
LATEST=0
UPLOAD=0
TAG=0
ENVIRONMENT="production"
: ${FORCE_VERSION:=""}
while getopts ":dlutfv:" opt;do
case $opt in
d)
ENVIRONMENT="development"
;;
f)
FORCE=1
;;
l)
LATEST=1
;;
t)
TAG=1
;;
u)
UPLOAD=1
;;
v)
FORCE_VERSION=$OPTARG
;;
\?)
echo "Invalid arguemnt: ${OPTARG}"
printHelp
exit 1
;;
:)
echo "Option -${OPTARG} requires an argument." >&2
printHelp
exit 1
;;
esac
done
# 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."
if [[ $FORCE -ne 1 ]]; then
exit 1;
fi
fi
# 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."
if [[ $FORCE -ne 1 ]]; then
exit 1;
fi
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}')."
if [[ $FORCE -ne 1 ]]; then
exit 1;
fi
fi
if [[ "${FORCE_VERSION}" != "" ]]; then
VERSION=${FORCE_VERSION}
else
if [[ $LATEST -eq 1 ]]; then
VERSION="latest"
else
VERSION=${PKG_VERSION}
fi
fi
BUILD_PAR="dist/static"
BUILD_DIR="${BUILD_PAR}/${VERSION}"
BUILD_TGZ="${BUILD_PAR}/${VERSION}.tar.gz"
CDN="releases.rancher.com/ui"
GS_URL="gs://${CDN}"
CACHE="Cache-Control:no-cache,must-revalidate"
GZIP="html,js,css,xml,txt,map,svg,ttf,woff,woff2"
GCLOUD_UUID="google/cloud-sdk@sha256:506775439bd4f44d90a3f6784f2b625e55c43635f9f2c4cae24ac4b3dea0277a"
echo "Branch: ${BRANCH}"
if [[ "${VERSION}" != "${PKG_VERSION}" ]]; then
echo "Package Version: ${PKG_VERSION}"
fi
echo "Version: ${VERSION}"
echo "Build Dir: ${BUILD_DIR}"
echo "Options: Force=${FORCE}, Latest=${LATEST}, Upload=${UPLOAD}, Tag=${TAG}"
echo "Current Directory: $(pwd)"
echo "Testing..."
runCmd ./node_modules/.bin/ember test --port 7999
echo "Done Testing."
if [[ $LATEST -ne 1 ]]; then
echo "Building Static Tarball..."
RANCHER_ENDPOINT="" 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
# Create a tarball of the version
runCmd tar -czf ${BUILD_TGZ} -C ${BUILD_PAR} ${VERSION}
fi;
echo "Building Hosted Version..."
RANCHER_ENDPOINT="" 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
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}/_upload"
runCmd gsutil -h "${CACHE}" rsync -c -r -d "${GS_URL}/_upload" "${GS_URL}/${VERSION}"
runCmd gsutil rm -a -f -R "${GS_URL}/_upload"
else
runCmd gsutil -m cp ${BUILD_TGZ} ${GS_URL}
runCmd gsutil -m cp -z "${GZIP}" -R ${BUILD_DIR} ${GS_URL}
fi
fi
# For use from Drone, automatically
if [[ $UPLOAD -eq 2 ]]; then
ABS_BUILD_DIR="$(pwd)/${BUILD_DIR}"
echo "Updating latest ($ABS_BUILD_DIR) thru Docker..."
# Upload new files, with gzip headers (rsync doesn't have this)
runCmd docker run --rm -v $ABS_BUILD_DIR:/upload $GCLOUD_UUID gsutil -h "${CACHE}" -m cp -z "${GZIP}" -R /upload "${GS_URL}/_upload"
# Rsync new files into latest diff
# Beware of the leopard: -m will break rsync on versioned buckets.
runCmd docker run --rm -v $ABS_BUILD_DIR:/upload $GCLOUD_UUID gsutil -h "${CACHE}" rsync -c -r "${GS_URL}/_upload" "${GS_URL}/${VERSION}"
# Remove temp dir
runCmd docker run --rm -v $ABS_BUILD_DIR:/upload $GCLOUD_UUID gsutil rm -a -f -R "${GS_URL}/_upload"
fi