From 4c89a64f0e22694504b862d6d30520cd998b4387 Mon Sep 17 00:00:00 2001 From: zomars Date: Thu, 11 Aug 2022 18:29:51 -0600 Subject: [PATCH] Adds script to generate dummy DB per preview --- scripts/vercel.sh | 76 +++++++++++++++++++++++++++++++++++++++++++++++ vercel.sh | 59 ------------------------------------ 2 files changed, 76 insertions(+), 59 deletions(-) create mode 100644 scripts/vercel.sh delete mode 100755 vercel.sh diff --git a/scripts/vercel.sh b/scripts/vercel.sh new file mode 100644 index 0000000000..b4bae3adbd --- /dev/null +++ b/scripts/vercel.sh @@ -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 diff --git a/vercel.sh b/vercel.sh deleted file mode 100755 index 94f5ceb543..0000000000 --- a/vercel.sh +++ /dev/null @@ -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