diff --git a/.github/workflows/staging.yml b/.github/workflows/staging.yml index 2a87188..d8bb37d 100644 --- a/.github/workflows/staging.yml +++ b/.github/workflows/staging.yml @@ -3,18 +3,18 @@ name: Staging deployment on: push: branches-ignore: [main] + pull_request: + types: [opened, reopened] delete: concurrency: - group: staging-${{ github.event.ref }} + group: staging-${{ github.event.pull_request.head.ref || github.event.ref }} cancel-in-progress: true jobs: deploy: if: github.event_name == 'push' runs-on: ubuntu-latest - permissions: - pull-requests: write env: BRANCH: ${{ github.ref_name }} FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }} @@ -60,6 +60,28 @@ jobs: - name: Summary run: echo "Deployed to https://${HOST}" >> "$GITHUB_STEP_SUMMARY" + # Comment the staging URL on the branch's PR. Runs both after a successful + # push-deploy (covers PRs that already exist) and when a PR is opened or + # reopened (covers the common case of pushing the branch before opening the + # PR, where the deploy ran with no PR to comment on). The comment is deduped + # by body, so the two paths never double-post. + comment: + needs: [deploy] + if: | + always() && + (github.event_name == 'pull_request' || + (github.event_name == 'push' && needs.deploy.result == 'success')) + runs-on: ubuntu-latest + permissions: + pull-requests: write + env: + BRANCH: ${{ github.head_ref || github.ref_name }} + steps: + - name: Compute staging host + run: | + SLUG=$(echo "$BRANCH" | tr '[:upper:]' '[:lower:]' | sed -E 's/[^a-z0-9-]+/-/g; s/-+/-/g; s/^-//; s/-$//' | cut -c1-30 | sed 's/-*$//') + echo "HOST=timetracker-staging-${SLUG}.fly.dev" >> "$GITHUB_ENV" + - name: Comment staging URL on PR uses: actions/github-script@v7 with: