Adds script to generate dummy DB per preview

This commit is contained in:
zomars 2022-08-11 18:29:51 -06:00
parent f57726dc95
commit 4c89a64f0e
2 changed files with 76 additions and 59 deletions

76
scripts/vercel.sh Normal file
View File

@ -0,0 +1,76 @@
# We only branch if it's not main or production
if [[ ("$VERCEL_GIT_COMMIT_REF" == "main") || ("$VERCEL_GIT_COMMIT_REF" == "production") ]]; then
exit 1
fi
# We don't have snaplet installed on the CI, so we use this script to get it temporarily.
curl -sL https://app.snaplet.dev/get-cli/ | bash &>/dev/null
export PATH=/vercel/.local/bin/:$PATH
if [ "$VERCEL_GIT_COMMIT_SHA" == "" ]; then
echo "Error: VERCEL_GIT_COMMIT_SHA is empty"
exit 0
fi
if [ "$VERCEL_TOKEN" == "" ]; then
echo "Error: VERCEL_TOKEN is empty"
exit 0
fi
if [ "$VERCEL_PROJECT_ID" == "" ]; then
echo "Error: VERCEL_PROJECT_ID is empty"
exit 0
fi
if [ "$SNAPLET_ACCESS_TOKEN" == "" ]; then
echo "Error: SNAPLET_ACCESS_TOKEN is empty"
exit 0
fi
if [ "$SNAPLET_PROJECT_ID" == "" ]; then
echo "Error: SNAPLET_PROJECT_ID is empty"
exit 0
fi
# stop execution on error - don't let it build if something goes wrong
set -e
# Create new snaplet instant db for this branch
snaplet db create --git --latest
# Save the new snaplet instant db url
DATABASE_URL=$(snaplet db url --git)
if [ "$DATABASE_URL" == "" ]; then
echo "Error: DATABASE_URL is empty"
exit 0
fi
if [ "$VERCEL_ORG_ID" == "" ]; then
# Use this for personal projects
VERCEL_PROJECT_ENDPOINT=$(echo "https://api.vercel.com/v1/projects/$VERCEL_PROJECT_ID/env")
else
# Use this for team projects
VERCEL_PROJECT_ENDPOINT=$(echo "https://api.vercel.com/v1/projects/$VERCEL_PROJECT_ID/env?teamId=$VERCEL_ORG_ID")
fi
echo "calling... $VERCEL_PROJECT_ENDPOINT"
# We update DATABASE_URL using Vercel API
curl -f -sS -o /dev/null -X POST "$VERCEL_PROJECT_ENDPOINT" \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer $VERCEL_TOKEN" \
--data-raw '{
"target": "preview",
"gitBranch": "'$VERCEL_GIT_COMMIT_REF'",
"type": "encrypted",
"key": "DATABASE_URL",
"value": "'$DATABASE_URL'"
}'
res=$?
if test "$res" != "0"; then
echo "the curl command failed with: $res"
exit 0
else
echo "Successfully updated DATABASE_URL"
exit 1
fi

View File

@ -1,59 +0,0 @@
# github submodule repo addresses without https:// prefix
# This didn't work ¯\_(ツ)_/¯
# declare -A remotes=(
# ["apps/website"]="github.com/calcom/website"
# ["apps/api"]="github.com/calcom/api"
# )
# github access token is necessary
# add it to Environment Variables on Vercel
if [ "$GITHUB_ACCESS_TOKEN" == "" ]; then
echo "Error: GITHUB_ACCESS_TOKEN is empty"
exit 1
fi
# stop execution on error - don't let it build if something goes wrong
set -e
# get submodule commit
output=$(git submodule status --recursive) # get submodule info
# Extract each submodule commit hash and path
submodules=$(echo $output | sed "s/ -/__/g" | sed "s/ /=/g" | sed "s/-//g" | tr "__" "\n")
git config --global init.defaultBranch main
git config --global advice.detachedHead false
for submodule in $submodules; do
IFS="=" read COMMIT SUBMODULE_PATH <<<"$submodule"
# This should be a hash table but couldn't make it work ¯\_(ツ)_/¯
# SUBMODULE_GITHUB=$remotes[$SUBMODULE_PATH]
if [ "$SUBMODULE_PATH" == "apps/website" ]; then
SUBMODULE_GITHUB=github.com/calcom/website
fi
if [ "$SUBMODULE_PATH" == "apps/api" ]; then
SUBMODULE_GITHUB=github.com/calcom/api
fi
# set up an empty temporary work directory
rm -rf tmp || true # remove the tmp folder if exists
mkdir tmp # create the tmp folder
cd tmp # go into the tmp folder
# checkout the current submodule commit
git init # initialise empty repo
git remote add $SUBMODULE_PATH https://$GITHUB_ACCESS_TOKEN@$SUBMODULE_GITHUB # add origin of the submodule
git fetch --depth=1 $SUBMODULE_PATH $COMMIT # fetch only the required version
git checkout $COMMIT # checkout on the right commit
# move the submodule from tmp to the submodule path
cd .. # go folder up
rm -rf tmp/.git # remove .git
mv tmp/* $SUBMODULE_PATH/ # move the submodule to the submodule path
# clean up
rm -rf tmp # remove the tmp folder
done