@@ -20,6 +20,54 @@ if [[ `git status --porcelain` ]]; then
2020 exit 1
2121fi
2222
23+ if [ -f .env ]; then
24+ export $( grep -v ' ^#' .env | sed ' s/\r$//' | awk ' /=/ {print $1}' )
25+ else
26+ echo " ==> Can not find .env variables"
27+ exit 1
28+ fi
29+
30+ if [[ -z ${BUNNY_SCRIPTS_ACCESS_KEY+x} || -z ${BUNNY_SCRIPTS_ACCOUNT_KEY+x} ]]; then
31+ echo " ==> Make sure BUNNY_SCRIPTS_ACCESS_KEY and BUNNY_SCRIPTS_ACCOUNT_KEY are defined in .env"
32+ exit 1
33+ fi
34+
35+ upload_to_bunny () {
36+ local_file=$1
37+ remote_file=$2
38+ find=${3:- }
39+ replace=${4:- }
40+
41+ if [ ! -z " $find " ] && [ ! -z " $replace " ]; then
42+ sed " s/$find /$replace /g" $local_file > /tmp/tmp_file
43+ put_response=$( curl \
44+ --request PUT \
45+ --url " https://storage.bunnycdn.com/sa-cdn/$remote_file " \
46+ --header " AccessKey: $BUNNY_SCRIPTS_ACCESS_KEY " \
47+ --upload-file " /tmp/tmp_file" \
48+ --output - \
49+ --silent \
50+ -w " %{stdout} %{http_code}"
51+ )
52+ rm /tmp/tmp_file
53+ else
54+ put_response=$( curl \
55+ --request PUT \
56+ --url " https://storage.bunnycdn.com/sa-cdn/$remote_file " \
57+ --header " AccessKey: $BUNNY_SCRIPTS_ACCESS_KEY " \
58+ --upload-file " $local_file " \
59+ --output - \
60+ --silent \
61+ -w " %{stdout} %{http_code}"
62+ )
63+ fi
64+
65+ if ! [[ $put_response = * 201* ]]; then
66+ echo " ==> Failed to update $remote_file , got http code: $put_response "
67+ exit 1
68+ fi
69+ }
70+
2371read -p " ==> Specify the latest SRI version: " VERSION
2472
2573LATEST_FILE=" ./dist/v$VERSION /app.js"
@@ -54,17 +102,42 @@ if [[ $REPLY =~ ^[Yy]$ ]]; then
54102 rsync --rsync-path=" sudo rsync" " $SCRIPTS_LATEST_PATH /light.js" " $REMOTE_PATH /light.js"
55103 rsync --rsync-path=" sudo rsync" " $SCRIPTS_LATEST_PATH /light.js.map" " $REMOTE_PATH /light.js.map"
56104
57- echo " ==> Creating v$VERSION folder on $SERVER_NAME "
58- ssh app@external.simpleanalytics.com mkdir -p " /var/www/default/v$VERSION "
59-
60105 echo " ==> Copying SRI v$VERSION file to $SERVER_NAME "
61- rsync --quiet --rsync-path=" sudo rsync" " ./dist/v$VERSION /custom/app.js" " $REMOTE_PATH /v$VERSION /app.js"
106+ rsync --quiet --rsync-path=" sudo mkdir -p $REMOTE_PATH /v $VERSION && sudo rsync" " ./dist/v$VERSION /custom/app.js" " $REMOTE_PATH /v$VERSION /app.js"
62107 rsync --quiet --rsync-path=" sudo rsync" " ./dist/v$VERSION /custom/app.js.map" " $REMOTE_PATH /v$VERSION /app.js.map"
63108 rsync --quiet --rsync-path=" sudo rsync" " ./dist/v$VERSION /custom/light.js" " $REMOTE_PATH /v$VERSION /light.js"
64109 rsync --quiet --rsync-path=" sudo rsync" " ./dist/v$VERSION /custom/light.js.map" " $REMOTE_PATH /v$VERSION /light.js.map"
65110 rsync --quiet --rsync-path=" sudo rsync" " ./dist/v$VERSION /custom/auto-events.js" " $REMOTE_PATH /v$VERSION /auto-events.js"
66111 rsync --quiet --rsync-path=" sudo rsync" " ./dist/v$VERSION /custom/auto-events.js.map" " $REMOTE_PATH /v$VERSION /auto-events.js.map"
67112
113+ echo " ==> Upload files to Bunny"
114+ upload_to_bunny " dist/v${VERSION} /app.js" " sri/v${VERSION} .js" " app.js.map" " v${VERSION} .js.map"
115+ upload_to_bunny " dist/v${VERSION} /app.js.map" " sri/v${VERSION} .js.map" " app.js" " v${VERSION} .js"
116+ upload_to_bunny " dist/latest/auto-events.js" " auto-events.js"
117+ upload_to_bunny " dist/latest/auto-events.js.map" " auto-events.js.map"
118+ upload_to_bunny " dist/latest/hello.js" " hello.js"
119+ upload_to_bunny " dist/latest/hello.js.map" " hello.js.map"
120+ upload_to_bunny " dist/latest/latest.dev.js" " latest.dev.js"
121+ upload_to_bunny " dist/latest/latest.js" " latest.js"
122+ upload_to_bunny " dist/latest/latest.js.map" " latest.js.map"
123+ upload_to_bunny " dist/latest/light.js" " light.js"
124+ upload_to_bunny " dist/latest/light.js.map" " light.js.map"
125+
126+ echo " ==> Flushing files on Bunny"
127+ flush_response=$( curl \
128+ --request POST \
129+ --header " AccessKey: $BUNNY_SCRIPTS_ACCOUNT_KEY " \
130+ --url " https://api.bunny.net/purge?url=https://simpleanalyticscdn.b-cdn.net/*" \
131+ --output - \
132+ --silent \
133+ -w " %{stdout} %{http_code}"
134+ )
135+
136+ if ! [[ $flush_response = * 200 ]]; then
137+ echo " ==> Failed to flush $cdnfilename , got http code: $flush_response "
138+ exit 1
139+ fi
140+
68141 echo -e " ==> ${GREEN} Woop woop! Deployed to $SERVER_NAME !${RESET} "
69142else
70143 echo ' ==> Cancelled by you'
0 commit comments