zc2 0.0.23

P2P compute broker with credit-based billing, WAL, and broker mesh support
name: Deploy

# Trunk-based staging deploy. Fires when the Build workflow completes
# successfully on the trunk (master/main) or a `release/**` branch, so
# every green push to the trunk ships to staging automatically — staging
# always reflects the head of master (the yoii-mcs model). release/**
# also deploys to staging as a release candidate; production ships from
# the v* tag via release.yml. Gating on Build success keeps a red build
# from deploying, without adding a test step to this fast path.

on:
  workflow_run:
    workflows: ["Build"]
    types: [completed]
    branches:
      - master
      - main
      - 'release/**'
  workflow_dispatch:

env:
  FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: "true"

jobs:
  deploy-staging:
    if: github.event_name == 'workflow_dispatch' || github.event.workflow_run.conclusion == 'success'
    runs-on: cpu
    timeout-minutes: 10
    steps:
      - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0  # v7.0.0
        with:
          # workflow_run context resolves github.sha to the default branch.
          # Pin to the actual SHA that triggered Build so the deploy
          # payload points at the right commit.
          ref: ${{ github.event.workflow_run.head_sha || github.sha }}
      - name: Deploy to staging
        env:
          DEPLOY_SHA: ${{ github.event.workflow_run.head_sha || github.sha }}
          DEPLOY_PAT: ${{ secrets.DEPLOY_PAT }}
        run: |
          set -euo pipefail

          # Fail loudly on a missing secret. Previously an unset DEPLOY_PAT sent
          # `Authorization: token ` and GitHub replied 401 Bad credentials — but
          # curl without --fail exits 0, so the step went green and this job
          # reported a successful deploy that never dispatched anything. This
          # repo shipped "successful" staging deploys that did nothing at all.
          if [ -z "${DEPLOY_PAT:-}" ]; then
            echo "::error::DEPLOY_PAT is not set on zakuro-ai/zc — cannot dispatch the staging deploy."
            exit 1
          fi

          # Capture the status separately: repository_dispatch returns 204 with an
          # empty body on success, so the body alone cannot be asserted on.
          STATUS=$(curl -sS -X POST \
            -o /tmp/dispatch-body.txt -w '%{http_code}' \
            -H "Authorization: token ${DEPLOY_PAT}" \
            -H "Accept: application/vnd.github+json" \
            https://api.github.com/repos/zakuro-ai/zakuro-infra/dispatches \
            -d "{\"event_type\":\"deploy-staging\",\"client_payload\":{\"service\":\"zc\",\"sha\":\"$DEPLOY_SHA\"}}")

          if [ "$STATUS" != "204" ]; then
            echo "::error::zakuro-infra dispatch failed (HTTP $STATUS) — staging was NOT deployed."
            cat /tmp/dispatch-body.txt
            exit 1
          fi
          echo "Dispatched deploy-staging to zakuro-infra for zc @ ${DEPLOY_SHA} (HTTP 204)."