Skip to content

Commit cb2545e

Browse files
committed
[#170] Refactor : 워크플로우파일 수정
1 parent 72e3c88 commit cb2545e

File tree

2 files changed

+60
-255
lines changed

2 files changed

+60
-255
lines changed

.github/workflows/dev_deploy.yml

Lines changed: 59 additions & 224 deletions
Original file line numberDiff line numberDiff line change
@@ -1,290 +1,125 @@
1+
# GrowIt 프로젝트의 CI/CD 파이프라인 워크플로우
12
name: GrowIt CI/CD Pipeline
23

4+
# 워크플로우 트리거 조건 설정
35
on:
46
push:
5-
branches: [ refactor/#170 ]
6-
paths:
7-
- 'src/**'
8-
- 'build.gradle'
9-
- 'settings.gradle'
10-
- '.github/workflows/**'
11-
12-
env:
13-
GRADLE_OPTS: "-Dorg.gradle.daemon=false -Dorg.gradle.workers.max=4"
14-
JAVA_OPTS: "-Xmx4g"
7+
branches: [ refactor/#170 ] # develop 브랜치에 push가 발생할 때만 실행
158

9+
# 실행할 작업들을 정의
1610
jobs:
11+
#----------------------------------------------------------------------- 테스트작업
1712
test:
1813
runs-on: ubuntu-latest
1914

2015
steps:
16+
17+
# 1. 깃허브 저장소 코드를 워크플로우 환경으로 가져오기
2118
- uses: actions/checkout@v3
2219

20+
# 2. Java 개발 킷(JDK) 17 버전 설치 및 설정
2321
- name: Set up JDK 17
2422
uses: actions/setup-java@v3
2523
with:
26-
distribution: 'temurin'
27-
java-version: '17'
28-
29-
- name: Setup Gradle Cache
30-
uses: actions/cache@v3
31-
with:
32-
path: |
33-
~/.gradle/caches
34-
~/.gradle/wrapper
35-
key: '${{ runner.os }}-gradle-${{ hashFiles(''**/*.gradle*'', ''**/gradle-wrapper.properties'') }}'
36-
restore-keys: |
37-
${{ runner.os }}-gradle-
38-
39-
- name: Make test application.yml
40-
run: |
41-
mkdir -p ./src/test/resources
42-
mkdir -p ./src/main/resources
43-
44-
# test용 application.yml 생성
45-
cat << EOF > ./src/test/resources/application.yml
46-
server:
47-
port: 8080
48-
address: 0.0.0.0
49-
50-
spring:
51-
datasource:
52-
driver-class-name: org.h2.Driver
53-
url: jdbc:h2:mem:testdb;MODE=MySQL;DB_CLOSE_DELAY=-1
54-
username: sa
55-
password:
56-
57-
sql:
58-
init:
59-
mode: never
60-
61-
jpa:
62-
properties:
63-
hibernate:
64-
dialect: org.hibernate.dialect.H2Dialect
65-
show_sql: true
66-
format_sql: true
67-
use_sql_comments: true
68-
hbm2ddl:
69-
auto: create-drop
70-
default_batch_fetch_size: 1000
71-
72-
oauth2:
73-
client:
74-
ios-kakao:
75-
client-authentication-method: client_secret_post
76-
client-id: test-client-id
77-
client-secret: test-client-secret
78-
redirect-uri: test-redirect-uri
79-
authorization-grant-type: authorization_code
80-
scope:
81-
- account_email
82-
- profile_nickname
83-
client-name: ios-kakao
84-
server-kakao:
85-
client-authentication-method: client_secret_post
86-
client-id: test-client-id
87-
client-secret: test-client-secret
88-
redirect-uri: http://localhost:8080/auth/kakao/test
89-
authorization-grant-type: authorization_code
90-
scope:
91-
- account_email
92-
- profile_nickname
93-
client-name: server-kakao
94-
provider:
95-
kakao:
96-
authorization-uri: https://kauth.kakao.com/oauth/authorize
97-
token-uri: https://kauth.kakao.com/oauth/token
98-
user-info-uri: https://kapi.kakao.com/v2/user/me
99-
user-name-attribute: id
24+
distribution: 'temurin' # Eclipse Temurin JDK 사용
25+
java-version: '17' # Java 17 버전 지정
10026

101-
jwt:
102-
secretKey: 6ec1d58be4b8cee07c36adf42bdc4d621fb5fb49136bc0763265ad58d4eada6b
103-
104-
mail:
105-
host: smtp.gmail.com
106-
port: 587
107-
108-
password: test-password
109-
properties:
110-
mail:
111-
smtp:
112-
auth: true
113-
starttls:
114-
enable: true
115-
required: true
116-
connectiontimeout: 5000
117-
timeout: 5000
118-
writetimeout: 5000
119-
auth-code-expiration-millis: 1800000
120-
121-
aws:
122-
accessKey: test-access-key
123-
secretKey: test-secret-key
124-
s3:
125-
bucket: test-bucket
126-
base-url: http://test-url
127-
128-
openai:
129-
model1: test-model1
130-
model2: test-model2
131-
api:
132-
key: test-key
133-
url: test-url
134-
135-
management:
136-
endpoints:
137-
web:
138-
exposure:
139-
include: prometheus
140-
metrics:
141-
enable:
142-
all: true
143-
prometheus:
144-
metrics:
145-
export:
146-
enabled: true
147-
EOF
148-
149-
# 메인 application.yml 생성
150-
echo "${{ secrets.APPLICATION_YML }}" > ./src/main/resources/application.yml
151-
shell: bash
152-
153-
- name: Grant execute permission for gradlew
154-
run: chmod +x gradlew
155-
156-
- name: Test with Gradle
157-
run: ./gradlew test --parallel --max-workers=4
158-
159-
- name: Publish Test Results
160-
uses: actions/upload-artifact@v4
161-
if: success() || failure()
162-
with:
163-
name: test-results
164-
path: build/reports/tests/
27+
- name: Run Tests
28+
run: ./gradlew test
16529

30+
# --------------------------------------------------- 빌드 작업
16631
build:
16732
needs: test
168-
runs-on: ubuntu-latest
33+
runs-on: ubuntu-latest # Ubuntu 최신 버전에서 실행
16934

17035
steps:
36+
# 1. 깃허브 저장소 코드를 워크플로우 환경으로 가져오기
17137
- uses: actions/checkout@v3
17238

39+
# 2. Java 개발 킷(JDK) 17 버전 설치 및 설정
17340
- name: Set up JDK 17
17441
uses: actions/setup-java@v3
17542
with:
176-
distribution: 'temurin'
177-
java-version: '17'
178-
179-
- name: Setup Gradle Cache
180-
uses: actions/cache@v3
181-
with:
182-
path: |
183-
~/.gradle/caches
184-
~/.gradle/wrapper
185-
key: '${{ runner.os }}-gradle-${{ hashFiles(''**/*.gradle*'', ''**/gradle-wrapper.properties'') }}'
186-
restore-keys: |
187-
${{ runner.os }}-gradle-
43+
distribution: 'temurin' # Eclipse Temurin JDK 사용
44+
java-version: '17' # Java 17 버전 지정
18845

46+
# 3. application.yml 설정 파일 생성
18947
- name: Make application.yml
19048
run: |
191-
cd ./src/main/resources
192-
echo "${{ secrets.APPLICATION_YML }}" > ./application.yml
49+
cd ./src/main/resources # resources 디렉토리로 이동
50+
echo "${{ secrets.APPLICATION_YML }}" > ./application.yml # GitHub Secrets에서 설정값 가져와서 파일 생성
19351
shell: bash
19452

53+
# 4. gradlew 파일에 실행 권한 부여
19554
- name: Grant execute permission for gradlew
19655
run: chmod +x gradlew
19756

57+
# 5. Gradle을 사용하여 프로젝트 빌드
19858
- name: Build with Gradle
19959
uses: gradle/gradle-build-action@v2
20060
with:
201-
arguments: build -x test --parallel --build-cache
61+
arguments: build -x test # 테스트 제외하고 빌드
62+
20263

64+
# 6. 빌드된 JAR 파일을 아티팩트로 업로드
20365
- name: Upload build artifact
20466
uses: actions/upload-artifact@v4
20567
with:
206-
name: GrowItServer
207-
path: build/libs/*.jar
208-
retention-days: 1
68+
name: GrowItServer # 아티팩트 이름 지정
69+
path: build/libs/*.jar # 업로드할 JAR 파일 경로
20970

71+
# 배포 작업
21072
deploy:
211-
needs: build
73+
needs: build # build 작업이 성공적으로 완료된 후에 실행
21274
runs-on: ubuntu-latest
21375

21476
steps:
77+
# 1. 이전 빌드 작업에서 생성된 아티팩트 다운로드
21578
- name: Download build artifact
21679
uses: actions/download-artifact@v4
21780
with:
218-
name: GrowItServer
219-
path: build/libs/
220-
221-
- name: Health check before deploy
222-
run: |
223-
response=$(curl --write-out '%{http_code}' --silent --output /dev/null http://${{ secrets.EC2_HOST }}:8080/health || echo "000")
224-
echo "Current health status: $response"
81+
name: GrowItServer # 다운로드할 아티팩트 이름
82+
path: build/libs/ # 저장할 경로
22583

84+
# 2. EC2 인스턴스에 배포
22685
- name: Deploy to EC2
22786
env:
228-
EC2_SSH_KEY: ${{ secrets.EC2_SSH_KEY }}
229-
EC2_USERNAME: ${{ secrets.EC2_USERNAME }}
230-
EC2_HOST: ${{ secrets.EC2_HOST }}
87+
# 환경 변수 설정 (GitHub Secrets에서 가져옴)
88+
EC2_SSH_KEY: ${{ secrets.EC2_SSH_KEY }} # EC2 접속용 SSH 키
89+
EC2_USERNAME: ${{ secrets.EC2_USERNAME }} # EC2 사용자 이름
90+
EC2_HOST: ${{ secrets.EC2_HOST }} # EC2 호스트 주소
23191
run: |
92+
# SSH 키 파일 생성
23293
echo "$EC2_SSH_KEY" > private_key.pem
94+
95+
# SSH 키 파일 권한 설정 (소유자만 읽기/쓰기 가능)
23396
chmod 600 private_key.pem
23497
98+
# 빌드된 JAR 파일 찾기 (*plain.jar 제외)
23599
jar_file=$(find build/libs -name '*.jar' ! -name '*plain.jar' | head -n 1)
236100
237-
cat << 'EOF' > deploy.sh
238-
#!/bin/bash
239-
240-
APP_NAME="GrowItServer"
241-
JAR_PATH="/home/$EC2_USERNAME/$APP_NAME.jar"
242-
LOG_PATH="/home/$EC2_USERNAME/app.log"
243-
BACKUP_PATH="/home/$EC2_USERNAME/backup"
244-
245-
mkdir -p $BACKUP_PATH
246-
247-
if [ -f $JAR_PATH ]; then
248-
mv $JAR_PATH $BACKUP_PATH/$APP_NAME.jar.$(date +%Y%m%d_%H%M%S)
249-
fi
250-
251-
if [ -f $LOG_PATH ]; then
252-
mv $LOG_PATH $BACKUP_PATH/app.log.$(date +%Y%m%d_%H%M%S)
253-
fi
254-
255-
pid=$(pgrep -f $APP_NAME)
256-
if [ ! -z "$pid" ]; then
257-
kill -15 $pid
258-
sleep 10
259-
260-
if ps -p $pid > /dev/null; then
261-
kill -9 $pid
101+
# 기존 JAR 파일 삭제
102+
ssh -i private_key.pem -o StrictHostKeyChecking=no $EC2_USERNAME@$EC2_HOST "
103+
if [ -f /home/$EC2_USERNAME/GrowItServer.jar ]; then
104+
rm -f /home/$EC2_USERNAME/GrowItServer.jar
262105
fi
263-
fi
106+
"
264107
265-
nohup java -jar \
266-
-Dspring.profiles.active=prod \
267-
-XX:+HeapDumpOnOutOfMemoryError \
268-
-XX:HeapDumpPath=/home/$EC2_USERNAME/heapdump.hprof \
269-
-Xms512m -Xmx1024m \
270-
$JAR_PATH > $LOG_PATH 2>&1 &
108+
# JAR 파일을 EC2로 전송 (StrictHostKeyChecking 무시)
109+
scp -i private_key.pem -o StrictHostKeyChecking=no "$jar_file" $EC2_USERNAME@$EC2_HOST:/home/$EC2_USERNAME/GrowItServer.jar
271110
272-
sleep 30
273-
if pgrep -f $APP_NAME > /dev/null; then
274-
echo "Application started successfully"
275-
exit 0
276-
else
277-
echo "Application failed to start"
278-
exit 1
279-
fi
280-
EOF
111+
# EC2에 SSH 접속하여 애플리케이션 재시작
112+
ssh -i private_key.pem -o StrictHostKeyChecking=no $EC2_USERNAME@$EC2_HOST "
113+
# 실행 중인 Java 프로세스 종료
114+
pgrep java | xargs -r kill -15
281115
282-
scp -i private_key.pem -o StrictHostKeyChecking=no deploy.sh $EC2_USERNAME@$EC2_HOST:/home/$EC2_USERNAME/
283-
scp -i private_key.pem -o StrictHostKeyChecking=no "$jar_file" $EC2_USERNAME@$EC2_HOST:$JAR_PATH
116+
# 프로세스 종료 대기
117+
sleep 10
284118
285-
ssh -i private_key.pem -o StrictHostKeyChecking=no $EC2_USERNAME@$EC2_HOST "
286-
chmod +x /home/$EC2_USERNAME/deploy.sh
287-
/home/$EC2_USERNAME/deploy.sh
119+
# 새 버전 애플리케이션 실행 (백그라운드에서)
120+
# app.log 파일에 표준 출력과 에러를 기록
121+
nohup java -jar /home/$EC2_USERNAME/GrowItServer.jar > app.log 2>&1 &
288122
"
289123
124+
# 보안을 위해 SSH 키 파일 삭제
290125
rm -f private_key.pem

src/test/java/umc/GrowIT/Server/GrowItServerApplicationTests.java

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,8 @@
22

33
import org.junit.jupiter.api.Test;
44
import org.springframework.boot.test.context.SpringBootTest;
5-
import org.springframework.test.context.TestPropertySource;
65

7-
@SpringBootTest(
8-
webEnvironment = SpringBootTest.WebEnvironment.MOCK,
9-
properties = {
10-
"spring.datasource.url=jdbc:h2:mem:testdb",
11-
"spring.datasource.driverClassName=org.h2.Driver",
12-
"spring.datasource.username=sa",
13-
"spring.datasource.password=",
14-
"spring.jpa.database-platform=org.hibernate.dialect.H2Dialect",
15-
"spring.h2.console.enabled=true",
16-
"spring.jpa.hibernate.ddl-auto=create-drop",
17-
"spring.jpa.show-sql=true",
18-
19-
"aws.accessKey=test",
20-
"aws.secretKey=test",
21-
"aws.s3.bucket=test",
22-
"aws.s3.base-url=http://test",
23-
24-
"jwt.secretKey=testsecretkeytestsecretkeytestsecretkeytestsecretkey",
25-
26-
"openai.api.key=test",
27-
"openai.api.url=test",
28-
"openai.model1=test",
29-
"openai.model2=test",
30-
31-
"mail.host=test",
32-
"mail.port=587",
33-
"mail.username=test",
34-
"mail.password=test"
35-
}
36-
)
6+
@SpringBootTest
377
class GrowItServerApplicationTests {
388

399
@Test

0 commit comments

Comments
 (0)