Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] #5 CICD-파이프라인 구현 #6

Merged
merged 12 commits into from
Sep 26, 2024
74 changes: 74 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
name: X-BE CICD

on:
push:
branches:
- main
pull_request:
branches:
- main # main 브랜치로의 PR 시에도 실행되도록 설정


jobs:

build:
runs-on: ubuntu-latest

steps:
- name: Checkout the repository
uses: actions/checkout@v2

- name: Set up JDK 17
uses: actions/setup-java@v2
with:
java-version: '17'

- name: Cache Gradle packages
uses: actions/cache@v2
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle


- name: Build with Gradle (Skip Tests)
run: ./gradlew build -x test # '-x test' 옵션으로 테스트를 제외하고 빌드

- name: Docker Hub Login
run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USER_NAME }}" --password-stdin

- name: Build Docker image
run: docker build -t ${{ secrets.DOCKER_USER_NAME }}/x-be

- name: Push Docker image
run: docker push ${{ secrets.DOCKER_USER_NAME }}/x-be

deploy:
runs-on: ubuntu-latest
needs: build # CI 파이프라인의 build 작업이 끝나야 deploy가 실행됨

steps:
# EC2 서버로 배포
- name: Deploy to EC2
uses: appleboy/[email protected]
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.EC2_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
port: 22
script: |
# 배포 경로로 이동
cd /home/ubuntu/xclone

# Docker 이미지를 pull
docker pull ${{ secrets.DOCKER_USER_NAME }}/x-be:latest

# 기존 컨테이너 중지 및 삭제
docker stop app || true
docker rm app || true

# 새로운 컨테이너 실행
docker run -d --name app -p 8080:8080 ${{ secrets.DOCKER_USER_NAME }}/x-be:latest
8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# OpenJDK 17 기반의 이미지 사용
FROM openjdk:17-jdk-alpine

# 빌드된 JAR 파일을 도커 이미지에 복사
COPY build/libs/your-app.jar app.jar

# 애플리케이션 실행 (prod 프로파일로 실행)
ENTRYPOINT ["java","-Dspring.profiles.active=prod","-jar","/app.jar"]