Merge pull request #15 from Goldchae/main #14
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 AWS EC2 using Docker | |
on: | |
push: | |
branches: | |
- main | |
env: | |
DOCKER_IMAGE: celina324/qup-frontend | |
jobs: | |
build-and-push-docker: | |
runs-on: ubuntu-latest | |
steps: | |
- 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@v3 | |
with: | |
context: . | |
file: ./Dockerfile | |
push: true | |
tags: ${{ env.DOCKER_IMAGE }}:latest, ${{ env.DOCKER_IMAGE }}:${{ github.sha }} | |
# 도커 이미지 EC2 인스턴스에 배포 | |
deploy_to_ec2: | |
needs: build-and-push-docker | |
runs-on: ubuntu-latest | |
steps: | |
- name: Deploy to EC2 | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.EC2_HOST }} # EC2 IP 주소 | |
username: ${{ secrets.EC2_USER }} # EC2 사용자 | |
key: ${{ secrets.EC2_SSH_PRIVATE_KEY }} # pem 키 | |
# 기존 컨테이너 중지 | |
script: | | |
if ! docker pull ${{ env.DOCKER_IMAGE }}:latest; then | |
echo "Failed to pull the latest image." | |
exit 1 | |
fi | |
docker stop qup-frontend || true | |
docker rm qup-frontend || true | |
docker run -d --name qup-frontend -p 80:3000 ${{ env.DOCKER_IMAGE }}:latest |