Files
classroomio/.github/workflows/railway-preview-teardown.yml
Abdulmuizz Hamzat 8af0675895 fix: minio bucket seed race (#820)
* fix(workflows): retry MinIO bucket creation to survive post-boot format window

A freshly duplicated cio-minio's HTTP listener accepts connections
before it finishes formatting its storage backend (confirmed via its
own boot logs: "Formatting 1st pool, 1 set(s), 1 drives per set."),
so a bucket-creation call landing in that window can transiently fail
with a misleading "specified bucket does not exist" response even
though auth and connectivity (mc alias set) already succeeded. Retry
bucket creation up to 10 times with a 5s backoff instead of failing
the whole run on what's usually just a not-ready-yet response.

* fix(workflows): truncate env name slug to fit Railway's length limit

railway environment new failed with "Error in name - Invalid input"
for a 48-char name (pr-cursor-notes-workspace-feedback-6d09-0e6aa7c9)
derived from a long auto-generated branch name. Railway docs cite a
32-char limit for service names; environment names hit the same wall.

The collision-resistant hash suffix (added for the earlier CodeRabbit
finding) is what actually guarantees uniqueness -- the slug is only
there for readability -- so cap the slug at 18 chars instead of
dropping the hash. pr-<slug><=18>-<hash8> stays at or under 30 chars.

* fix(workflows): retry mc anonymous set for the same boot-race window

The mb retry loop worked as intended in the latest test run (attempt
1 failed on all three buckets during cio-minio's post-boot format
window, attempt 2 succeeded), but the very next call -- anonymous set
download on cio/media -- hit the identical "specified bucket does not
exist" error right after mb had just succeeded. Same underlying race,
one command later. Wrap it in the same retry pattern.

* update workflow

* fix: silen error on db:seed

* update workflow

* update workflow

* update workflow
2026-08-03 02:05:10 +01:00

62 lines
2.8 KiB
YAML

name: Railway Preview Teardown
# Auto-delete a preview environment created by railway-preview.yml when its PR closes,
# so manually-spun previews don't linger and accrue Railway cost. (Native Railway PR
# environments clean themselves up; this covers the pr-<branch> envs made by the manual
# workflow.) Deletion is best-effort — if no matching env exists, the run is a no-op.
#
# Uses the same RAILWAY_API_TOKEN + RAILWAY_PROJECT_ID secrets as the deploy workflow.
# Note: secrets are unavailable to PRs from forks, so this only tears down previews for
# same-repo branches (which is where manual previews come from anyway).
on:
pull_request:
types: [closed]
concurrency:
group: railway-preview-${{ github.event.pull_request.head.ref }}
cancel-in-progress: false
jobs:
teardown:
runs-on: ubuntu-latest
if: github.event.pull_request.head.repo.full_name == github.repository
env:
RAILWAY_API_TOKEN: ${{ secrets.RAILWAY_API_TOKEN }}
RAILWAY_PROJECT_ID: ${{ secrets.RAILWAY_PROJECT_ID }}
RAILWAY_WORKSPACE: ${{ vars.RAILWAY_WORKSPACE || 'cio projects' }}
BASE_ENV: ${{ vars.RAILWAY_BASE_ENV || 'staging' }}
steps:
- name: Install Railway CLI
run: npm install -g @railway/cli@5.30.1
- name: Link to base project
# `railway environment delete` takes no --project/--workspace/--environment
# flags — only `railway link` does, so it must run first to establish that
# context (same requirement as railway-preview.yml).
run: |
railway link --project "$RAILWAY_PROJECT_ID" --workspace "$RAILWAY_WORKSPACE" --environment "$BASE_ENV"
- name: Delete the preview environment
# Mirror the name derivation in railway-preview.yml so the env name matches.
# HEAD_REF is bound via env (not interpolated into the script) so a malicious
# branch name can never be executed as shell — Actions expands ${{ }} as text
# BEFORE bash parses it, so a raw `${{ ...head.ref }}` here is a script-injection hole.
env:
HEAD_REF: ${{ github.event.pull_request.head.ref }}
run: |
slug=$(echo "$HEAD_REF" | tr '[:upper:]/_.' '[:lower:]---' | sed 's/[^a-z0-9-]//g' | sed 's/--*/-/g; s/^-//; s/-$//' | cut -c1-18 | sed 's/-$//')
hash=$(echo -n "$HEAD_REF" | sha256sum | cut -c1-8)
env_name="pr-${slug}-${hash}"
echo "Attempting to delete environment: ${env_name}"
if ! output=$(railway environment delete "${env_name}" --yes 2>&1); then
echo "$output"
if echo "$output" | grep -qi "not found"; then
echo "No matching preview environment (or already deleted) — nothing to do."
else
exit 1
fi
else
echo "$output"
fi