From 17d9fa5f9215ffa9d9b863373d8389be5d0e4ae7 Mon Sep 17 00:00:00 2001 From: Karl Cardenas Date: Wed, 22 May 2024 10:55:45 -0700 Subject: [PATCH] =?UTF-8?q?[version-4-0]=20ci:=20updated=20logic=20to=20en?= =?UTF-8?q?able=20Netlify=20previews=20for=20release=20branch=20PRs=20(?= =?UTF-8?q?=E2=80=A6=20(#2894)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ci: updated logic to enable Netlify previews for release branch PRs (#2888) * chore: fix comment --- .github/workflows/clean-up-report.yaml | 15 +++++ .github/workflows/release-branch-pr.yaml | 42 ++++++------- scripts/netlify.sh | 49 ++++++++------- scripts/netlify_add_branch.sh | 77 ++++++++++++++++++++++++ scripts/netlify_remove_branch.sh | 77 ++++++++++++++++++++++++ 5 files changed, 215 insertions(+), 45 deletions(-) create mode 100755 scripts/netlify_add_branch.sh create mode 100755 scripts/netlify_remove_branch.sh diff --git a/.github/workflows/clean-up-report.yaml b/.github/workflows/clean-up-report.yaml index a81244318b..72113a27e9 100644 --- a/.github/workflows/clean-up-report.yaml +++ b/.github/workflows/clean-up-report.yaml @@ -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 diff --git a/.github/workflows/release-branch-pr.yaml b/.github/workflows/release-branch-pr.yaml index a563ceca89..792ed9c1cd 100644 --- a/.github/workflows/release-branch-pr.yaml +++ b/.github/workflows/release-branch-pr.yaml @@ -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: @@ -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 diff --git a/scripts/netlify.sh b/scripts/netlify.sh index 15f0922824..c1a92f232c 100755 --- a/scripts/netlify.sh +++ b/scripts/netlify.sh @@ -1,12 +1,15 @@ #!/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) @@ -14,25 +17,27 @@ 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. 1 is allowed, 0 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 \ No newline at end of file diff --git a/scripts/netlify_add_branch.sh b/scripts/netlify_add_branch.sh new file mode 100755 index 0000000000..5bdd17c30d --- /dev/null +++ b/scripts/netlify_add_branch.sh @@ -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." diff --git a/scripts/netlify_remove_branch.sh b/scripts/netlify_remove_branch.sh new file mode 100755 index 0000000000..823fae45a7 --- /dev/null +++ b/scripts/netlify_remove_branch.sh @@ -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."