Merge pull request #18 from Goldchae/main #17
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 }} | |
# Docker 이미지 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 system prune -af --volumes | |
# 기존 컨테이너 중지 및 제거 | |
docker stop qup-frontend || true | |
docker rm qup-frontend || true | |
# 새 컨테이너 실행 | |
docker run -d --name qup-frontend -p 80:3000 ${{ env.DOCKER_IMAGE }}:latest |