Skip to content

Commit d2ebb29

Browse files
authored
chore: Dockerfile 세팅 및 CI/CD스크립트 리팩터링 (#20)
* [BOOK-88] refactor: 빌드 스크립트 리팩터링 * [BOOK-88] feat: prod, dev 용 Dockerfile 구현 * [BOOK-88] refactor: 코드리뷰 반영 * [BOOK-88] refactor: 도커 스테이지 최적화 * [BOOK-88] refactor: 변경된 도커파일에 맞게 스크립트 변경
1 parent d4c1705 commit d2ebb29

File tree

4 files changed

+100
-82
lines changed

4 files changed

+100
-82
lines changed

.github/workflows/dev-ci-cd.yml

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Dev CI/CD - Build and Deploy
1+
name: Dev CI/CD - Build, Push and Deploy
22

33
on:
44
push:
@@ -12,13 +12,13 @@ concurrency:
1212
env:
1313
REGISTRY: docker.io
1414
IMAGE_NAME: ninecraft0523/ninecraft-server
15+
MODULE: apis
1516

1617
jobs:
17-
build-and-push:
18+
build-push-and-deploy:
1819
runs-on: ubuntu-24.04
1920
timeout-minutes: 20
20-
outputs:
21-
tags: ${{ steps.meta.outputs.tags }}
21+
environment: development
2222

2323
steps:
2424
- name: Checkout code
@@ -28,27 +28,17 @@ jobs:
2828
run: |
2929
mkdir ./secret
3030
echo "${{ secrets.DEV_SECRET_PROPERTIES }}" > ./secret/application-dev-secret.properties
31-
echo "${{ secrets.PROD_SECRET_PROPERTIES }}" > ./secret/application-prod-secret.properties
3231
echo "${{ secrets.TEST_SECRET_PROPERTIES }}" > ./secret/application-test-secret.properties
3332
chmod 600 ./secret/*
3433
35-
- name: Set up JDK 21
36-
uses: actions/setup-java@v4
37-
with:
38-
java-version: '21'
39-
distribution: 'temurin'
40-
cache: gradle
34+
- name: Set up Docker Buildx
35+
uses: docker/setup-buildx-action@v3
4136

42-
- name: Setup Gradle
43-
uses: gradle/actions/setup-gradle@v4
37+
- name: Log in to Docker Hub
38+
uses: docker/login-action@v3
4439
with:
45-
gradle-home-cache-cleanup: true
46-
47-
- name: Grant execute permission for gradlew
48-
run: chmod +x gradlew
49-
50-
- name: Run full Gradle build
51-
run: ./gradlew build --parallel --build-cache
40+
username: ${{ secrets.DOCKERHUB_USERNAME }}
41+
password: ${{ secrets.DOCKERHUB_TOKEN }}
5242

5343
- name: Extract metadata for Docker
5444
id: meta
@@ -58,32 +48,20 @@ jobs:
5848
tags: |
5949
type=raw,value=development-latest
6050
61-
- name: Set up Docker Buildx
62-
uses: docker/setup-buildx-action@v3
63-
64-
- name: Log in to Docker Hub
65-
uses: docker/login-action@v3
66-
with:
67-
username: ${{ secrets.DOCKERHUB_USERNAME }}
68-
password: ${{ secrets.DOCKERHUB_TOKEN }}
69-
7051
- name: Build and push Docker image
52+
id: build-and-push
7153
uses: docker/build-push-action@v6
7254
with:
7355
context: .
56+
file: ./Dockerfile
7457
platforms: linux/amd64,linux/arm64
7558
push: true
7659
tags: ${{ steps.meta.outputs.tags }}
7760
cache-from: type=gha
7861
cache-to: type=gha,mode=max
62+
build-args: |
63+
MODULE=${{ env.MODULE }}
7964
80-
deploy-dev:
81-
needs: build-and-push
82-
runs-on: ubuntu-24.04
83-
timeout-minutes: 10
84-
environment: development
85-
86-
steps:
8765
- name: Deploy to Development Server
8866
uses: appleboy/[email protected]
8967
with:
@@ -92,11 +70,9 @@ jobs:
9270
key: ${{ secrets.DEV_SSH_KEY }}
9371
port: ${{ secrets.DEV_PORT }}
9472
script: |
95-
cd /opt/app
9673
export DOCKERHUB_USERNAME="${{ secrets.DOCKERHUB_USERNAME }}"
9774
export DOCKERHUB_TOKEN="${{ secrets.DOCKERHUB_TOKEN }}"
98-
export IMAGE_TAG="${{ needs.build-and-push.outputs.tags }}"
75+
export IMAGE_TAG="$(echo "${{ steps.meta.outputs.tags }}" | head -n1)"
9976
cd ~/deploy
10077
chmod +x ./deploy.sh
10178
./deploy.sh
102-

.github/workflows/prod-ci-cd.yml

Lines changed: 17 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
name: Prod CI/CD - Build and Deploy
1+
name: Prod CI/CD - Build, Push and Deploy
22

33
on:
44
release:
55
types:
6-
- published # Release가 published 때만 실행
6+
- published # GitHub Release가 'published' 상태일 때만 워크플로우를 실행합니다.
77

88
concurrency:
99
group: ${{ github.workflow }}-${{ github.ref }}
@@ -12,15 +12,13 @@ concurrency:
1212
env:
1313
REGISTRY: docker.io
1414
IMAGE_NAME: ninecraft0523/ninecraft-server
15+
MODULE: apis
1516

1617
jobs:
17-
build-and-push:
18+
build-push-and-deploy:
1819
runs-on: ubuntu-24.04
1920
timeout-minutes: 25
20-
outputs:
21-
image-digest: ${{ steps.build.outputs.digest }}
22-
version: ${{ steps.meta.outputs.version }}
23-
tags: ${{ steps.meta.outputs.tags }}
21+
environment: production
2422

2523
steps:
2624
- name: Checkout code
@@ -29,28 +27,18 @@ jobs:
2927
- name: Inject application-secret.properties from Secrets
3028
run: |
3129
mkdir ./secret
32-
echo "${{ secrets.DEV_SECRET_PROPERTIES }}" > ./secret/application-dev-secret.properties
3330
echo "${{ secrets.PROD_SECRET_PROPERTIES }}" > ./secret/application-prod-secret.properties
3431
echo "${{ secrets.TEST_SECRET_PROPERTIES }}" > ./secret/application-test-secret.properties
3532
chmod 600 ./secret/*
3633
37-
- name: Set up JDK 21
38-
uses: actions/setup-java@v4
39-
with:
40-
java-version: '21'
41-
distribution: 'temurin'
42-
cache: gradle
34+
- name: Set up Docker Buildx
35+
uses: docker/setup-buildx-action@v3
4336

44-
- name: Setup Gradle
45-
uses: gradle/actions/setup-gradle@v4
37+
- name: Log in to Docker Hub
38+
uses: docker/login-action@v3
4639
with:
47-
gradle-home-cache-cleanup: true
48-
49-
- name: Grant execute permission for gradlew
50-
run: chmod +x gradlew
51-
52-
- name: Run full Gradle build with strict validation
53-
run: ./gradlew build --parallel --build-cache --warning-mode all
40+
username: ${{ secrets.DOCKERHUB_USERNAME }}
41+
password: ${{ secrets.DOCKERHUB_TOKEN }}
5442

5543
- name: Extract metadata for Docker
5644
id: meta
@@ -61,33 +49,20 @@ jobs:
6149
type=semver,pattern={{version}}
6250
type=raw,value=production-latest
6351
64-
- name: Set up Docker Buildx
65-
uses: docker/setup-buildx-action@v3
66-
67-
- name: Log in to Docker Hub
68-
uses: docker/login-action@v3
69-
with:
70-
username: ${{ secrets.DOCKERHUB_USERNAME }}
71-
password: ${{ secrets.DOCKERHUB_TOKEN }}
72-
7352
- name: Build and push Docker image
74-
id: build
53+
id: build-and-push
7554
uses: docker/build-push-action@v6
7655
with:
7756
context: .
57+
file: ./Dockerfile
7858
platforms: linux/amd64,linux/arm64
7959
push: true
8060
tags: ${{ steps.meta.outputs.tags }}
8161
cache-from: type=gha
8262
cache-to: type=gha,mode=max
63+
build-args: |
64+
MODULE=${{ env.MODULE }}
8365
84-
deploy-prod:
85-
needs: build-and-push
86-
runs-on: ubuntu-24.04
87-
timeout-minutes: 20
88-
environment: production
89-
90-
steps:
9166
- name: Deploy to Production Server
9267
uses: appleboy/[email protected]
9368
with:
@@ -96,11 +71,10 @@ jobs:
9671
key: ${{ secrets.PROD_SSH_KEY }}
9772
port: ${{ secrets.PROD_PORT }}
9873
script: |
99-
cd /opt/app
10074
export DOCKERHUB_USERNAME="${{ secrets.DOCKERHUB_USERNAME }}"
10175
export DOCKERHUB_TOKEN="${{ secrets.DOCKERHUB_TOKEN }}"
102-
export IMAGE_TAG="$(echo "${{ needs.build-and-push.outputs.tags }}" | head -n1)"
103-
export VERSION_TAG="${{ needs.build-and-push.outputs.version }}"
76+
export IMAGE_TAG="$(echo "${{ steps.meta.outputs.tags }}" | head -n1)"
77+
export VERSION_TAG="${{ steps.meta.outputs.version }}"
10478
export RELEASE_VERSION="${{ github.event.release.tag_name }}"
10579
cd ~/deploy
10680
chmod +x ./deploy.sh

Dockerfile

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Build stage
2+
FROM gradle:8.7-jdk21 AS build
3+
ARG MODULE=apis
4+
WORKDIR /app
5+
6+
# 의존성 캐싱 최적화를 위한 단계별 복사
7+
# 1. 의존성 관련 파일만 먼저 복사
8+
COPY build.gradle settings.gradle ./
9+
COPY ${MODULE}/build.gradle ./${MODULE}/
10+
11+
# 2. 소스코드 없이 의존성만 다운로드
12+
RUN ./gradlew :${MODULE}:dependencies --no-daemon
13+
14+
# 3. 소스코드 전체 복사
15+
COPY . .
16+
17+
# 4. 실제 애플리케이션 빌드
18+
RUN ./gradlew :${MODULE}:bootJar --parallel --no-daemon
19+
20+
# Run stage
21+
FROM openjdk:21-slim
22+
ARG MODULE=apis
23+
WORKDIR /app
24+
25+
# 멀티스테이지 빌드로 최종 이미지 크기 최소화
26+
COPY --from=build /app/${MODULE}/build/libs/${MODULE}-*.jar app.jar
27+
28+
# 런타임에 필요한 secret 폴더 복사
29+
COPY --from=build /app/secret ./secret/
30+
31+
# JVM 실행 설정
32+
# - Xms512m: 초기 힙 메모리 512MB
33+
# - Xmx1g: 최대 힙 메모리 1GB
34+
ENTRYPOINT ["java", "-Xms512m", "-Xmx1g", "-jar", "app.jar"]

Dockerfile-dev

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Build stage
2+
FROM gradle:8.7-jdk21 AS build
3+
ARG MODULE=apis
4+
WORKDIR /app
5+
6+
# 의존성 캐싱 최적화를 위한 단계별 복사
7+
# 1. 의존성 관련 파일만 먼저 복사
8+
COPY build.gradle settings.gradle ./
9+
COPY ${MODULE}/build.gradle ./${MODULE}/
10+
11+
# 2. 소스코드 없이 의존성만 다운로드
12+
RUN ./gradlew :${MODULE}:dependencies --no-daemon
13+
14+
# 3. 소스코드 전체 복사
15+
COPY . .
16+
17+
# 4. 실제 애플리케이션 빌드
18+
RUN ./gradlew :${MODULE}:bootJar --parallel --no-daemon
19+
20+
# Run stage
21+
FROM openjdk:21-slim
22+
ARG MODULE=apis
23+
WORKDIR /app
24+
25+
# 멀티스테이지 빌드로 최종 이미지 크기 최소화
26+
COPY --from=build /app/${MODULE}/build/libs/${MODULE}-*.jar app.jar
27+
28+
# 런타임에 필요한 secret 폴더 복사
29+
COPY --from=build /app/secret ./secret/
30+
31+
# JVM 실행 설정
32+
# Xms512m: 초기 힙 메모리 512MB
33+
# Xmx1g: 최대 힙 메모리 1GB
34+
ENTRYPOINT ["java", "-Xms512m", "-Xmx1g", "-jar", "app.jar"]

0 commit comments

Comments
 (0)