Skip to content

Update README.md

Update README.md #1

Workflow file for this run

name: Deploy to AWS EC2
on:
push:
branches:
- master
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build and push Docker image
uses: docker/build-push-action@v4
with:
context: .
push: true
tags: ${{ secrets.DOCKER_USERNAME }}/my-react-app:latest
- name: Set up SSH
run: |
mkdir -p ~/.ssh
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
echo -e "StrictHostKeyChecking no\n" >> ~/.ssh/config
- name: Deploy to EC2
env:
HOST: ${{ secrets.AWS_EC2_HOST }}
USER: ${{ secrets.AWS_EC2_USER }}
KEY: ${{ secrets.AWS_EC2_KEY }}
IMAGE: ${{ secrets.DOCKER_USERNAME }}/my-react-app:latest
run: |
ssh -o StrictHostKeyChecking=no ${{ secrets.AWS_EC2_USER }}@${{ secrets.AWS_EC2_HOST }} << EOF
# Pull the latest image
sudo docker pull $IMAGE
# Stop the running container
sudo docker stop my-react-app || true
# Remove the stopped container
sudo docker rm my-react-app || true
# Remove old images
sudo docker image prune -f
# Run a new container with the latest image
sudo docker run -d -p 80:3000 --name my-react-app $IMAGE
EOF