airi/.github/workflows/deploy-server-dev.yml

130 lines
4.9 KiB
YAML

name: Deploy to server-dev
run-name: Deploy ${{ inputs.branch }} to server-dev (@${{ github.actor }})
on:
workflow_dispatch:
inputs:
branch:
description: 'Branch of this repository to deploy'
required: true
type: string
reset_database:
description: 'Reset the server-dev Neon branch to its parent before the deploy'
required: true
type: boolean
default: true
# The dev environment is one shared slot, so runs do not overlap. A run holds
# the group until Railway finishes its deploys. GitHub keeps one pending run
# and cancels an older pending run, so the latest request is the next deploy.
concurrency:
group: server-dev
cancel-in-progress: false
permissions:
contents: read
# Starts the "Cloudflare Workers (server-dev)" workflow.
actions: write
jobs:
deploy:
name: Deploy - server-dev
runs-on: ubuntu-latest
env:
DEPLOY_BRANCH: ${{ inputs.branch }}
GH_TOKEN: ${{ github.token }}
# A project token for the Railway dev environment. It selects the
# project and the environment for each railway command.
RAILWAY_TOKEN: ${{ secrets.RAILWAY_DEV_TOKEN }}
steps:
# Stop before the database reset if no deploy can follow it.
- name: Check the branch
run: gh api "repos/${GITHUB_REPOSITORY}/branches/${DEPLOY_BRANCH}" --silent
# The API runs migrations on startup, and the Drizzle migrator only
# applies migrations newer than the last applied one. A database that
# another branch migrated makes it skip older migrations of this branch
# without an error. Reset first so that the new deploy migrates a clean
# baseline.
- name: Reset the Neon branch
if: inputs.reset_database
uses: neondatabase/reset-branch-action@v1
with:
project_id: ${{ vars.NEON_PROJECT_ID }}
branch: ${{ vars.NEON_SERVER_DEV_BRANCH }}
parent: true
api_key: ${{ secrets.NEON_API_KEY }}
- uses: actions/setup-node@v6
with:
node-version: '^26.12.1'
check-latest: true
- run: npm install --global @railway/cli@5
# NOTICE:
# `railway service source connect` is the direct command for a source
# change, but Railway rejects it for a project token with "Unauthorized".
# A project token can change the same field through an environment
# config edit, the path that `railway config apply` uses.
# Evidence: run 38268809740 failed, run 38270278514 passed the edit.
# Use `service source connect` again when Railway accepts project tokens
# for it.
#
# `redeploy --from-source` also covers a run that keeps the same branch.
# The services must restart there to migrate the reset database.
#
# The step waits for a new deployment to pass its healthcheck. A run that
# ends earlier lets the next run reset the database while this branch
# still builds, and this branch then migrates the reset database.
- name: Deploy the Railway dev services from the branch
timeout-minutes: 30
run: |
services=(ts-api auth-server)
declare -A previous
for service in "${services[@]}"; do
previous[$service]=$(railway service status --service "$service" --json | jq --raw-output '.deploymentId')
railway environment edit --service-config "$service" source.branch "$DEPLOY_BRANCH" --message "Deploy $DEPLOY_BRANCH to server-dev"
railway service redeploy --service "$service" --from-source --yes
done
for service in "${services[@]}"; do
while true; do
latest=$(railway service status --service "$service" --json)
id=$(jq --raw-output '.deploymentId' <<< "$latest")
status=$(jq --raw-output '.status' <<< "$latest")
if [[ "$id" != "${previous[$service]}" ]]; then
case "$status" in
SUCCESS) break ;;
FAILED | CRASHED)
echo "::error::The $service deployment $id ended with status $status"
exit 1
;;
esac
fi
sleep 15
done
done
# Railway follows later pushes to the branch. The frontend does not, so
# each run builds it from the same branch.
- name: Deploy the frontend
run: gh workflow run deploy-cloudflare-workers-dev-server.yml --repo "$GITHUB_REPOSITORY" --ref "$DEPLOY_BRANCH"
- name: Summary
env:
RESET_DATABASE: ${{ inputs.reset_database }}
run: |
{
echo "| | |"
echo "| --- | --- |"
echo "| Branch | \`${DEPLOY_BRANCH}\` |"
echo "| Database reset | ${RESET_DATABASE} |"
echo "| Actor | @${GITHUB_ACTOR} |"
} >> "$GITHUB_STEP_SUMMARY"