mirror of
https://github.com/whekin/household-bot.git
synced 2026-03-31 18:34:02 +00:00
- Add migration 0020 for rent_payment_destinations jsonb column - Add DB_SCHEMA env var support for multi-schema deployments - Create custom migrate.ts script with proper search_path handling - Update drizzle.config.ts and client.ts to use DB_SCHEMA - Add db_schema variable to Terraform with dev=test/prod=public defaults - Update CD workflow to set DB_SCHEMA based on branch
191 lines
6.8 KiB
YAML
191 lines
6.8 KiB
YAML
name: CD
|
|
|
|
on:
|
|
workflow_run:
|
|
workflows:
|
|
- CI
|
|
types:
|
|
- completed
|
|
branches:
|
|
- main
|
|
- dev
|
|
workflow_dispatch:
|
|
|
|
permissions:
|
|
contents: read
|
|
id-token: write
|
|
|
|
concurrency:
|
|
group: cd-${{ github.ref_name }}
|
|
cancel-in-progress: false
|
|
|
|
jobs:
|
|
check-secrets:
|
|
name: Check deploy prerequisites
|
|
runs-on: ubuntu-latest
|
|
# Select GitHub Environment based on branch
|
|
environment: ${{ github.ref == 'refs/heads/main' && 'Production' || 'Development' }}
|
|
outputs:
|
|
eligible_event: ${{ steps.check.outputs.eligible_event }}
|
|
secrets_ok: ${{ steps.check.outputs.secrets_ok }}
|
|
db_secret_ok: ${{ steps.check.outputs.db_secret_ok }}
|
|
|
|
steps:
|
|
- name: Evaluate trigger and required secrets
|
|
id: check
|
|
env:
|
|
GCP_PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }}
|
|
GCP_WORKLOAD_IDENTITY_PROVIDER: ${{ secrets.GCP_WORKLOAD_IDENTITY_PROVIDER }}
|
|
GCP_SERVICE_ACCOUNT: ${{ secrets.GCP_SERVICE_ACCOUNT }}
|
|
DATABASE_URL: ${{ secrets.DATABASE_URL }}
|
|
run: |
|
|
eligible_event=false
|
|
if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
|
|
eligible_event=true
|
|
elif [[ "${{ github.event_name }}" == "workflow_run" && "${{ github.event.workflow_run.conclusion }}" == "success" ]]; then
|
|
eligible_event=true
|
|
fi
|
|
|
|
secrets_ok=false
|
|
if [[ -n "$GCP_PROJECT_ID" && -n "$GCP_WORKLOAD_IDENTITY_PROVIDER" && -n "$GCP_SERVICE_ACCOUNT" ]]; then
|
|
secrets_ok=true
|
|
fi
|
|
|
|
db_secret_ok=false
|
|
if [[ -n "$DATABASE_URL" ]]; then
|
|
db_secret_ok=true
|
|
fi
|
|
|
|
echo "eligible_event=$eligible_event" >> "$GITHUB_OUTPUT"
|
|
echo "secrets_ok=$secrets_ok" >> "$GITHUB_OUTPUT"
|
|
echo "db_secret_ok=$db_secret_ok" >> "$GITHUB_OUTPUT"
|
|
|
|
deploy:
|
|
name: Deploy Cloud Run
|
|
runs-on: ubuntu-latest
|
|
needs: check-secrets
|
|
timeout-minutes: 30
|
|
if: ${{ needs.check-secrets.outputs.eligible_event == 'true' && needs.check-secrets.outputs.secrets_ok == 'true' && needs.check-secrets.outputs.db_secret_ok == 'true' }}
|
|
environment: ${{ github.ref == 'refs/heads/main' && 'Production' || 'Development' }}
|
|
env:
|
|
GCP_REGION: ${{ vars.GCP_REGION || 'europe-west1' }}
|
|
ARTIFACT_REPOSITORY: ${{ vars.ARTIFACT_REPOSITORY || 'household-bot' }}
|
|
# Dynamic Service Names based on environment
|
|
# Branch 'main' -> Environment 'prod' -> household-prod-*
|
|
# Branch 'dev' -> Environment 'dev' -> household-dev-*
|
|
CLOUD_RUN_SERVICE_BOT: ${{ github.ref == 'refs/heads/main' && 'household-prod-bot-api' || 'household-dev-bot-api' }}
|
|
CLOUD_RUN_SERVICE_MINI: ${{ github.ref == 'refs/heads/main' && 'household-prod-mini-app' || 'household-dev-mini-app' }}
|
|
TELEGRAM_BOT_TOKEN_SECRET_ID: ${{ github.ref == 'refs/heads/main' && 'telegram-bot-token' || 'telegram-bot-token-test' }}
|
|
|
|
steps:
|
|
- name: Checkout deployment ref
|
|
uses: actions/checkout@v4
|
|
with:
|
|
ref: ${{ github.event_name == 'workflow_run' && github.event.workflow_run.head_sha || github.sha }}
|
|
|
|
- name: Setup Bun
|
|
uses: oven-sh/setup-bun@v2
|
|
with:
|
|
bun-version-file: .bun-version
|
|
|
|
- name: Install dependencies
|
|
run: bun install --frozen-lockfile
|
|
|
|
- name: Authenticate to Google Cloud
|
|
uses: google-github-actions/auth@v2
|
|
with:
|
|
workload_identity_provider: ${{ secrets.GCP_WORKLOAD_IDENTITY_PROVIDER }}
|
|
service_account: ${{ secrets.GCP_SERVICE_ACCOUNT }}
|
|
|
|
- name: Run database migrations
|
|
env:
|
|
DATABASE_URL: ${{ secrets.DATABASE_URL }}
|
|
DB_SCHEMA: ${{ github.ref == 'refs/heads/main' && 'public' || 'test' }}
|
|
run: bun run db:migrate
|
|
|
|
- name: Setup gcloud
|
|
uses: google-github-actions/setup-gcloud@v2
|
|
|
|
- name: Load Telegram bot token for command sync
|
|
id: telegram-token
|
|
run: |
|
|
set +e
|
|
token="$(gcloud secrets versions access latest \
|
|
--secret "${TELEGRAM_BOT_TOKEN_SECRET_ID}" \
|
|
--project "${{ secrets.GCP_PROJECT_ID }}" 2>/dev/null)"
|
|
status=$?
|
|
set -e
|
|
|
|
if [[ $status -ne 0 || -z "$token" ]]; then
|
|
echo "available=false" >> "$GITHUB_OUTPUT"
|
|
exit 0
|
|
fi
|
|
|
|
echo "::add-mask::$token"
|
|
{
|
|
echo "available=true"
|
|
echo "token<<EOF"
|
|
echo "$token"
|
|
echo "EOF"
|
|
} >> "$GITHUB_OUTPUT"
|
|
|
|
- name: Configure Artifact Registry auth
|
|
run: |
|
|
gcloud auth configure-docker "${GCP_REGION}-docker.pkg.dev" --quiet
|
|
|
|
- name: Resolve image tags
|
|
id: images
|
|
run: |
|
|
bot_image="${GCP_REGION}-docker.pkg.dev/${{ secrets.GCP_PROJECT_ID }}/${ARTIFACT_REPOSITORY}/bot:${GITHUB_SHA}"
|
|
mini_image="${GCP_REGION}-docker.pkg.dev/${{ secrets.GCP_PROJECT_ID }}/${ARTIFACT_REPOSITORY}/miniapp:${GITHUB_SHA}"
|
|
|
|
echo "bot_image=$bot_image" >> "$GITHUB_OUTPUT"
|
|
echo "mini_image=$mini_image" >> "$GITHUB_OUTPUT"
|
|
|
|
- name: Build and push bot image
|
|
run: |
|
|
docker build -f apps/bot/Dockerfile -t "${{ steps.images.outputs.bot_image }}" .
|
|
docker push "${{ steps.images.outputs.bot_image }}"
|
|
|
|
- name: Build and push mini app image
|
|
run: |
|
|
docker build -f apps/miniapp/Dockerfile -t "${{ steps.images.outputs.mini_image }}" .
|
|
docker push "${{ steps.images.outputs.mini_image }}"
|
|
|
|
- name: Deploy bot service
|
|
run: |
|
|
gcloud run deploy "${CLOUD_RUN_SERVICE_BOT}" \
|
|
--image "${{ steps.images.outputs.bot_image }}" \
|
|
--region "${GCP_REGION}" \
|
|
--project "${{ secrets.GCP_PROJECT_ID }}" \
|
|
--allow-unauthenticated \
|
|
--quiet
|
|
|
|
- name: Deploy mini app service
|
|
run: |
|
|
gcloud run deploy "${CLOUD_RUN_SERVICE_MINI}" \
|
|
--image "${{ steps.images.outputs.mini_image }}" \
|
|
--region "${GCP_REGION}" \
|
|
--project "${{ secrets.GCP_PROJECT_ID }}" \
|
|
--allow-unauthenticated \
|
|
--quiet
|
|
|
|
- name: Sync Telegram commands
|
|
if: ${{ steps.telegram-token.outputs.available == 'true' }}
|
|
env:
|
|
TELEGRAM_BOT_TOKEN: ${{ steps.telegram-token.outputs.token }}
|
|
run: bun run ops:telegram:commands set
|
|
|
|
- name: Set Telegram Webhook
|
|
if: ${{ steps.telegram-token.outputs.available == 'true' }}
|
|
env:
|
|
TELEGRAM_BOT_TOKEN: ${{ steps.telegram-token.outputs.token }}
|
|
run: |
|
|
SERVICE_URL=$(gcloud run services describe "${CLOUD_RUN_SERVICE_BOT}" \
|
|
--region "${GCP_REGION}" \
|
|
--project "${{ secrets.GCP_PROJECT_ID }}" \
|
|
--format 'value(status.url)')
|
|
|
|
export TELEGRAM_WEBHOOK_URL="$SERVICE_URL/webhook/telegram"
|
|
bun run ops:telegram:webhook set
|