-
Notifications
You must be signed in to change notification settings - Fork 0
108 lines (86 loc) · 3.16 KB
/
prod-deploy-container.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
name: CI/CD Workflow for Develop
on:
# PR 작성 및 업데이트 시 CI 실행
pull_request:
branches: [ "develop" ]
# develop 브랜치로의 푸시 시 CD 실행
push:
branches: [ "develop" ]
jobs:
# 공통 설정 및 Gradle 빌드용 Setup job
gradle-setup:
runs-on: ubuntu-latest
permissions:
contents: read
steps:
# 소스 코드 체크아웃
- uses: actions/checkout@v4
# JDK 및 Gradle 설정
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
- name: Setup Gradle
uses: gradle/actions/setup-gradle@v4
with:
gradle-version: '7.3'
# Gradle Wrapper 실행 권한 추가
- name: Grant execute permission for gradlew
run: chmod +x ./gradlew
# 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
# 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 }} .
# Docker Hub에 로그인 및 이미지 푸시
- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}
- name: Push Docker image to Docker Hub
run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_IMAGE_NAME }}
# SSH 비밀 키 설정
- name: Install SSH Key
uses: webfactory/[email protected]
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USER }}
key: ${{ secrets.EC2_KEY }}
script: |
cd /home/${{ secrets.EC2_USER }} # docker-compose.yml 파일 위치로 이동
# Spring 서버 컨테이너 중지
sudo docker-compose stop SpringServer
# Spring 서버 컨테이너 삭제
sudo docker-compose rm -f SpringServer
# 사용 중인 이미지 강제 삭제 (캐시 무효화를 위해)
sudo docker images -q ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_IMAGE_NAME }} | xargs -r sudo docker rmi -f
# 최신 이미지 pull
sudo docker-compose pull SpringServer
# Spring 서버만 다시 실행
sudo docker-compose up -d SpringServer