Skip to content

chore: deploy to aws ec2 #18

chore: deploy to aws ec2

chore: deploy to aws ec2 #18

Workflow file for this run

name: Build and Deploy to aws ECS
on:
push:
branches: [ "main" ]
pull_request:
types: ["closed"]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout Code Base
uses: actions/checkout@v4
- name: Log files
run: ls -al
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build and Push Docker image to Docker Hub Registry.
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_REPOSITORY_NAME }}:latest
build-args: |
NEXT_PUBLIC_API_URL=${{ secrets.NEXT_PUBLIC_API_URL }}
NEXT_PUBLIC_BUILD_API_URL=${{ secrets.NEXT_PUBLIC_BUILD_API_URL }}
- name: Deploy to EC2
uses: appleboy/ssh-action@v0.1.9
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USER }}
key: ${{ secrets.EC2_PRIVATE_KEY }}
script: |
# Pull the latest Docker image from Docker Hub
docker pull ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_REPOSITORY_NAME }}:latest
# Stop and remove the existing container (if any)
docker stop frontend || true
docker rm frontend || true
# Run the new container
docker run -d --name frontend -p 3000:3000 ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_REPOSITORY_NAME }}:latest