|
| 1 | +#!/bin/bash |
| 2 | +MAIN_TARGET="main" |
| 3 | + |
| 4 | +# Function to build and deploy a test page for the Stargate Web Client example |
| 5 | +# $1: prefix - the prefix for the deployment, "stargate/v1/$MAIN_TARGET" |
| 6 | +# $2: version - the version of the deployment (branch name or npm version) |
| 7 | +# $3: deploying_branch - 1 if we are deploying a branch, 0 if we are deploying a version |
| 8 | +build_and_deploy() { |
| 9 | + prefix=$1 |
| 10 | + version=$2 |
| 11 | + deploying_branch=$3 |
| 12 | + |
| 13 | + echo "Building Stargate Web Client example version $version with prefix $prefix" |
| 14 | + vite build --base=$prefix/$version/ |
| 15 | + if [ $? -ne 0 ]; then |
| 16 | + echo "Build failed." |
| 17 | + exit 1 |
| 18 | + fi |
| 19 | + |
| 20 | + if [ -z "$APPBUILDER_BUCKET" ]; then |
| 21 | + echo "APPBUILDER_BUCKET environment variable is not set." |
| 22 | + exit 1 |
| 23 | + fi |
| 24 | + |
| 25 | + cachecontrol="public, max-age=31536000, immutable" |
| 26 | + # if we are deploying a branch, we need to change the cache control, compared to a version |
| 27 | + if [ $deploying_branch -eq 1 ]; then |
| 28 | + cachecontrol="public, max-age=0, s-maxage=86400" |
| 29 | + fi |
| 30 | + |
| 31 | + echo "Deploying to version $version with prefix $prefix to bucket $APPBUILDER_BUCKET" |
| 32 | + |
| 33 | + # depending on the prefix, we need to deploy to different locations |
| 34 | + if [ $prefix == "stargate/v1/$MAIN_TARGET" ]; then |
| 35 | + aws s3 sync ./dist s3://$APPBUILDER_BUCKET/appbuilder/$prefix/$version/ --region us-east-1 --cache-control "$cachecontrol" |
| 36 | + touch empty |
| 37 | + aws s3 cp empty s3://$APPBUILDER_BUCKET/appbuilder/$prefix/$version --region us-east-1 \ |
| 38 | + --website-redirect https://appbuilder.shapediver.com/$prefix/$version/ --cache-control "$cachecontrol" |
| 39 | + aws s3 cp empty s3://$APPBUILDER_BUCKET/appbuilder/$prefix/.invalidate --region us-east-1 \ |
| 40 | + --cache-control "$cachecontrol" |
| 41 | + rm empty |
| 42 | + else |
| 43 | + echo "Unsupported prefix for deployment." |
| 44 | + exit 1 |
| 45 | + fi |
| 46 | +} |
| 47 | + |
| 48 | +# load environment variables from .env file |
| 49 | +if [ -f .env ]; then |
| 50 | + export $(grep -v '^#' .env | sed 's/#.*//' | sed 's/^ *//;s/ *$//' | xargs) |
| 51 | +fi |
| 52 | + |
| 53 | +# where should we deploy? |
| 54 | +prefix=$1 |
| 55 | +if [ -z "$prefix" ]; then |
| 56 | + echo "Please specify a prefix." |
| 57 | + exit 1 |
| 58 | +fi |
| 59 | + |
| 60 | +# check for git changes |
| 61 | +if [[ -n $(git status --porcelain) ]]; then |
| 62 | + echo "There are uncommitted changes." |
| 63 | + exit 1 |
| 64 | +fi |
| 65 | + |
| 66 | +# Get the current branch |
| 67 | +branch=$(git rev-parse --abbrev-ref HEAD) |
| 68 | + |
| 69 | +# npm version |
| 70 | +npm_version=$(node -p "require('./package.json').version") |
| 71 | +echo "Current npm version: $npm_version" |
| 72 | + |
| 73 | +deploying_branch=1 |
| 74 | + |
| 75 | +# If the branch is "development", "staging" or starts with "task/", we use the branch name as the version |
| 76 | +if [ "$branch" == "development" ] || [ "$branch" == "staging" ]; then |
| 77 | + deploying_branch=1 |
| 78 | + version=$branch |
| 79 | + |
| 80 | + # And we create a new tag with the name "WordPressPlugin@branch" |
| 81 | + git tag -fa "StargateWebClient@$branch" -m "Release of branch $branch" |
| 82 | + git push origin "StargateWebClient@$branch" --force |
| 83 | +elif [[ $branch == task/* ]]; then |
| 84 | + deploying_branch=1 |
| 85 | + # In this case we have to remove the "task/" prefix |
| 86 | + version=${branch#task/} |
| 87 | +else |
| 88 | + echo "Unsupported branch name." |
| 89 | + exit 1 |
| 90 | +fi |
| 91 | + |
| 92 | +build_and_deploy $prefix $version $deploying_branch |
0 commit comments