Skip to content

Commit ba331fd

Browse files
authored
Merge pull request #40 from Geumpumta/ip
feat: 운영(Prod) 서버 CI/CD 구축 및 환경 설정 분리
2 parents b20c2f6 + d047257 commit ba331fd

File tree

7 files changed

+219
-3
lines changed

7 files changed

+219
-3
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
# CI가 성공했을 때만 실행
1515
if: ${{ github.event.workflow_run.conclusion == 'success' }}
1616
runs-on: [self-hosted]
17-
17+
1818
# [설정] GitHub Environment의 'development' 변수들(32124 포트 등)을 사용
1919
environment:
2020
name: development
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ permissions:
1515
jobs:
1616
build:
1717
runs-on: ubuntu-latest
18-
18+
1919
services:
2020
redis:
2121
image: redis:alpine

.github/workflows/prod-cd.yml

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
name: Prod - CD with Docker
2+
3+
on:
4+
workflow_run:
5+
workflows: [ "Prod - Java CI with Gradle" ] # Prod CI 파일 이름과 일치해야 함
6+
types: [ completed ]
7+
8+
permissions:
9+
contents: read
10+
packages: write
11+
12+
jobs:
13+
build:
14+
# Prod 브랜치인 경우에만 실행
15+
if: ${{ github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.head_branch == 'main' }}
16+
runs-on: ubuntu-latest
17+
outputs:
18+
image-tag: ${{ steps.meta.outputs.tags }}
19+
20+
steps:
21+
- name: Download artifact
22+
uses: actions/download-artifact@v4
23+
with:
24+
run-id: ${{ github.event.workflow_run.id }}
25+
github-token: ${{ secrets.ACTION_TOKEN }}
26+
name: build-libs
27+
28+
- name: Setup Docker Buildx
29+
uses: docker/setup-buildx-action@v3
30+
with:
31+
driver: docker-container
32+
driver-opts: |
33+
network=host
34+
35+
- name: Login to Github Container Registry
36+
uses: docker/login-action@v3
37+
with:
38+
registry: ghcr.io
39+
username: ${{ github.actor }}
40+
password: ${{ secrets.ACTION_TOKEN }}
41+
42+
- name: Extract metadata
43+
id: meta
44+
uses: docker/metadata-action@v5
45+
with:
46+
images: ghcr.io/${{ github.repository }}
47+
tags: |
48+
type=sha
49+
50+
- name: Build and Push Docker image
51+
uses: docker/build-push-action@v6
52+
with:
53+
context: .
54+
file: Dockerfile
55+
push: true
56+
tags: ${{ steps.meta.outputs.tags }}
57+
labels: ${{ steps.meta.outputs.labels }}
58+
platforms: linux/amd64,linux/arm64
59+
cache-from: type=gha
60+
cache-to: type=gha,mode=max
61+
62+
deploy:
63+
needs: build
64+
runs-on: [self-hosted]
65+
66+
# [Prod] GitHub Environment 사용
67+
environment:
68+
name: production
69+
70+
steps:
71+
- name: Login to GHCR
72+
uses: docker/login-action@v3
73+
with:
74+
registry: ghcr.io
75+
username: ${{ github.actor }}
76+
password: ${{ secrets.ACTION_TOKEN }}
77+
78+
- name: Pull latest image
79+
run: docker pull ${{ needs.build.outputs.image-tag }}
80+
81+
- name: Clean up old container and image
82+
run: |
83+
docker rm -f ${{ vars.CONTAINER_NAME }} || true
84+
docker image prune -f
85+
86+
- name: Run New Container (Prod)
87+
run: |
88+
docker run -d \
89+
--name ${{ vars.CONTAINER_NAME }} \
90+
--network ${{ vars.NETWORK_NAME }} \
91+
-p ${{ secrets.WEB_PORT }}:8080 \
92+
-e SPRING_PROFILES_ACTIVE=prod \
93+
-e TZ=Asia/Seoul \
94+
${{ needs.build.outputs.image-tag }}

.github/workflows/prod-ci.yml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
name: Prod - Java CI with Gradle
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
pull_request:
7+
branches: [ "main" ]
8+
9+
permissions:
10+
contents: read
11+
checks: write
12+
pull-requests: write
13+
14+
jobs:
15+
build:
16+
runs-on: ubuntu-latest
17+
18+
services:
19+
redis:
20+
image: redis:alpine
21+
ports:
22+
- 6379:6379
23+
options: >-
24+
--health-cmd "redis-cli ping"
25+
--health-interval 10s
26+
--health-timeout 5s
27+
--health-retries 5
28+
29+
steps:
30+
- name: Checkout
31+
uses: actions/checkout@v4
32+
with:
33+
submodules: true
34+
token: ${{ secrets.ACTION_TOKEN }}
35+
36+
- name: Set up JDK 21
37+
uses: actions/setup-java@v4
38+
with:
39+
java-version: '21'
40+
distribution: 'temurin'
41+
42+
- name: Setup Gradle
43+
uses: gradle/actions/setup-gradle@v4
44+
45+
- name: Add +x permission to gradlew
46+
run: chmod +x gradlew
47+
48+
- name: Build with Gradle
49+
run: ./gradlew clean build
50+
51+
- name: Publish Test Report
52+
uses: mikepenz/action-junit-report@v5
53+
if: success() || failure()
54+
with:
55+
report_paths: '**/build/test-results/test/TEST-*.xml'
56+
57+
- name: Upload Build Artifacts
58+
uses: actions/upload-artifact@v4
59+
with:
60+
name: build-libs
61+
path: |
62+
build/libs/*.jar
63+
Dockerfile
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
spring:
2+
config:
3+
activate:
4+
on-profile: prod
5+
import:
6+
- security/application-database.yml
7+
- security/application-security.yml
8+
- security/application-mail.yml
9+
- security/application-swagger.yml
10+
- security/application-wifi.yml
11+
- security/application-cloudinary.yml
12+
13+
datasource:
14+
url: ${geumpumta.mysql.url}
15+
username: ${geumpumta.mysql.username}
16+
password: ${geumpumta.mysql.password}
17+
driver-class-name: com.mysql.cj.jdbc.Driver
18+
19+
data:
20+
redis:
21+
host: ${geumpumta.redis.host}
22+
port: ${geumpumta.redis.port}
23+
password: ${geumpumta.redis.password}
24+
repositories:
25+
enabled: false
26+
27+
mvc:
28+
log-request-details: true
29+
30+
jpa:
31+
hibernate:
32+
ddl-auto: validate
33+
open-in-view: false
34+
show-sql: true
35+
properties:
36+
hibernate:
37+
format_sql: false
38+
highlight_sql: false
39+
default_batch_fetch_size: 100
40+
servlet:
41+
multipart:
42+
max-file-size: 10MB # 한 파일 최대 크기
43+
max-request-size: 100MB # 전체 요청 최대 크기 (여러 파일 합산)
44+
45+
server:
46+
forward-headers-strategy: framework
47+
48+
logging:
49+
level:
50+
# ---- HTTP 트래픽 흐름을 살펴봅니다.
51+
org.springframework.web: info
52+
# ---- Hibernate가 실행할 질의문을 살펴봅니다.
53+
org.hibernate.SQL: warn
54+
# ---- 질의문에 바인딩되는 파라미터를 살펴봅니다.
55+
org.hibernate.orm.jdbc.bind: warn
56+
# ---- 질의문 실행 결과를 살펴봅니다.
57+
org.hibernate.orm.jdbc.extract: warn
58+

src/main/resources/application.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ spring:
55
group:
66
local: local
77
dev: dev
8+
prod: prod
89
---
910

1011
spring:

src/main/resources/security

0 commit comments

Comments
 (0)