@@ -62,14 +62,15 @@ runs:
6262 fi
6363
6464 # Piggyback on the VM's stickyDiskToken for phone-home authentication.
65- # This is a Sanctum token that proves the caller is a real Blacksmith VM
66- # belonging to the claimed installation — it's only accessible from inside
67- # the VM via the metadata service and is not publicly visible.
65+ # It proves the caller is a real Blacksmith VM belonging to the claimed
66+ # installation, and the backend binds it to the runner that adopted this
67+ # testbox's run. It's only accessible from inside the VM via the metadata
68+ # service and is not publicly visible.
6869 AUTH_TOKEN=$(curl -s --connect-timeout 2 --max-time 5 "http://${METADATA_ADDR}/stickyDiskToken")
6970
7071 if [ -z "$API_URL" ] || [ -z "$INSTALLATION_MODEL_ID" ] || [ -z "$AUTH_TOKEN" ]; then
71- echo "Warning: could not read required metadata (backendURL, installationModelID, stickyDiskToken)"
72- exit 0
72+ echo "::error::Could not read required metadata (backendURL, installationModelID, stickyDiskToken); the testbox cannot phone home. "
73+ exit 1
7374 fi
7475
7576 if [ -n "$BLACKSMITH_HOSTNAME" ]; then
@@ -79,22 +80,55 @@ runs:
7980 fi
8081 RUNNER_SSH_PORT="${BLACKSMITH_SSH_PORT:-22}"
8182
82- RESPONSE=$(curl -s -f -X POST "${API_URL}/api/testbox/phone-home" \
83- -H "Content-Type: application/json" \
84- -H "Authorization: Bearer ${AUTH_TOKEN}" \
85- -d "{
86- \"testbox_id\": \"${TESTBOX_ID}\",
87- \"installation_model_id\": ${INSTALLATION_MODEL_ID},
88- \"status\": \"hydrating\",
89- \"ip_address\": \"${RUNNER_HOST}\",
90- \"ssh_port\": \"${RUNNER_SSH_PORT}\",
91- \"working_directory\": \"${GITHUB_WORKSPACE}\",
92- \"adopted_run_id\": \"${GITHUB_RUN_ID}\",
93- \"metadata\": {}
94- }" 2>/dev/null) || {
95- echo "Warning: phone-home (hydrating) failed, continuing in degraded mode"
96- RESPONSE=""
97- }
83+ # The first phone-home can race warmup's write of the dispatched run ID
84+ # and the adoption webhook. The backend answers 503 + Retry-After while
85+ # that binding is pending, so retry transient failures up to a deadline
86+ # and fail the job if the handshake never lands: without it the CLI's
87+ # SSH key is never installed and the testbox is unusable.
88+ PHONE_HOME_DEADLINE=$(( $(date +%s) + 90 ))
89+ PHONE_HOME_RETRY_DELAY=2
90+ PHONE_HOME_RESPONSE_FILE="$STATE/phone_home_response"
91+ ATTEMPT=0
92+ while true; do
93+ ATTEMPT=$((ATTEMPT + 1))
94+ HTTP_CODE=$(curl -s -o "$PHONE_HOME_RESPONSE_FILE" -w '%{http_code}' --max-time 30 \
95+ -X POST "${API_URL}/api/testbox/phone-home" \
96+ -H "Content-Type: application/json" \
97+ -H "Authorization: Bearer ${AUTH_TOKEN}" \
98+ -d "{
99+ \"testbox_id\": \"${TESTBOX_ID}\",
100+ \"installation_model_id\": ${INSTALLATION_MODEL_ID},
101+ \"status\": \"hydrating\",
102+ \"ip_address\": \"${RUNNER_HOST}\",
103+ \"ssh_port\": \"${RUNNER_SSH_PORT}\",
104+ \"working_directory\": \"${GITHUB_WORKSPACE}\",
105+ \"adopted_run_id\": \"${GITHUB_RUN_ID}\",
106+ \"metadata\": {}
107+ }") || HTTP_CODE=000
108+
109+ case "$HTTP_CODE" in
110+ 2??)
111+ break
112+ ;;
113+ 000|408|425|429|5??)
114+ ;;
115+ *)
116+ echo "::error::Testbox phone-home rejected (HTTP ${HTTP_CODE}): $(cat "$PHONE_HOME_RESPONSE_FILE" 2>/dev/null)"
117+ exit 1
118+ ;;
119+ esac
120+
121+ if [ "$(date +%s)" -ge "$PHONE_HOME_DEADLINE" ]; then
122+ echo "::error::Testbox phone-home did not succeed before the deadline (last HTTP ${HTTP_CODE}, ${ATTEMPT} attempts): $(cat "$PHONE_HOME_RESPONSE_FILE" 2>/dev/null)"
123+ exit 1
124+ fi
125+
126+ echo "Phone-home not accepted yet (HTTP ${HTTP_CODE}, attempt ${ATTEMPT}); retrying in ${PHONE_HOME_RETRY_DELAY}s"
127+ sleep "$PHONE_HOME_RETRY_DELAY"
128+ done
129+
130+ RESPONSE=$(cat "$PHONE_HOME_RESPONSE_FILE")
131+ rm -f "$PHONE_HOME_RESPONSE_FILE"
98132
99133 echo "$TESTBOX_ID" > "$STATE/testbox_id"
100134 echo "$INSTALLATION_MODEL_ID" > "$STATE/installation_model_id"
@@ -105,16 +139,11 @@ runs:
105139 echo "$GITHUB_WORKSPACE" > "$STATE/working_directory"
106140 echo "$GITHUB_RUN_ID" > "$STATE/adopted_run_id"
107141
108- if [ -n "$RESPONSE" ]; then
109- echo "$RESPONSE" | jq -r '.ssh_public_key // empty' > "$STATE/ssh_public_key"
110- IDLE_TIMEOUT=$(echo "$RESPONSE" | jq -r '.idle_timeout // empty')
111- echo "${IDLE_TIMEOUT:-10}" > "$STATE/idle_timeout"
112- else
113- echo "" > "$STATE/ssh_public_key"
114- echo "10" > "$STATE/idle_timeout"
115- fi
142+ echo "$RESPONSE" | jq -r '.ssh_public_key // empty' > "$STATE/ssh_public_key"
143+ IDLE_TIMEOUT=$(echo "$RESPONSE" | jq -r '.idle_timeout // empty')
144+ echo "${IDLE_TIMEOUT:-10}" > "$STATE/idle_timeout"
116145
117- echo "Testbox initialized: testbox_id=${TESTBOX_ID}"
146+ echo "Testbox initialized: testbox_id=${TESTBOX_ID} (phone-home accepted after ${ATTEMPT} attempt(s)) "
118147
119148 - name : Start heartbeat
120149 if : steps.metadata.outputs.available == 'true' && inputs.testbox_id
0 commit comments