From c36fd2e0be63a6cf652d3c18c70b63e15241cff0 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 16 May 2026 16:36:58 +0000 Subject: [PATCH] Fly deploy: auto-detect org slug for non-interactive apps create `fly apps create` requires `-o ` when no TTY is attached (always in CI). The previous run failed with "org slug must be specified when not running interactively". Auto-detects from the token's accessible orgs (org-scoped tokens return exactly one). Falls back to a FLY_ORG repo variable if the auto-detect picks the wrong org or returns nothing. Handles both array and object shapes of `fly orgs list -j` output to survive flyctl version differences. --- .github/workflows/fly-deploy.yml | 33 ++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/.github/workflows/fly-deploy.yml b/.github/workflows/fly-deploy.yml index 74816df..7f9e0ee 100644 --- a/.github/workflows/fly-deploy.yml +++ b/.github/workflows/fly-deploy.yml @@ -122,14 +122,43 @@ jobs: - name: Ensure Fly app exists env: FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }} + # Optional repo variable escape hatch for multi-org tokens + # or auto-detect edge cases. Set at: + # https://github.com///settings/variables/actions + FLY_ORG_OVERRIDE: ${{ vars.FLY_ORG }} run: | if fly apps list -j | grep -q "\"Name\":\"$FLY_APP_NAME\""; then echo "app $FLY_APP_NAME exists" + exit 0 + fi + + # Non-interactive `fly apps create` needs an org slug. Pick it: + # 1. Honor an explicit FLY_ORG repo variable if set. + # 2. Otherwise auto-detect from the token's accessible orgs + # (org-scoped tokens see exactly one). + if [ -n "$FLY_ORG_OVERRIDE" ]; then + org="$FLY_ORG_OVERRIDE" + echo "using FLY_ORG override: $org" else - echo "creating app $FLY_APP_NAME" - fly apps create "$FLY_APP_NAME" + # fly orgs list -j can return either an array of org objects + # or an object keyed by slug, depending on the flyctl version. + # Handle both shapes. + org=$(fly orgs list -j | jq -r ' + if type == "array" then (.[0].Slug // .[0].slug // empty) + else (keys // [])[0] // empty + end + ') + if [ -z "$org" ] || [ "$org" = "null" ]; then + echo "::error::Could not auto-detect Fly org from token." + echo "::error::Set FLY_ORG repo variable at https://github.com/${{ github.repository }}/settings/variables/actions" + exit 1 + fi + echo "auto-detected Fly org slug: $org" fi + echo "creating app $FLY_APP_NAME in org $org" + fly apps create "$FLY_APP_NAME" -o "$org" + - name: Stage Fly secrets env: FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}