-
Notifications
You must be signed in to change notification settings - Fork 1
59 lines (50 loc) · 1.69 KB
/
deploy.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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