Make campaign queue API idempotent #39
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Deploy Sandbox | |
| on: | |
| workflow_dispatch: | |
| push: | |
| branches: [main] | |
| paths-ignore: | |
| - "docs/**" | |
| - "*.md" | |
| concurrency: | |
| group: datamailer-sandbox | |
| cancel-in-progress: false | |
| permissions: | |
| contents: read | |
| id-token: write | |
| env: | |
| AWS_REGION: us-east-1 | |
| SANDBOX_INSTANCE_ID: ${{ vars.DATAMAILER_SANDBOX_INSTANCE_ID || 'i-044716a7d6b053eef' }} | |
| SANDBOX_HEALTH_URL: ${{ vars.DATAMAILER_SANDBOX_HEALTH_URL || 'https://datamailer.dtcdev.click/health/' }} | |
| SANDBOX_DEPLOY_ROLE_ARN: ${{ vars.DATAMAILER_SANDBOX_DEPLOY_ROLE_ARN || 'arn:aws:iam::817685572750:role/datamailer-sandbox-github-deploy' }} | |
| jobs: | |
| test: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v5 | |
| - uses: astral-sh/setup-uv@v7 | |
| - name: Install dependencies | |
| run: uv sync --dev | |
| - name: Run lint | |
| run: uv run ruff check . | |
| - name: Run tests | |
| run: uv run pytest | |
| deploy: | |
| runs-on: ubuntu-latest | |
| needs: test | |
| if: github.ref == 'refs/heads/main' | |
| steps: | |
| - name: Configure AWS credentials | |
| uses: aws-actions/configure-aws-credentials@v5 | |
| with: | |
| role-to-assume: ${{ env.SANDBOX_DEPLOY_ROLE_ARN }} | |
| aws-region: ${{ env.AWS_REGION }} | |
| - name: Deploy sandbox host | |
| id: deploy | |
| run: | | |
| set -euo pipefail | |
| COMMAND_ID=$( | |
| aws ssm send-command \ | |
| --region "$AWS_REGION" \ | |
| --instance-ids "$SANDBOX_INSTANCE_ID" \ | |
| --document-name AWS-RunShellScript \ | |
| --comment "Deploy Datamailer sandbox ${GITHUB_SHA}" \ | |
| --parameters commands="[ | |
| \"set -eux\", | |
| \"sudo -u ubuntu git -C /opt/datamailer fetch origin main\", | |
| \"sudo -u ubuntu git -C /opt/datamailer checkout --force ${GITHUB_SHA}\", | |
| \"sudo -u ubuntu /usr/local/bin/uv --directory /opt/datamailer sync --frozen\", | |
| \"sudo -u ubuntu /usr/local/bin/uv --directory /opt/datamailer pip install gunicorn\", | |
| \"sudo -u ubuntu /usr/local/bin/uv --directory /opt/datamailer run python manage.py collectstatic --noinput\", | |
| \"sudo -u ubuntu /usr/local/bin/uv --directory /opt/datamailer run python manage.py migrate --noinput\", | |
| \"sudo -u ubuntu /usr/local/bin/uv --directory /opt/datamailer run python manage.py provision_client_scope --organization datatalksclub --organization-name DataTalksClub --audience dtc-courses --audience-name 'DataTalksClub Courses' --client dtc-courses --client-name 'DTC Courses'\", | |
| \"sudo -u ubuntu /usr/local/bin/uv --directory /opt/datamailer run python manage.py set_client_senders dtc-courses --organization datatalksclub --default-sender courses --sender courses=courses@dtcdev.click --sender no-reply=no-reply@dtcdev.click\", | |
| \"systemctl restart datamailer\", | |
| \"bash /opt/datamailer/scripts/install_sandbox_worker_service.sh\", | |
| \"systemctl is-active datamailer\", | |
| \"systemctl is-active datamailer-transactional-worker\", | |
| \"systemctl is-active datamailer-ses-webhooks-worker\", | |
| \"systemctl is-active datamailer-cmp-callbacks-worker\", | |
| \"sudo -u ubuntu git -C /opt/datamailer rev-parse --short HEAD\" | |
| ]" \ | |
| --query 'Command.CommandId' \ | |
| --output text | |
| ) | |
| echo "command_id=$COMMAND_ID" >> "$GITHUB_OUTPUT" | |
| aws ssm wait command-executed \ | |
| --region "$AWS_REGION" \ | |
| --command-id "$COMMAND_ID" \ | |
| --instance-id "$SANDBOX_INSTANCE_ID" | |
| - name: Show deployment output | |
| if: always() | |
| run: | | |
| aws ssm get-command-invocation \ | |
| --region "$AWS_REGION" \ | |
| --command-id "${{ steps.deploy.outputs.command_id }}" \ | |
| --instance-id "$SANDBOX_INSTANCE_ID" \ | |
| --query '{Status:Status,ResponseCode:ResponseCode,Stdout:StandardOutputContent,Stderr:StandardErrorContent}' | |
| - name: Verify sandbox health | |
| run: | | |
| curl --fail --silent --show-error --retry 10 --retry-delay 3 "$SANDBOX_HEALTH_URL" |