Skip to content

Commit 990b32f

Browse files
fix: Update post_deploy.sh to handle service principal ID resolution in non-interactive mode
1 parent ed85d80 commit 990b32f

2 files changed

Lines changed: 37 additions & 16 deletions

File tree

.github/workflows/job-deploy-linux.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,9 @@ jobs:
352352
BACKEND_URL: ${{ steps.get_output_linux.outputs.BACKEND_URL }}
353353
AZURE_STORAGE_ACCOUNT_NAME: ${{ steps.get_output_linux.outputs.AZURE_STORAGE_ACCOUNT_NAME }}
354354
AZURE_AI_SEARCH_NAME: ${{ steps.get_output_linux.outputs.AZURE_AI_SEARCH_NAME }}
355+
# Needed by post_deploy.sh to resolve the principal id when the workflow
356+
# is signed in as a service principal (no interactive user).
357+
AZURE_CLIENT_ID: ${{ secrets.AZURE_CLIENT_ID }}
355358
run: |
356359
set -e
357360

infra/scripts/post_deploy.sh

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -253,22 +253,27 @@ enable_public_access_if_waf() {
253253
}
254254

255255
get_value_from_deployment() {
256-
local deployment_outputs="$1"
257-
local primary_key="$2"
258-
local fallback_key="$3"
259-
260-
python3 - <<PY
261-
import json
262-
import sys
263-
outputs = json.load(sys.stdin)
264-
keys = ["$primary_key", "$fallback_key"]
256+
# Deployment outputs JSON is piped on stdin; positional args are key names.
257+
# JSON is forwarded via an env var because `python3 - <<PY` would otherwise
258+
# consume stdin for the heredoc, leaving nothing for json.load(sys.stdin).
259+
local primary_key="$1"
260+
local fallback_key="$2"
261+
local outputs_json
262+
outputs_json="$(cat)"
263+
264+
DEPLOYMENT_OUTPUTS_JSON="$outputs_json" \
265+
PRIMARY_KEY="$primary_key" \
266+
FALLBACK_KEY="$fallback_key" \
267+
python3 - <<'PY'
268+
import json, os, sys
269+
outputs = json.loads(os.environ.get("DEPLOYMENT_OUTPUTS_JSON", "") or "{}")
270+
keys = [k for k in (os.environ.get("PRIMARY_KEY", ""), os.environ.get("FALLBACK_KEY", "")) if k]
265271
output_keys = {k.lower(): k for k in outputs}
266272
for key in keys:
267-
for candidate in [key, key.lower(), key.upper(), key.capitalize()]:
268-
actual = output_keys.get(candidate.lower())
269-
if actual and isinstance(outputs[actual], dict) and outputs[actual].get("value") is not None:
270-
print(outputs[actual]["value"])
271-
sys.exit(0)
273+
actual = output_keys.get(key.lower())
274+
if actual and isinstance(outputs[actual], dict) and outputs[actual].get("value") is not None:
275+
print(outputs[actual]["value"])
276+
sys.exit(0)
272277
sys.exit(1)
273278
PY
274279
}
@@ -678,9 +683,22 @@ main() {
678683
echo "==============================================="
679684
echo ""
680685

681-
user_principal_id="$(az ad signed-in-user show --query id -o tsv 2>/dev/null || true)"
686+
# Resolve the principal id to use for team-config uploads. In CI the workflow
687+
# logs in as a service principal (OIDC), so `az ad signed-in-user show` returns
688+
# nothing. Fall back to an explicit USER_PRINCIPAL_ID env var, then to the SP
689+
# object id looked up via AZURE_CLIENT_ID.
690+
if [ -n "${USER_PRINCIPAL_ID:-}" ]; then
691+
user_principal_id="$USER_PRINCIPAL_ID"
692+
info "Using principal id from USER_PRINCIPAL_ID env var."
693+
else
694+
user_principal_id="$(az ad signed-in-user show --query id -o tsv 2>/dev/null || true)"
695+
if [ -z "$user_principal_id" ] && [ -n "${AZURE_CLIENT_ID:-}" ]; then
696+
info "No interactive user — falling back to service principal object id (AZURE_CLIENT_ID=$AZURE_CLIENT_ID)."
697+
user_principal_id="$(az ad sp show --id "$AZURE_CLIENT_ID" --query id -o tsv 2>/dev/null || true)"
698+
fi
699+
fi
682700
if [ -z "$user_principal_id" ]; then
683-
fatal "Could not retrieve signed-in user principal id."
701+
fatal "Could not retrieve signed-in user principal id. In CI, set USER_PRINCIPAL_ID or ensure AZURE_CLIENT_ID is exported and the SP is visible to Microsoft Graph."
684702
fi
685703

686704
activate_python_env

0 commit comments

Comments
 (0)