2023-06-09 00:10:37 +00:00
|
|
|
#!/bin/bash -e
|
2023-06-09 00:01:41 +00:00
|
|
|
|
|
|
|
BRANCH_NAME="$(git branch --show-current)"
|
2023-06-10 20:32:41 +00:00
|
|
|
SCRIPTS_DIR="$(dirname "${BASH_SOURCE[0]}")"
|
2023-06-09 00:01:41 +00:00
|
|
|
|
|
|
|
__netlify_upload() {
|
2023-06-10 16:08:48 +00:00
|
|
|
local branch="$BRANCH_NAME"
|
2023-06-09 00:17:07 +00:00
|
|
|
local production="$DEPLOY_PRODUCTION"
|
2023-06-10 20:59:08 +00:00
|
|
|
[[ "$BRANCH_NAME" = "main" ]] && production="true"
|
2023-06-09 00:17:07 +00:00
|
|
|
[[ -z "$production" ]] && production="false"
|
|
|
|
|
2023-06-09 00:01:41 +00:00
|
|
|
curl -sL \
|
|
|
|
-H "Content-Type: application/zip" \
|
|
|
|
-H "Authorization: Bearer ${NETLIFY_API_KEY}" \
|
|
|
|
--data-binary "@${1}" \
|
2023-06-10 16:08:48 +00:00
|
|
|
"https://api.netlify.com/api/v1/sites/${NETLIFY_SITE_ID}/deploys?branch=${branch}&production=${production}"
|
2023-06-09 00:01:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
__netlify_get_deploy() {
|
|
|
|
local deploy_id="$1"
|
|
|
|
curl -sL \
|
|
|
|
-H "Authorization: Bearer ${NETLIFY_API_KEY}" \
|
|
|
|
"https://api.netlify.com/api/v1/deploys/${deploy_id}"
|
|
|
|
}
|
|
|
|
|
2023-06-10 20:59:08 +00:00
|
|
|
# Publish a deployment to main URL.
|
|
|
|
__netlify_promote() {
|
|
|
|
local deploy_id="$1"
|
|
|
|
curl -sL \
|
|
|
|
-H "Authorization: Bearer ${NETLIFY_API_KEY}" \
|
|
|
|
-H "Content-Type: application/json" \
|
|
|
|
--data "{}" \
|
|
|
|
"https://api.netlify.com/api/v1/sites/${NETLIFY_SITE_ID}/deploys/${deploy_id}/restore"
|
|
|
|
}
|
|
|
|
|
|
|
|
__netlify_get_error() {
|
|
|
|
local error_message
|
|
|
|
error_message="$(json_get "$upload_resp" message)"
|
|
|
|
[[ "$error_message" = "null" ]] && error_message="$(json_get "$upload_resp" error_message)"
|
|
|
|
echo -n "$error_message"
|
|
|
|
}
|
|
|
|
|
2023-06-10 20:32:41 +00:00
|
|
|
json_get() {
|
|
|
|
local json_body="$1"
|
|
|
|
shift
|
|
|
|
|
|
|
|
echo -n "$json_body" | "${SCRIPTS_DIR}/read_json.mjs" "$@"
|
|
|
|
}
|
|
|
|
|
2023-06-09 00:01:41 +00:00
|
|
|
deploy_netlify() {
|
|
|
|
local upload_resp
|
|
|
|
upload_resp="$(__netlify_upload "$1")"
|
|
|
|
|
2023-06-10 20:59:08 +00:00
|
|
|
local error_message="$(__netlify_get_error "$upload_resp")"
|
2023-06-10 20:32:41 +00:00
|
|
|
|
2023-06-09 00:01:41 +00:00
|
|
|
if [[ "$error_message" != "null" ]]; then
|
|
|
|
echo "Deploy to netlify failed:"
|
|
|
|
echo " * ${error_message}"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2023-06-10 20:32:41 +00:00
|
|
|
local deploy_id="$(json_get "$upload_resp" id)"
|
2023-06-09 00:01:41 +00:00
|
|
|
local deploy_resp=""
|
|
|
|
local deploy_state=""
|
|
|
|
local retry_count=10
|
|
|
|
while [[ "$retry_count" -gt 0 ]]; do
|
|
|
|
deploy_resp="$(__netlify_get_deploy "$deploy_id")"
|
2023-06-10 20:32:41 +00:00
|
|
|
deploy_state="$(json_get "$deploy_resp" 'state')"
|
2023-06-09 00:01:41 +00:00
|
|
|
case "$deploy_state" in
|
|
|
|
ready)
|
|
|
|
echo 'Deploy to netlify OK!'
|
2023-06-10 20:32:41 +00:00
|
|
|
echo " * main url: $(json_get "$deploy_resp" 'ssl_url')"
|
|
|
|
echo " * branch: $(json_get "$deploy_resp" 'deploy_ssl_url')"
|
|
|
|
echo " * permalink: $(json_get "$deploy_resp" 'links' 'permalink')"
|
2023-06-10 20:59:08 +00:00
|
|
|
break
|
2023-06-09 00:01:41 +00:00
|
|
|
;;
|
|
|
|
error)
|
|
|
|
echo "Deploy to netlify failed:"
|
2023-06-10 20:32:41 +00:00
|
|
|
echo " * $(json_get "$deploy_resp" 'error_message')"
|
2023-06-09 00:01:41 +00:00
|
|
|
return 1
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
retry_count="$((retry_count - 1))"
|
|
|
|
sleep 3
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
2023-06-10 20:59:08 +00:00
|
|
|
|
|
|
|
if [[ "$BRANCH_NAME" = "main" ]]; then
|
|
|
|
echo "Promoting latest main build..."
|
|
|
|
local promote_resp="$(__netlify_promote "$(json_get "$deploy_resp" 'id')")"
|
|
|
|
error_message="$(__netlify_get_error "$promote_resp")"
|
|
|
|
|
|
|
|
if [[ "$error_message" != "null" ]]; then
|
|
|
|
echo "Promote netlify deploy failed:"
|
|
|
|
echo " * ${error_message}"
|
|
|
|
return 1
|
|
|
|
else
|
2024-09-14 19:59:48 +00:00
|
|
|
echo 'Deployed to main url.'
|
2023-06-10 20:59:08 +00:00
|
|
|
fi
|
|
|
|
fi
|
2023-06-09 00:01:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# For deployment, we care a bit less
|
|
|
|
if [[ -n "${NETLIFY_API_KEY}" && -n "${NETLIFY_SITE_ID}" ]]; then
|
|
|
|
echo "Deploy to netlify..."
|
2024-01-18 00:29:37 +00:00
|
|
|
deploy_netlify um-react-site.zip
|
2023-06-10 20:49:00 +00:00
|
|
|
else
|
|
|
|
echo "skip netlify deployment."
|
2023-06-09 00:01:41 +00:00
|
|
|
fi
|