Skip to content

Merge pull request #254 from urinaner/fix/253 #102

Merge pull request #254 from urinaner/fix/253

Merge pull request #254 from urinaner/fix/253 #102

Workflow file for this run

name: Backend CI/CD using GitHub Actions & Docker
# 워크플로우 실행 조건 설정
on:
push:
branches: [ "main" ]
paths:
- 'backend/**'
pull_request:
branches: [ "main" ]
paths:
- 'backend/**'
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
steps:
# 코드 체크아웃
- uses: actions/checkout@v4
# 백엔드 빌드 환경 설정 (JDK 21)
- name: Set up JDK 21
uses: actions/setup-java@v4
with:
java-version: '21'
distribution: 'temurin'
# 백엔드 설정 파일 생성
- name: Create backend configuration files
run: |
mkdir -p ./backend/src/main/resources
cd ./backend/src/main/resources
echo "${{ secrets.APPLICATION_YML }}" > application.yml
echo "${{ secrets.APPLICATION_AWS_YML }}" > application-aws.yml
# Gradle 설정
- name: Setup Gradle
uses: gradle/actions/setup-gradle@417ae3ccd767c252f5661f1ace9f835f9654f2b5
# 백엔드 빌드 실행
- name: Build Backend
run: |
cd backend
chmod +x ./gradlew
./gradlew build -x test
# 도커 이미지 빌드 및 푸시
- name: Docker build & push
run: |
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
cd backend
docker build -t ${{ secrets.DOCKER_USERNAME }}/backend:latest .
docker push ${{ secrets.DOCKER_USERNAME }}/backend:latest
# EC2 인스턴스에 배포
- name: Deploy to EC2
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.EC2_HOST }}
username: ubuntu
key: ${{ secrets.EC2_PRIVATE_KEY }}
script: |
# 최신 도커 이미지 받아오기
sudo docker pull ${{ secrets.DOCKER_USERNAME }}/backend:latest
# 실행 중인 컨테이너 중지 및 제거
sudo docker stop $(sudo docker ps -a -q) || true
sudo docker rm $(sudo docker ps -a -q) || true
# 새 컨테이너 실행
sudo docker run -d -p 8080:8080 ${{ secrets.DOCKER_USERNAME }}/backend:latest
# 사용하지 않는 이미지 정리
sudo docker image prune -a -f