Skip to content

Merge pull request #185 from checkmo2025/refactor/184/refactorByFront #149

Merge pull request #185 from checkmo2025/refactor/184/refactorByFront

Merge pull request #185 from checkmo2025/refactor/184/refactorByFront #149

Workflow file for this run

name: Deploy to EC2 with CodeDeploy
on:
push:
branches:
- develop
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
# 1. 코드 체크아웃
- name: Checkout code
uses: actions/checkout@v4
# 2. AWS 자격 증명 설정
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ap-northeast-2
# 3. ECR 로그인
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
# 4. Docker 이미지 빌드 및 ECR 푸시 (여기서 빌드가 일어납니다)
- name: Build and push Docker image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: checkmo
IMAGE_TAG: latest
run: |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
- name: Create .env file
run: |
echo "${{ secrets.ENV_FILE }}" > .env
# 5. 설정 파일 전송 (SCP)
- name: Copy files to EC2
uses: appleboy/scp-action@master
with:
host: ${{ secrets.EC2_HOST }}
username: ubuntu
key: ${{ secrets.EC2_SSH_KEY }}
source: "compose.yml,nginx/,.env"
target: "/home/ubuntu/app"
# 6. EC2 접속 및 배포 실행 (SSH)
- name: Deploy to EC2
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.EC2_HOST }}
username: ubuntu
key: ${{ secrets.EC2_SSH_KEY }}
script: |
cd /home/ubuntu/app
# ECR 로그인
aws ecr get-login-password --region ap-northeast-2 | docker login --username AWS --password-stdin ${{ steps.login-ecr.outputs.registry }}
# 최신 이미지 Pull
docker compose pull
# 컨테이너 재실행
docker compose up -d
# nginx DNS 캐시 초기화
docker compose restart nginx
# 미사용 이미지 정리
docker image prune -f