Skip to content

Update main.yml

Update main.yml #10

Workflow file for this run

name: Build, Push, and Deploy Docker Image
on:
push:
branches:
- main # main 브랜치에 푸시될 때 트리거
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
# 1. 코드 체크아웃
- name: Checkout repository
uses: actions/checkout@v4
# 2. node_modules 캐시 설정 (필요할 경우)
- name: Cache node_modules
uses: actions/[email protected]
with:
path: ~/.npm # If using npm
# For yarn, use path: ~/.cache/yarn
key: ${{ runner.os }}-node-modules-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-modules-
# 3. Docker Hub에 로그인
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
# 4. Docker 이미지 빌드 및 푸시
- name: Build Docker image
run: |
docker build \
--build-arg NEXT_PUBLIC_SPRING_API_URL=${{ secrets.NEXT_PUBLIC_SPRING_API_URL }} \
--build-arg NEXT_PUBLIC_FAST_API_URL=${{ secrets.NEXT_PUBLIC_FAST_API_URL }} \
-t ${{ secrets.DOCKER_USERNAME }}/react:v1 .
- name: Push Docker image
run: |
docker push "${{ secrets.DOCKER_USERNAME }}/react:v1"
# 5. EC2 서버로 배포 (SSH 연결)
- name: Deploy to EC2
uses: appleboy/[email protected]
with:
host: ${{ secrets.EC2_HOST }}
username: ec2-user # EC2 사용자 이름
key: ${{ secrets.EC2_SSH_KEY }}
script: |
# 모든 컨테이너 및 이미지 삭제 후 새로운 컨테이너 실행
docker rm -f $(docker ps -a -q) || true # 모든 컨테이너 강제 삭제
docker rmi -f $(docker images -q) || true # 모든 이미지 강제 삭제
docker run -dp 3000:3000 ${{ secrets.DOCKER_USERNAME }}/react:v1