-
Notifications
You must be signed in to change notification settings - Fork 10
Deployment: Staging deploy workflow for EC2 via SSM #770
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
3835163
feat(*): backend staging deployment for ec2
Ayush8923 bdc1b1c
fix(*): remove the github shagst
Ayush8923 d302669
fix(*): change the vars to secrets and upate the file name
Ayush8923 31da975
Merge branch 'main' into feat/staging-deployment-ec2
Ayush8923 585ee53
fix(*): added the migration in CD
Ayush8923 4a0f74d
fix(*): update the naming convention of enviornment
Ayush8923 f5fb119
fix(*): created docker compose and update deployment guide
Ayush8923 38a18e9
fix(*): update the script as per coderabbit suggestion
Ayush8923 d3956c6
fix(*): update the command
Ayush8923 e84e153
fix(*): test workflow
Ayush8923 385ac45
fix(*): test workflow
Ayush8923 af72d96
fix(*): test workflow
Ayush8923 3ce13df
fix(*): add the dedicated service of migration
Ayush8923 f7dbacf
fix(*): change the base branch
Ayush8923 c3ab229
fix(*): update the push brnach name
Ayush8923 cf1d3a6
fix(*): update the postgres server
Ayush8923 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| name: Deploy Kaapi staging to EC2 | ||
|
|
||
| on: | ||
| push: | ||
| branches: [main] | ||
| workflow_dispatch: | ||
|
Ayush8923 marked this conversation as resolved.
|
||
|
|
||
| concurrency: | ||
| group: staging-ec2-deploy | ||
| cancel-in-progress: false | ||
|
|
||
| jobs: | ||
| deploy: | ||
| runs-on: ubuntu-latest | ||
| environment: AWS_STAGING_ENV | ||
|
|
||
| permissions: | ||
| id-token: write | ||
| contents: read | ||
|
|
||
| steps: | ||
| - name: Configure AWS credentials | ||
| uses: aws-actions/configure-aws-credentials@v6 | ||
| with: | ||
| role-to-assume: ${{ secrets.STAGING_EC2_DEPLOY_ROLE_ARN }} | ||
| aws-region: ap-south-1 | ||
|
|
||
| - name: Trigger deploy on EC2 via SSM | ||
| id: ssm | ||
| env: | ||
| INSTANCE_ID: ${{ secrets.STAGING_EC2_INSTANCE_ID }} | ||
| run: | | ||
| CMD_ID=$(aws ssm send-command \ | ||
| --instance-ids "$INSTANCE_ID" \ | ||
| --document-name "AWS-RunShellScript" \ | ||
| --comment "Deploy kaapi-backend kaapi-staging" \ | ||
| --parameters commands='["set -eux","chown -R ubuntu:ubuntu /data/kaapi-backend","sudo -iu ubuntu bash -lc \"cd /data/kaapi-backend && git fetch --all && git pull origin main && docker compose -f docker-compose.staging.yml build && docker compose -f docker-compose.staging.yml --profile migrate run --rm migrate && docker compose -f docker-compose.staging.yml up -d --remove-orphans && docker image prune -f\""]' \ | ||
| --cloud-watch-output-config CloudWatchOutputEnabled=true \ | ||
| --query "Command.CommandId" --output text) | ||
| echo "cmd_id=$CMD_ID" >> "$GITHUB_OUTPUT" | ||
| echo "Sent SSM command: $CMD_ID" | ||
|
|
||
| - name: Wait for SSM command to finish | ||
| env: | ||
| INSTANCE_ID: ${{ secrets.STAGING_EC2_INSTANCE_ID }} | ||
| CMD_ID: ${{ steps.ssm.outputs.cmd_id }} | ||
| run: | | ||
| WAIT_EXIT=0 | ||
| aws ssm wait command-executed \ | ||
| --command-id "$CMD_ID" \ | ||
| --instance-id "$INSTANCE_ID" || WAIT_EXIT=$? | ||
|
|
||
| aws ssm get-command-invocation \ | ||
| --command-id "$CMD_ID" \ | ||
| --instance-id "$INSTANCE_ID" \ | ||
| --query '{Status:Status,Stdout:StandardOutputContent,Stderr:StandardErrorContent}' \ | ||
| --output json | ||
|
|
||
| exit $WAIT_EXIT | ||
|
Ayush8923 marked this conversation as resolved.
|
||
File renamed without changes.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| version: "3.9" | ||
|
|
||
| services: | ||
| backend: | ||
| image: "${DOCKER_IMAGE_BACKEND?Variable not set}:${TAG:-latest}" | ||
| container_name: backend | ||
| restart: always | ||
| build: | ||
| context: ./backend | ||
| env_file: | ||
| - .env | ||
| ports: | ||
| - "8000:80" | ||
| healthcheck: | ||
| test: ["CMD", "curl", "-f", "http://localhost:80/api/v1/utils/health/"] | ||
| interval: 10s | ||
| timeout: 5s | ||
| retries: 5 | ||
| command: > | ||
| uv run uvicorn app.main:app --host 0.0.0.0 --port 80 --reload | ||
| develop: | ||
| watch: | ||
| # Sync backend source code into container immediately on change | ||
| - action: sync | ||
| path: ./backend/app | ||
| target: /app/app | ||
| # Rebuild image if dependencies change | ||
| - action: rebuild | ||
| path: ./backend/pyproject.toml | ||
| - action: rebuild | ||
| path: ./backend/uv.lock | ||
| logging: | ||
| driver: awslogs | ||
| options: | ||
| awslogs-region: ap-south-1 | ||
| awslogs-group: /ec2/kaapi-staging | ||
| awslogs-stream: backend | ||
| awslogs-create-group: "true" | ||
|
|
||
| migrate: | ||
| image: "${DOCKER_IMAGE_BACKEND?Variable not set}:${TAG:-latest}" | ||
| build: | ||
| context: ./backend | ||
| env_file: | ||
| - .env | ||
| profiles: ["migrate"] | ||
| command: ["uv", "run", "alembic", "upgrade", "head"] | ||
|
|
||
| celery_worker: | ||
| image: "${DOCKER_IMAGE_BACKEND?Variable not set}:${TAG:-latest}" | ||
| container_name: celery-worker | ||
| restart: always | ||
| build: | ||
| context: ./backend | ||
| depends_on: | ||
| backend: | ||
| condition: service_healthy | ||
| env_file: | ||
| - .env | ||
| command: ["uv", "run", "celery", "-A", "app.celery.celery_app", "worker", "--loglevel=info"] | ||
| logging: | ||
| driver: awslogs | ||
| options: | ||
| awslogs-region: ap-south-1 | ||
| awslogs-group: /ec2/kaapi-staging | ||
| awslogs-stream: celery-worker | ||
| awslogs-create-group: "true" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.