Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 5 additions & 89 deletions .github/actions/wait-for-deployment/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ inputs:
env_name:
description: The environment name (e.g., dev, pr-123)
required: true
max_wait_minutes:
description: Maximum number of minutes to wait for deployment
max_wait_seconds:
description: Maximum number of seconds to wait for deployment
required: false
default: '30'
default: '1200'
pantheon_machine_token:
description: Pantheon machine token for API authentication
required: true
Expand Down Expand Up @@ -47,92 +47,8 @@ runs:
env:
PANTHEON_SITE_NAME: ${{ inputs.pantheon_site_name }}
ENV_NAME: ${{ inputs.env_name }}
MAX_WAIT_MINUTES: ${{ inputs.max_wait_minutes }}
MAX_WAIT_SECONDS: ${{ inputs.max_wait_minutes }}
PANTHEON_MACHINE_TOKEN: ${{ inputs.pantheon_machine_token }}
EXPECTED_COMMIT: ${{ inputs.expected_commit }}
run: |
echo "Waiting for Pantheon deployment to complete..."
echo "Site: ${PANTHEON_SITE_NAME}.${ENV_NAME}"
echo "Expected commit: ${EXPECTED_COMMIT}"

# Get site UUID
SITE_UUID=$(terminus site:info "${PANTHEON_SITE_NAME}" --field=id)
echo "Site UUID: $SITE_UUID"

# Exchange machine token for API session token
SESSION_RESPONSE=$(curl -s -X POST https://api.pantheon.io/v0/authorize/machine-token \
-H "Content-Type: application/json" \
-d "{\"machine_token\": \"${PANTHEON_MACHINE_TOKEN}\", \"client\": \"github-actions\"}")

SESSION_TOKEN=$(echo "$SESSION_RESPONSE" | jq -r '.session')

if [ -z "$SESSION_TOKEN" ] || [ "$SESSION_TOKEN" == "null" ]; then
echo "::error::Failed to get API session token"
echo "Response: $SESSION_RESPONSE"
exit 1
fi

echo "Authenticated with Pantheon API"

# Calculate max attempts based on wait minutes
SLEEP_TIME=10
MAX_ATTEMPTS=$(( (MAX_WAIT_MINUTES * 60) / SLEEP_TIME ))
ATTEMPT=0

echo "Will check up to $MAX_ATTEMPTS times (${MAX_WAIT_MINUTES} minutes)"

while [ $ATTEMPT -lt $MAX_ATTEMPTS ]; do
ATTEMPT=$((ATTEMPT + 1))
echo "Check $ATTEMPT of $MAX_ATTEMPTS..."

# Get build list for this environment (Next.js-specific endpoint)
BUILDS_RESPONSE=$(curl -s "https://terminus.pantheon.io/api/sites/$SITE_UUID/environment/${ENV_NAME}/build/list?limit=10" \
-H "X-Pantheon-Session: $SESSION_TOKEN")

# Validate response is JSON before parsing (API may return HTML errors)
if ! echo "$BUILDS_RESPONSE" | jq -e . >/dev/null 2>&1; then
echo "Received non-JSON response, retrying..."
sleep $SLEEP_TIME
continue
fi

# Find build matching this commit SHA
BUILD=$(echo "$BUILDS_RESPONSE" | jq -r ".[] | select(.commit == \"$EXPECTED_COMMIT\")" 2>/dev/null)

if [ -z "$BUILD" ] || [ "$BUILD" == "null" ]; then
echo "No build found yet for commit ${EXPECTED_COMMIT:0:7}"
sleep $SLEEP_TIME
continue
fi

BUILD_ID=$(echo "$BUILD" | jq -r '.id')
BUILD_STATUS=$(echo "$BUILD" | jq -r '.status')
BUILD_COMMIT=$(echo "$BUILD" | jq -r '.commit')

echo "Build ID: $BUILD_ID"
echo "Commit: ${BUILD_COMMIT:0:7}"
echo "Status: $BUILD_STATUS"

if [ "$BUILD_STATUS" == "DEPLOYMENT_SUCCESS" ]; then
echo "Deployment successful"
echo "deployment_ready=true" >> $GITHUB_OUTPUT
echo "build_status=$BUILD_STATUS" >> $GITHUB_OUTPUT
exit 0
elif [[ "$BUILD_STATUS" == *"FAILURE"* ]]; then
echo "::error::Build or deployment failed (status: $BUILD_STATUS)"
echo "deployment_ready=false" >> $GITHUB_OUTPUT
echo "build_status=$BUILD_STATUS" >> $GITHUB_OUTPUT
exit 1
elif [[ "$BUILD_STATUS" == *"WORKING"* ]] || [[ "$BUILD_STATUS" == *"QUEUED"* ]]; then
echo "Still processing..."
else
echo "Status: $BUILD_STATUS"
fi

sleep $SLEEP_TIME
done

echo "::error::Timeout after ${MAX_WAIT_MINUTES} minutes"
echo "deployment_ready=false" >> $GITHUB_OUTPUT
echo "build_status=TIMEOUT" >> $GITHUB_OUTPUT
exit 1
terminus node:builds:wait ${PANTHEON_SITE_NAME}.${ENV_NAME} --commit=${EXPECTED_COMMIT} --max=${MAX_WAIT_SECONDS}
Loading