Skip to content

Commit

Permalink
ci: updated logic to enable Netlify previews for release branch PRs (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
karl-cardenas-coding committed May 22, 2024
1 parent b90fe44 commit b76f2ad
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 45 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/clean-up-report.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,26 @@ on:
branches-ignore: [master, main, gh-pages]


env:
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}
GITHUB_BRANCH: ${{ github.event.ref }}

concurrency:
group: ${{ github.event.ref }}
cancel-in-progress: true

jobs:
update_netlify:
runs-on: ubuntu-latest
steps:

- name: Checkout GitHub Pages Branch
uses: actions/checkout@v4

- name: Remove Branch From Netlify
run: cd scripts && ./netlify_remove_branch.sh

delete_reports:
name: Delete Reports
runs-on: ubuntu-latest
Expand Down
42 changes: 19 additions & 23 deletions .github/workflows/release-branch-pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ env:
MENDABLE_API_KEY: ${{ secrets.MENDABLE_API_KEY }}
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}
FULLSTORY_ORGID: ${{ secrets.FULLSTORY_ORGID }}
ALGOLIA_ADMIN_KEY: ${{ secrets.DEV_ALGOLIA_ADMIN_KEY }}
ALGOLIA_APP_ID: ${{ secrets.DEV_GATSBY_ALGOLIA_APP_ID }}
ALGOLIA_SEARCH_KEY: ${{ secrets.DEV_GATSBY_ALGOLIA_SEARCH_KEY }}
GITHUB_BRANCH: ${{ github.ref_name }}


concurrency:
Expand All @@ -32,34 +37,25 @@ jobs:
node-version: "20"
cache: "npm"

- name: Determine branch name
id: extract_branch
run: |
if [ "${{ github.event_name }}" = "pull_request" ]; then
echo "GITHUB_BRANCH=${{ github.head_ref }}" >> $GITHUB_ENV
else
echo "GITHUB_BRANCH=${{ github.ref_name }}" >> $GITHUB_ENV
fi
- run: npm ci

- name: Update Netlify for Collab Drawer
run: cd scripts && ./netlify_add_branch.sh

- name: Post Netlify progress
uses: mshick/add-pr-comment@v2
with:
message: |
🤖 Starting the Netlify preview build for commit ${{ github.sha }}. This may take a few minutes.
refresh-message-position: true


- name: Deploy to Netlify
id: netlify
uses: nwtgck/actions-netlify@v3.0.0
with:
publish-dir: ./build
deploy-message: 'Manual Netlify deployment from GitHub Actions - ${{ github.sha }}'
enable-pull-request-comment: true
overwrites-pull-request-comment: true
enable-commit-comment: true

- name: Post Netlify URL
uses: mshick/add-pr-comment@v2
with:
message: |
🚀 Netlify preview deployed succesfully for commit ${{ github.sha }}. Click [here](${{steps.netlify.outputs.deploy-url}}) to preview the changes.
message-failure: |
👎 Uh oh! The Netlify Preview failed to deploy for commit ${{ github.sha }}. Please check the Netlify logs for more information.
refresh-message-position: true
update-only: true
🤖 Netlify configured to enable preview build for branch: ${{env.GITHUB_BRANCH}} . Subsequent commits will automatically trigger a Netlify build preview.
refresh-message-position: false

- run: npm run build
49 changes: 27 additions & 22 deletions scripts/netlify.sh
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
77 changes: 77 additions & 0 deletions scripts/netlify_add_branch.sh
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."
77 changes: 77 additions & 0 deletions scripts/netlify_remove_branch.sh
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."

0 comments on commit b76f2ad

Please sign in to comment.