diff --git a/.github/workflows/deploy-environment.yml b/.github/workflows/deploy-environment.yml index 3c26a26933..708fd762d0 100644 --- a/.github/workflows/deploy-environment.yml +++ b/.github/workflows/deploy-environment.yml @@ -13,10 +13,19 @@ on: description: "Docker image digest to deploy" required: true type: string - render-service-ids: - description: "Comma-separated list of Render service IDs to deploy to" + render-api-service-id: + description: "Render API service ID" required: true type: string + render-worker-service-ids: + description: "Comma-separated list of Render worker service IDs" + required: true + type: string + has-migrations: + description: "Whether this deployment includes database migrations" + required: false + type: boolean + default: false skip-backend: description: "Skip backend deployment if no backend changes" required: false @@ -88,8 +97,11 @@ jobs: - name: Deploy Backend to Render run: | - IFS=',' read -ra SERVICE_IDS <<< "${{ inputs.render-service-ids }}" - ./.github/workflows/deploy_server.sh ${{ inputs.docker-digest }} "${SERVICE_IDS[@]}" + ./.github/workflows/deploy_server.sh \ + ${{ inputs.docker-digest }} \ + ${{ inputs.has-migrations }} \ + "${{ inputs.render-api-service-id }}" \ + "${{ inputs.render-worker-service-ids }}" env: RENDER_API_TOKEN: ${{ secrets.RENDER_API_TOKEN }} diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 7649ab2420..9e611388c0 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -19,6 +19,7 @@ jobs: outputs: backend: ${{ steps.filter.outputs.backend }} frontend: ${{ steps.filter.outputs.frontend }} + migrations: ${{ steps.filter.outputs.migrations }} steps: - uses: actions/checkout@v6 - uses: dorny/paths-filter@v3 @@ -34,6 +35,8 @@ jobs: - 'clients/**' - '.github/workflows/deploy.yml' - '.github/workflows/deploy-environment.yml' + migrations: + - 'server/migrations/**' build: name: "Build Docker Image 🐳" @@ -103,7 +106,9 @@ jobs: with: environment: sandbox docker-digest: ${{ needs.build.outputs.digest }} - render-service-ids: "srv-crkocgbtq21c73ddsdbg,srv-d089jj7diees73934kgg" + render-api-service-id: "srv-crkocgbtq21c73ddsdbg" + render-worker-service-ids: "srv-d089jj7diees73934kgg" + has-migrations: ${{ needs.changes.outputs.migrations == 'true' || github.event_name == 'workflow_dispatch' }} skip-backend: ${{ needs.changes.outputs.backend != 'true' && github.event_name != 'workflow_dispatch' }} skip-frontend: ${{ needs.changes.outputs.frontend != 'true' && github.event_name != 'workflow_dispatch' }} secrets: @@ -122,7 +127,9 @@ jobs: with: environment: production docker-digest: ${{ needs.build.outputs.digest }} - render-service-ids: "srv-ci4r87h8g3ne0dmvvl60,srv-d4k6otfgi27c73cicnpg,srv-d4k62svpm1nc73af5e3g,srv-d3hrh1j3fgac73a1t4r0" + render-api-service-id: "srv-ci4r87h8g3ne0dmvvl60" + render-worker-service-ids: "srv-d4k6otfgi27c73cicnpg,srv-d4k62svpm1nc73af5e3g,srv-d3hrh1j3fgac73a1t4r0" + has-migrations: ${{ needs.changes.outputs.migrations == 'true' || github.event_name == 'workflow_dispatch' }} skip-backend: ${{ needs.changes.outputs.backend != 'true' && github.event_name != 'workflow_dispatch' }} skip-frontend: ${{ needs.changes.outputs.frontend != 'true' && github.event_name != 'workflow_dispatch' }} secrets: diff --git a/.github/workflows/deploy_server.sh b/.github/workflows/deploy_server.sh index 5eea2da1e8..3bb419c91f 100755 --- a/.github/workflows/deploy_server.sh +++ b/.github/workflows/deploy_server.sh @@ -2,18 +2,20 @@ set -euo pipefail -# Usage: ./deploy_server.sh [service_id_2] [service_id_n] -if [ $# -lt 2 ]; then - echo "Usage: $0 [service_id_2] [service_id_n]" - echo "Example: $0 sha256:abc123... srv-123 srv-456" +# Usage: ./deploy_server.sh +if [ $# -lt 4 ]; then + echo "Usage: $0 " + echo "Example: $0 sha256:abc123... true srv-123 srv-456,srv-789" exit 1 fi IMG="ghcr.io/polarsource/polar@${1}" -shift # Remove the first argument, leaving only service IDs +HAS_MIGRATIONS="${2}" +API_SERVICE_ID="${3}" +WORKER_SERVICE_IDS="${4}" -# Read service IDs from remaining arguments -declare -a servers=("$@") +# Convert worker service IDs from comma-separated string to array +IFS=',' read -ra WORKER_SERVERS <<< "$WORKER_SERVICE_IDS" # Configuration TIMEOUT=300 # 5 minutes timeout @@ -37,9 +39,10 @@ check_deployment_status() { # Function to deploy a set of servers deploy_servers() { local -n servers_ref=$1 - local environment=${2:-"unknown"} + local services=${2:-"unknown"} + local environment=${3:-"unknown"} - echo "🚀 Starting deployment to ${environment}..." + echo "🚀 Starting deployment of ${services} to ${environment}..." # Trigger deployments local -A deploy_map=() # Maps service_id to deploy_id @@ -66,7 +69,7 @@ deploy_servers() { done # Wait for deployments to complete - echo "⏳ Waiting for ${environment} deployments to complete..." + echo "⏳ Waiting for ${services} in ${environment} deployments to complete..." local start_time=$(date +%s) local all_complete=false @@ -116,10 +119,27 @@ deploy_servers() { fi done - echo "✅ All deployments completed successfully!" + echo "✅ ${services} deployments completed successfully!" } -# Deploy the provided servers -deploy_servers servers "environment" +# Deploy based on migration status +if [ "$HAS_MIGRATIONS" = "true" ]; then + echo "📋 Deploying sequentially (migrations detected)" + + # Deploy API first (runs migrations via preDeployCommand) + api_server=("$API_SERVICE_ID") + deploy_servers api_server "API" "environment" + + # Deploy workers after API completes + if [ ${#WORKER_SERVERS[@]} -gt 0 ]; then + deploy_servers WORKER_SERVERS "workers" "environment" + fi +else + echo "📋 Deploying all services in parallel" + + # Deploy all services in parallel + all_servers=("$API_SERVICE_ID" "${WORKER_SERVERS[@]}") + deploy_servers all_servers "All" "environment" +fi echo "🎉 Deployment completed successfully!"