Skip to content

Merge pull request #29 from jothamteshome/feat/abstract-youtube-logic #52

Merge pull request #29 from jothamteshome/feat/abstract-youtube-logic

Merge pull request #29 from jothamteshome/feat/abstract-youtube-logic #52

name: Deploy watch-together backend to EC2 via AWS SSM
on:
push:
branches:
- main
paths:
- 'backend/**'
- 'shared/**'
- '.github/workflows/deploy-backend.yml'
permissions:
id-token: write
contents: read
packages: write
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v6
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v6
with:
role-to-assume: ${{ secrets.AWS_PERPETUAL_APP_HOST_ROLE_TO_ASSUME }}
aws-region: us-east-1
- name: Update SSM Parameters
run: |
aws ssm put-parameter --name "/watch-together/backend/port" --value "${{ secrets.BACKEND_PORT }}" --type "String" --overwrite
aws ssm put-parameter --name "/watch-together/backend/youtube/api/key" --value "${{ secrets.YOUTUBE_API_KEY }}" --type "SecureString" --overwrite
- name: Log in to GHCR
uses: docker/login-action@v4
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push image
uses: docker/build-push-action@v7
with:
context: .
file: backend/Dockerfile
push: true
tags: ghcr.io/jothamteshome/watch-together-backend:latest
- name: Deploy on EC2
run: |
command_id=$(aws ssm send-command \
--instance-ids ${{ secrets.EC2_INSTANCE_ID }} \
--document-name "AWS-RunShellScript" \
--parameters '{"commands":[
"git config --system --add safe.directory /opt/infra-hub",
"git -C /opt/infra-hub fetch origin main",
"git -C /opt/infra-hub reset --hard origin/main",
"bash /opt/infra-hub/compose/perpetual-app-host/scripts/setup-instance.sh",
"bash -c \". /etc/profile.d/init-env.sh && cd /opt/infra-hub/compose/perpetual-app-host && docker compose pull watch-together && docker compose up -d --no-deps watch-together && docker image prune -f\""
]}' \
--comment "Deploy watch-together backend" \
--query "Command.CommandId" \
--output text)
aws ssm wait command-executed \
--command-id "$command_id" \
--instance-id ${{ secrets.EC2_INSTANCE_ID }}
aws ssm get-command-invocation \
--command-id "$command_id" \
--instance-id ${{ secrets.EC2_INSTANCE_ID }} \
--query "StandardOutputContent" \
--output text