Update deploy.yml #20
This file contains 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 to EC2 | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
steps: | |
# Step 1: Check out the latest code from GitHub | |
- name: Checkout the code | |
uses: actions/checkout@v3 | |
# Step 2: Set up Docker Buildx | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v2 | |
# Step 3: Log in to Docker Hub | |
- name: Log in to Docker Hub | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ secrets.DOCKER_HUB_USERNAME }} | |
password: ${{ secrets.DOCKER_HUB_PASSWORD }} | |
# Step 4: Build the Docker images | |
- name: Build backend Docker image | |
run: | | |
docker build -t mohammadshahidbeigh/mindvault-backend:latest ./mindvault-backend | |
- name: Build frontend Docker image | |
run: | | |
docker build -t mohammadshahidbeigh/mindvault-frontend:latest ./src | |
# Step 5: Push the Docker images to Docker Hub | |
- name: Push backend image to Docker Hub | |
run: | | |
docker push mohammadshahidbeigh/mindvault-backend:latest | |
- name: Push frontend image to Docker Hub | |
run: | | |
docker push mohammadshahidbeigh/mindvault-frontend:latest | |
# Step 6: SSH into EC2 and deploy the containers | |
- name: SSH into EC2 and start containers | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.EC2_PUBLIC_IP }} | |
username: ubuntu | |
key: ${{ secrets.EC2_SSH_KEY }} | |
script: | | |
# Stop existing containers | |
docker stop $(docker ps -q) || true | |
docker rm $(docker ps -aq) || true | |
# Pull the latest Docker images | |
docker pull mohammadshahidbeigh/mindvault-backend:latest | |
docker pull mohammadshahidbeigh/mindvault-frontend:latest | |
# Run backend container | |
docker run -d -p 4000:4000 --name backend-app mohammadshahidbeigh/mindvault-backend:latest | |
# Run frontend container | |
docker run -d -p 80:80 --name frontend-app mohammadshahidbeigh/mindvault-frontend:latest | |