-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
48 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,17 @@ | ||
name: Deploy to EC2 on develop | ||
name: CI/CD Workflow for Develop | ||
|
||
on: | ||
push: | ||
branches: [ "develop" ] | ||
# PR 작성 및 업데이트 시 CI 실행 | ||
pull_request: | ||
branches: [ "develop" ] | ||
|
||
# develop 브랜치로의 푸시 시 CD 실행 | ||
push: | ||
branches: [ "develop" ] | ||
|
||
jobs: | ||
build-and-push-image: | ||
# 공통 설정 및 Gradle 빌드용 Setup job | ||
gradle-setup: | ||
runs-on: ubuntu-latest | ||
permissions: | ||
contents: read | ||
|
@@ -16,14 +20,13 @@ jobs: | |
# 소스 코드 체크아웃 | ||
- uses: actions/checkout@v4 | ||
|
||
# JDK 설정 | ||
# JDK 및 Gradle 설정 | ||
- name: Set up JDK 17 | ||
uses: actions/setup-java@v4 | ||
with: | ||
java-version: '17' | ||
distribution: 'temurin' | ||
|
||
# Gradle 설정 | ||
- name: Setup Gradle | ||
uses: gradle/actions/setup-gradle@v4 | ||
with: | ||
|
@@ -35,42 +38,71 @@ jobs: | |
|
||
# Gradle 빌드 | ||
- name: Build with Gradle Wrapper | ||
id: build | ||
run: ./gradlew build | ||
|
||
# 빌드 결과물 업로드 | ||
- name: Upload build artifacts | ||
uses: actions/upload-artifact@v3 | ||
with: | ||
name: application-jar | ||
path: build/libs/*.jar | ||
|
||
# CI 작업 - Gradle 빌드만 수행 (PR에서만 실행) | ||
ci-gradle-build: | ||
runs-on: ubuntu-latest | ||
needs: gradle-setup | ||
if: github.event_name == 'pull_request' | ||
steps: | ||
- name: Check Build Result | ||
run: echo "Gradle Build completed successfully." | ||
|
||
# CD 작업 - Docker 빌드, 이미지 푸시 및 배포 (develop 브랜치에 머지 시) | ||
build-push-deploy: | ||
runs-on: ubuntu-latest | ||
needs: gradle-setup | ||
if: github.event_name == 'push' | ||
permissions: | ||
contents: read | ||
|
||
steps: | ||
# 소스 코드 체크아웃 | ||
- uses: actions/checkout@v4 | ||
|
||
# 아티팩트 다운로드 | ||
- name: Download build artifacts | ||
uses: actions/download-artifact@v3 | ||
with: | ||
name: application-jar | ||
path: build/libs | ||
|
||
# Docker 이미지 빌드 | ||
- name: Build Docker image | ||
run: docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_IMAGE_NAME }} . | ||
run: docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_IMAGE_NAME }} . | ||
|
||
# Docker Hub에 로그인 | ||
# Docker Hub에 로그인 및 이미지 푸시 | ||
- name: Log in to Docker Hub | ||
uses: docker/login-action@v2 | ||
with: | ||
username: ${{ secrets.DOCKERHUB_USERNAME }} | ||
password: ${{ secrets.DOCKERHUB_PASSWORD }} | ||
|
||
# Docker 이미지 푸시 | ||
- name: Push Docker image to Docker Hub | ||
run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_IMAGE_NAME }} | ||
|
||
deploy-to-ec2: | ||
needs: build-and-push-image | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
# SSH 비밀 키 설정 | ||
- name: Install SSH Key | ||
uses: webfactory/[email protected] | ||
with: | ||
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} | ||
|
||
# EC2에 접속하여 Docker Compose 실행 | ||
# EC2 서버에 Docker Compose로 배포 | ||
- name: Deploy on EC2 | ||
env: | ||
EC2_HOST: ${{ secrets.EC2_HOST }} | ||
EC2_USER: ${{ secrets.EC2_USER }} | ||
run: | | ||
ssh -o StrictHostKeyChecking=no $EC2_USER@$EC2_HOST << EOF | ||
cd /home/ubuntu # docker-compose.yml 파일 위치로 이동 | ||
# 기존 컨테이너 중지 | ||
|
@@ -85,22 +117,3 @@ jobs: | |
# 모든 서비스 다시 실행 | ||
docker-compose up -d | ||
EOF | ||
dependency-submission: | ||
|
||
runs-on: ubuntu-latest | ||
permissions: | ||
contents: write | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: Set up JDK 17 | ||
uses: actions/setup-java@v4 | ||
with: | ||
java-version: '17' | ||
distribution: 'temurin' | ||
|
||
# Generates and submits a dependency graph, enabling Dependabot Alerts for all project dependencies. | ||
# See: https://github.com/gradle/actions/blob/main/dependency-submission/README.md | ||
- name: Generate and submit dependency graph | ||
uses: gradle/actions/dependency-submission@af1da67850ed9a4cedd57bfd976089dd991e2582 # v4.0.0 |