-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci: updated logic to enable Netlify previews for release branch PRs (#…
- Loading branch information
1 parent
b90fe44
commit b76f2ad
Showing
5 changed files
with
215 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,43 @@ | ||
#!/bin/bash | ||
|
||
# List of branches to NOT create a Netlify preview | ||
# Master branch does not need a preview | ||
# Release branches get a preview through docs-latest.spectrocloud.com | ||
############################################ | ||
# This script checks if a Netlify context is for branch-deploy. | ||
# Netlify branch-deploy contexts are only allowed for branches that match version-*. This script is used in the Netlify build settings to determine if a preview should be created. | ||
# This script is created to prevent both a build-preview and a branch-deploy preview from being created for the same branch at the same time. | ||
# In the CI/CD pipeline, the scripts netlify_add_branch.sh and netlify_remove_branch.sh are used to manage the allowed branches list in the Netlify build settings. | ||
# The allowed branches list is used to determine which branches are allowed to create a Netlify preview for the purpose on enabling the Netlify Collab drawer. | ||
# By default, only deploy previews targeting the production branch are allowed, unless manually specified in the Netlify site settings. The scripts netlify_add_branch.sh and netlify_remove_branch.sh handle this responsiblity. | ||
|
||
|
||
# List of branches to NOT create an automatic Netlify preview. This also includes branch-deploy previews. | ||
disallowed_branches=("master" "release-*") | ||
target_branch=$1 | ||
context=$CONTEXT | ||
|
||
# Get current branch name | ||
current_branch=$(git branch --show-current) | ||
|
||
# Use HEAD if current_branch is empty | ||
[ -z "$current_branch" ] && current_branch="$HEAD" | ||
|
||
echo "Branch name: $current_branch" | ||
echo "Current branch name: $current_branch" | ||
echo "Context: $context" | ||
|
||
# Initialize not_allowed flag | ||
not_allowed=0 | ||
# Initialize allowed flag | ||
allowed=1 | ||
|
||
# Compare current_branch against disallowed list | ||
for disallowed in "${disallowed_branches[@]}" | ||
do | ||
if [[ "$current_branch" == $disallowed ]]; then | ||
not_allowed=1 | ||
break | ||
# Check if context is branch-deploy and current branch matches version-* | ||
if [[ "$context" == "branch-deploy" ]]; then | ||
if [[ "$current_branch" == version-* ]]; then | ||
allowed=1 | ||
else | ||
allowed=0 | ||
fi | ||
done | ||
fi | ||
|
||
# Exit based on not_allowed flag | ||
if [ $not_allowed -eq 1 ]; then | ||
echo "Not allowed to create a Netlify preview" | ||
exit 0 | ||
else | ||
# Exit based on allowed flag | ||
# Netlify has inverse exit codes. 0 is allowed, 1 is not allowed. | ||
if [ $allowed -eq 1 ]; then | ||
echo "Allowed to create a Netlify preview" | ||
exit 1 | ||
fi | ||
else | ||
echo "Not allowed to create a Netlify preview" | ||
exit 0 | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#!/bin/bash | ||
|
||
# Function to handle errors | ||
handle_error() { | ||
echo "Error: $1" | ||
exit 1 | ||
} | ||
|
||
# Check if NETLIFY_SITE_ID is set | ||
if [ -z "$NETLIFY_SITE_ID" ]; then | ||
handle_error "NETLIFY_SITE_ID is not set." | ||
fi | ||
|
||
# Check if NETLIFY_AUTH_TOKEN is set | ||
if [ -z "$NETLIFY_AUTH_TOKEN" ]; then | ||
handle_error "NETLIFY_AUTH_TOKEN is not set." | ||
fi | ||
|
||
# Check if GITHUB_BRANCH is set | ||
if [ -z "$GITHUB_BRANCH" ]; then | ||
handle_error "GITHUB_BRANCH is not set." | ||
fi | ||
|
||
# Extract the allowed branches list | ||
echo "Fetching allowed branches for site $NETLIFY_SITE_ID..." | ||
response=$(curl --location --write-out "HTTPSTATUS:%{http_code}" --silent --output /tmp/curl_response \ | ||
"https://api.netlify.com/api/v1/sites/$NETLIFY_SITE_ID" \ | ||
--header "Content-Type: application/json" \ | ||
--header "Authorization: Bearer $NETLIFY_AUTH_TOKEN") | ||
|
||
http_code=$(grep -o "HTTPSTATUS:[0-9]*" <<< "$response" | cut -d: -f2) | ||
content=$(sed -e "s/HTTPSTATUS:[0-9]*$//" /tmp/curl_response) | ||
|
||
if [ "$http_code" -ne 200 ]; then | ||
handle_error "Failed to fetch allowed branches. HTTP status code: $http_code" | ||
fi | ||
|
||
allowed_branches=$(echo "$content" | jq '.build_settings.allowed_branches') | ||
|
||
if [ -z "$allowed_branches" ]; then | ||
handle_error "Allowed branches list is empty." | ||
fi | ||
|
||
echo "Current allowed branches: $allowed_branches" | ||
|
||
# Check if the current GitHub branch is already in the allowed branches list | ||
if echo "$allowed_branches" | jq -e ". | index(\"$GITHUB_BRANCH\")" > /dev/null; then | ||
echo "The branch $GITHUB_BRANCH is already in the allowed branches list." | ||
exit 0 | ||
fi | ||
|
||
# Append the current GitHub branch to the allowed branches list | ||
allowed_branches=$(echo "$allowed_branches" | jq --arg branch "$GITHUB_BRANCH" '. + [$branch]') || handle_error "Could not append the branch to the allowed branches." | ||
|
||
echo "Updated allowed branches: $allowed_branches" | ||
|
||
# Update the build settings using the updated allowed branches | ||
echo "Updating build settings for site $NETLIFY_SITE_ID..." | ||
response=$(curl --location --write-out "HTTPSTATUS:%{http_code}" --silent --output /tmp/curl_response \ | ||
--request PATCH "https://api.netlify.com/api/v1/sites/$NETLIFY_SITE_ID" \ | ||
--header "Content-Type: application/json" \ | ||
--header "Authorization: Bearer $NETLIFY_AUTH_TOKEN" \ | ||
--data "{ | ||
\"build_settings\": { | ||
\"branch\": \"master\", | ||
\"allowed_branches\": $allowed_branches | ||
} | ||
}") | ||
|
||
http_code=$(grep -o "HTTPSTATUS:[0-9]*" <<< "$response" | cut -d: -f2) | ||
content=$(sed -e "s/HTTPSTATUS:[0-9]*$//" /tmp/curl_response) | ||
|
||
if [ "$http_code" -ne 200 ]; then | ||
handle_error "Failed to update Netlify settings. HTTP status code: $http_code" | ||
fi | ||
|
||
echo "Netlify logic completed successfully." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#!/bin/bash | ||
|
||
# Function to handle errors | ||
handle_error() { | ||
echo "Error: $1" | ||
exit 1 | ||
} | ||
|
||
# Check if NETLIFY_SITE_ID is set | ||
if [ -z "$NETLIFY_SITE_ID" ]; then | ||
handle_error "NETLIFY_SITE_ID is not set." | ||
fi | ||
|
||
# Check if NETLIFY_AUTH_TOKEN is set | ||
if [ -z "$NETLIFY_AUTH_TOKEN" ]; then | ||
handle_error "NETLIFY_AUTH_TOKEN is not set." | ||
fi | ||
|
||
# Check if GITHUB_BRANCH is set | ||
if [ -z "$GITHUB_BRANCH" ]; then | ||
handle_error "GITHUB_BRANCH is not set." | ||
fi | ||
|
||
# Extract the allowed branches list | ||
echo "Fetching allowed branches for site $NETLIFY_SITE_ID..." | ||
response=$(curl --location --write-out "HTTPSTATUS:%{http_code}" --silent --output /tmp/curl_response \ | ||
"https://api.netlify.com/api/v1/sites/$NETLIFY_SITE_ID" \ | ||
--header "Content-Type: application/json" \ | ||
--header "Authorization: Bearer $NETLIFY_AUTH_TOKEN") | ||
|
||
http_code=$(grep -o "HTTPSTATUS:[0-9]*" <<< "$response" | cut -d: -f2) | ||
content=$(sed -e "s/HTTPSTATUS:[0-9]*$//" /tmp/curl_response) | ||
|
||
if [ "$http_code" -ne 200 ]; then | ||
handle_error "Failed to fetch allowed branches. HTTP status code: $http_code" | ||
fi | ||
|
||
allowed_branches=$(echo "$content" | jq '.build_settings.allowed_branches') | ||
|
||
if [ -z "$allowed_branches" ]; then | ||
handle_error "Allowed branches list is empty." | ||
fi | ||
|
||
echo "Current allowed branches: $allowed_branches" | ||
|
||
# Check if the current GitHub branch is in the allowed branches list | ||
if ! echo "$allowed_branches" | jq -e ". | index(\"$GITHUB_BRANCH\")" > /dev/null; then | ||
echo "The branch $GITHUB_BRANCH is not in the allowed branches list." | ||
exit 0 | ||
fi | ||
|
||
# Remove the current GitHub branch from the allowed branches list | ||
allowed_branches=$(echo "$allowed_branches" | jq --arg branch "$GITHUB_BRANCH" 'del(.[] | select(. == $branch))') || handle_error "Could not remove the branch from the allowed branches." | ||
|
||
echo "Updated allowed branches: $allowed_branches" | ||
|
||
# Update the build settings using the updated allowed branches | ||
echo "Updating build settings for site $NETLIFY_SITE_ID..." | ||
response=$(curl --location --write-out "HTTPSTATUS:%{http_code}" --silent --output /tmp/curl_response \ | ||
--request PATCH "https://api.netlify.com/api/v1/sites/$NETLIFY_SITE_ID" \ | ||
--header "Content-Type: application/json" \ | ||
--header "Authorization: Bearer $NETLIFY_AUTH_TOKEN" \ | ||
--data "{ | ||
\"build_settings\": { | ||
\"branch\": \"master\", | ||
\"allowed_branches\": $allowed_branches | ||
} | ||
}") | ||
|
||
http_code=$(grep -o "HTTPSTATUS:[0-9]*" <<< "$response" | cut -d: -f2) | ||
content=$(sed -e "s/HTTPSTATUS:[0-9]*$//" /tmp/curl_response) | ||
|
||
if [ "$http_code" -ne 200 ]; then | ||
handle_error "Failed to update Netlify settings. HTTP status code: $http_code" | ||
fi | ||
|
||
echo "Netlify logic updated successfully." |