From 2d5ac6dda60f5b584de9a08a3123e7cccc18df83 Mon Sep 17 00:00:00 2001 From: Ricardo Pinto Date: Fri, 9 May 2025 15:57:44 +0100 Subject: [PATCH] Fixed multiple labels not working on ephemeral staging ref no-issue - Fixed a bug where multiple labels would not work on the ephemeral staging routing. It needs to add a route per label (site). --- .github/workflows/cicd.yml | 22 +++++++++---------- .../workflows/ephemeral-staging-teardown.yml | 7 +++--- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/.github/workflows/cicd.yml b/.github/workflows/cicd.yml index 376c663d3..b1ff953fe 100644 --- a/.github/workflows/cicd.yml +++ b/.github/workflows/cicd.yml @@ -250,24 +250,24 @@ jobs: # Get current config gcloud compute url-maps export stg-activitypub --global --project ${GCP_PROJECT} > config.yml # Delete unnecessary fields - yq 'del(.fingerprint)' config.yml -i - yq 'del(.creationTimestamp)' config.yml -i + yq -i 'del(.fingerprint)' config.yml + yq -i 'del(.creationTimestamp)' config.yml export DEFAULT_SERVICE="https://www.googleapis.com/compute/v1/projects/ghost-activitypub/global/backendServices/stg-netherlands-activitypub-api" export PR_SERVICE="https://www.googleapis.com/compute/v1/projects/ghost-activitypub/global/backendServices/stg-pr-${{ github.event.pull_request.number }}-api" # Add host rules and path matchers if they don't exist - yq '.hostRules = (.hostRules // [{"hosts": ["activitypub.ghostinfra.net"], "pathMatcher": "staging-environments"}])' config.yml > config.yml.tmp - mv config.yml.tmp config.yml - yq '.pathMatchers = (.pathMatchers // [{"name": "staging-environments", "defaultService": "'"$DEFAULT_SERVICE"'", "routeRules": []}])' config.yml > config.yml.tmp - mv config.yml.tmp config.yml + yq -i '.hostRules = (.hostRules // [{"hosts": ["activitypub.ghostinfra.net"], "pathMatcher": "staging-environments"}])' config.yml + yq -i '.pathMatchers = (.pathMatchers // [{"name": "staging-environments", "defaultService": "'"$DEFAULT_SERVICE"'", "routeRules": []}])' config.yml # Remove existing route rules for the PR service - yq '.pathMatchers[] |= (.routeRules |= map(select((.routeAction.weightedBackendServices // []) | length == 0 or .routeAction.weightedBackendServices[0].backendService != env(PR_SERVICE))))' config.yml > config.yml.tmp - mv config.yml.tmp config.yml + yq -i '.pathMatchers[] |= (.routeRules |= map(select((.routeAction.weightedBackendServices // []) | length == 0 or .routeAction.weightedBackendServices[0].backendService != env(PR_SERVICE))))' config.yml # Add new route rules for the PR service export MAX_PRIORITY=$(yq '[.pathMatchers[] | select(.name == "staging-environments") | .routeRules[]?.priority] | max // 0' config.yml) export NEXT_PRIORITY=$((MAX_PRIORITY + 1)) - export HEADER_MATCHES=$(echo "$LABELS" | jq -c '[.[] | select(.name | test("\\.ghost\\.is$")) | { "headerName": "X-Forwarded-Host", "exactMatch": "\(.name)" }']) - yq '.pathMatchers[0].routeRules += [{"priority": '"$NEXT_PRIORITY"', "matchRules": [{"prefixMatch": "/", "headerMatches": '$HEADER_MATCHES'}], "routeAction": {"weightedBackendServices": [ { "backendService": "'$PR_SERVICE'", "weight": 100 } ] } }]' config.yml > config.yml.tmp - mv config.yml.tmp config.yml + LABELS_JSON=$(echo "$LABELS" | jq -c '[.[] | select(.name | test("\\.ghost\\.is$")) | .name]') + for LABEL in $(echo "$LABELS_JSON" | jq -r '.[]'); do + echo "Adding route for label: $LABEL" + yq -i '.pathMatchers[0].routeRules += [{"priority": '$NEXT_PRIORITY', "matchRules": [{"prefixMatch": "/", "headerMatches": [{ "headerName": "X-Forwarded-Host", "exactMatch": "'$LABEL'" }]}], "routeAction": {"weightedBackendServices": [ { "backendService": "'$PR_SERVICE'", "weight": 100 } ] } }]' config.yml + export NEXT_PRIORITY=$((NEXT_PRIORITY + 1)) + done echo "Updating url map with:" cat config.yml gcloud compute url-maps import stg-activitypub --source=config.yml --global --project ${GCP_PROJECT} --quiet diff --git a/.github/workflows/ephemeral-staging-teardown.yml b/.github/workflows/ephemeral-staging-teardown.yml index 4628aedde..469380a97 100644 --- a/.github/workflows/ephemeral-staging-teardown.yml +++ b/.github/workflows/ephemeral-staging-teardown.yml @@ -100,12 +100,11 @@ jobs: # Get current config gcloud compute url-maps export stg-activitypub --global --project ${GCP_PROJECT} > config.yml # Delete unnecessary fields - yq 'del(.fingerprint)' config.yml -i - yq 'del(.creationTimestamp)' config.yml -i + yq -i 'del(.fingerprint)' config.yml + yq -i 'del(.creationTimestamp)' config.yml export PR_SERVICE="https://www.googleapis.com/compute/v1/projects/ghost-activitypub/global/backendServices/stg-pr-${PR_NUMBER}-api" # Remove existing route rules for the PR service - yq '.pathMatchers[] |= (.routeRules |= map(select((.routeAction.weightedBackendServices // []) | length == 0 or .routeAction.weightedBackendServices[0].backendService != env(PR_SERVICE))))' config.yml > config.yml.tmp - mv config.yml.tmp config.yml + yq -i '.pathMatchers[] |= (.routeRules |= map(select((.routeAction.weightedBackendServices // []) | length == 0 or .routeAction.weightedBackendServices[0].backendService != env(PR_SERVICE))))' config.yml echo "Updating url map with:" cat config.yml gcloud compute url-maps import stg-activitypub --source=config.yml --global --project ${GCP_PROJECT} --quiet