Skip to content

Commit 057daf7

Browse files
authored
Merge pull request #1 from Geumpumta/feat/CI&CD
feat: CI/CD 추가
2 parents 52092fb + 0afe93c commit 057daf7

File tree

6 files changed

+210
-0
lines changed

6 files changed

+210
-0
lines changed

.github/workflows/cd.yml

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
name: CD with Docker
2+
3+
on:
4+
workflow_run:
5+
workflows: [ "Java CI with Gradle" ]
6+
types: [ completed ]
7+
branches: [ "dev" ]
8+
9+
permissions:
10+
contents: read
11+
packages: write
12+
13+
jobs:
14+
build:
15+
if: ${{ github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.head_branch == 'dev' }}
16+
runs-on: ubuntu-latest
17+
outputs:
18+
image-tag: ${{ steps.meta.outputs.tags }}
19+
20+
steps:
21+
# 도커 이미지 빌드를 위한 빌드 파일 내려 받기
22+
- name: Download artifact
23+
uses: actions/download-artifact@v4
24+
with:
25+
run-id: ${{ github.event.workflow_run.id }}
26+
github-token: ${{ secrets.ACTION_TOKEN }}
27+
name: build-libs
28+
29+
# 도커 이미지 빌드를 위한 Docker Buildx 내려 받기
30+
- name: Setup Docker Buildx
31+
uses: docker/setup-buildx-action@v3
32+
with:
33+
driver: docker-container
34+
driver-opts: |
35+
network=host
36+
37+
# 도커 이미지 업로드를 위한 GitHub Container Registry 로그인
38+
# https://docs.github.com/ko/packages/working-with-a-github-packages-registry/working-with-the-container-registry
39+
- name: Login to Github Container Registry
40+
uses: docker/login-action@v3
41+
with:
42+
registry: ghcr.io
43+
# 개인 사용자 토큰의 소유자가 변경되면 이 설정 파일을 다시 커밋하여
44+
# GitHub Actions 최초 실행 사용자를 변경해야 합니다.
45+
username: ${{ github.actor }}
46+
password: ${{ secrets.ACTION_TOKEN }}
47+
48+
# 메타 데이터를 추출해 이미지 태그 및 레이블 생성
49+
- name: Extract metadata
50+
id: meta
51+
uses: docker/metadata-action@v5
52+
with:
53+
images: ghcr.io/${{ github.repository }}
54+
tags: |
55+
type=sha
56+
57+
- name: Build and Push Docker image
58+
uses: docker/build-push-action@v6
59+
with:
60+
context: .
61+
file: Dockerfile
62+
push: true
63+
tags: ${{ steps.meta.outputs.tags }}
64+
labels: ${{ steps.meta.outputs.labels }}
65+
platforms: linux/amd64,linux/arm64
66+
cache-from: type=gha
67+
cache-to: type=gha,mode=max
68+
69+
deploy:
70+
needs: build
71+
runs-on: [self-hosted]
72+
steps:
73+
# Docker 이미지 내려 받기를 위한 GHCR 로그인
74+
- name: Login to GHCR
75+
uses: docker/login-action@v3
76+
with:
77+
registry: ghcr.io
78+
username: ${{ github.actor }}
79+
password: ${{ secrets.ACTION_TOKEN }}
80+
81+
# 이미지 내려 받기
82+
- name: Pull latest image
83+
run: docker pull ${{ needs.build.outputs.image-tag }}
84+
85+
# 실행 중인 컨테이너 정리 및 기존 이미지 삭제
86+
- name: Clean up old container and image
87+
run: |
88+
docker rm -f ${{ vars.CONTAINER_NAME }} || true
89+
docker images "ghcr.io/${{ github.repository }}" \
90+
--format "{{.Repository}}:{{.Tag}}" \
91+
| grep -v "${{ needs.build.outputs.image-tag }}" \
92+
| xargs -r docker rmi -f || true
93+
94+
# 새로운 이미지로 새로운 컨테이너 생성하여 실행
95+
- name: Run New Container
96+
run: |
97+
docker run -d \
98+
--name ${{ vars.CONTAINER_NAME }} \
99+
--network ${{ vars.NETWORK_NAME }} \
100+
-p ${{ secrets.DEV_WEB_PORT }}:8080 \
101+
-e USE_PROFILE=dev \
102+
-e TZ=Asia/Seoul \
103+
${{ needs.build.outputs.image-tag }}

.github/workflows/ci.yml

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# This workflow uses actions that are not certified by GitHub.
2+
# They are provided by a third-party and are governed by
3+
# separate terms of service, privacy policy, and support
4+
# documentation.
5+
# This workflow will build a Java project with Gradle and cache/restore any dependencies to improve the workflow execution time
6+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-gradle
7+
8+
name: Java CI with Gradle
9+
10+
on:
11+
push:
12+
branches: [ "dev" ]
13+
pull_request:
14+
branches: [ "dev" ]
15+
16+
permissions:
17+
contents: read
18+
checks: write
19+
pull-requests: write
20+
21+
jobs:
22+
build:
23+
runs-on: ubuntu-latest
24+
25+
services:
26+
redis:
27+
image: redis:alpine
28+
ports:
29+
- 6379:6379
30+
options: >-
31+
--health-cmd "redis-cli ping"
32+
--health-interval 10s
33+
--health-timeout 5s
34+
--health-retries 5
35+
36+
steps:
37+
# 현재 저장소의 소스 코드와 서브 모듈 내려 받기
38+
- name: Checkout
39+
uses: actions/checkout@v4
40+
with:
41+
submodules: true
42+
token: ${{ secrets.ACTION_TOKEN }} # 조직이 아닌 개인 사용자 토큰이므로 유의할 것!
43+
44+
# JDK 21 (Eclipse Temurin) 환경 구성
45+
- name: Set up JDK 21
46+
uses: actions/setup-java@v4
47+
with:
48+
java-version: '21'
49+
distribution: 'temurin'
50+
51+
# Gradle 내려 받기 및 캐싱
52+
- name: Setup Gradle
53+
uses: gradle/actions/setup-gradle@v4
54+
55+
# Gradlew 실행 권한 부여
56+
- name: Add +x permission to gradlew
57+
run: chmod +x gradlew
58+
59+
# 빌드 진행
60+
- name: Build with Gradle
61+
run: ./gradlew clean build -Dspring.profiles.active=test
62+
63+
# 테스트 결과 출력
64+
- name: Publish Test Report
65+
uses: mikepenz/action-junit-report@v5
66+
if: success() || failure() # always run even if the previous step fails
67+
with:
68+
report_paths: '**/build/test-results/test/TEST-*.xml'
69+
70+
# 빌드 완료 후 아티펙트 업로드 (Actions 탭에서 내려 받기 가능)
71+
- name: Upload Build Artifacts
72+
uses: actions/upload-artifact@v4
73+
with:
74+
name: build-libs
75+
path: |
76+
build/libs/*.jar
77+
Dockerfile

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ dependencies {
3838
testImplementation 'org.springframework.security:spring-security-test'
3939
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
4040
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
41+
testImplementation 'com.h2database:h2'
42+
implementation 'com.h2database:h2'
4143

4244
}
4345

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+
test: test
89
---
910

1011
spring:

src/test/java/com/gpt/geumpumtabackend/GeumpumtaBackendApplicationTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import org.junit.jupiter.api.Test;
44
import org.springframework.boot.test.context.SpringBootTest;
5+
import org.springframework.test.context.ActiveProfiles;
56

67
@SpringBootTest
8+
@ActiveProfiles("test")
79
class GeumpumtaBackendApplicationTests {
810

911
@Test
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
spring:
2+
config:
3+
activate:
4+
on-profile: test
5+
6+
datasource:
7+
# H2 인메모리 DB (MySQL 호환 모드)
8+
url: jdbc:h2:mem:testdb;MODE=MySQL;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false
9+
driver-class-name: org.h2.Driver
10+
username: sa
11+
password:
12+
13+
jpa:
14+
hibernate:
15+
ddl-auto: create-drop # 테스트 시 엔티티로 스키마 생성/삭제
16+
properties:
17+
hibernate:
18+
dialect: org.hibernate.dialect.MySQL8Dialect # 운영 DB가 MySQL이면 맞춰둠
19+
format_sql: true
20+
open-in-view: false
21+
22+
# 스키마/데이터 init 스크립트가 있으면 H2에서 충돌할 수 있어 비활성화
23+
sql:
24+
init:
25+
mode: never

0 commit comments

Comments
 (0)