forked from calcom/cal.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vercel.sh
executable file
·76 lines (64 loc) · 2 KB
/
vercel.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
NEW_DATABASE_URL=$(snaplet db url --git)
if [ "$NEW_DATABASE_URL" == "" ]; then
echo "Error: NEW_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": "'$NEW_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