From dd7a238eb8f1cca52666a777118b7d12584e7cc4 Mon Sep 17 00:00:00 2001 From: Jixun Wu Date: Sat, 10 Jun 2023 21:59:08 +0100 Subject: [PATCH] ci: automatically promote latest main build as published. --- scripts/deploy.sh | 42 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/scripts/deploy.sh b/scripts/deploy.sh index 1e163ae..ea7a1ca 100755 --- a/scripts/deploy.sh +++ b/scripts/deploy.sh @@ -6,11 +6,8 @@ SCRIPTS_DIR="$(dirname "${BASH_SOURCE[0]}")" __netlify_upload() { local branch="$BRANCH_NAME" local production="$DEPLOY_PRODUCTION" + [[ "$BRANCH_NAME" = "main" ]] && production="true" [[ -z "$production" ]] && production="false" - if [[ "$BRANCH_NAME" = "main" ]]; then - production="true" - branch="" - fi curl -sL \ -H "Content-Type: application/zip" \ @@ -26,6 +23,23 @@ __netlify_get_deploy() { "https://api.netlify.com/api/v1/deploys/${deploy_id}" } +# 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" +} + json_get() { local json_body="$1" shift @@ -37,9 +51,7 @@ deploy_netlify() { local upload_resp upload_resp="$(__netlify_upload "$1")" - local error_message - error_message="$(json_get "$upload_resp" message)" - [[ "$error_message" = "null" ]] && error_message="$(json_get "$upload_resp" error_message)" + local error_message="$(__netlify_get_error "$upload_resp")" if [[ "$error_message" != "null" ]]; then echo "Deploy to netlify failed:" @@ -60,7 +72,7 @@ deploy_netlify() { 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')" - return 0 + break ;; error) echo "Deploy to netlify failed:" @@ -73,6 +85,20 @@ deploy_netlify() { ;; esac done + + 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 + echo 'Deoployed to main url.' + fi + fi } # For deployment, we care a bit less