Skip to content

Commit

Permalink
Merge pull request #77 from capstone-five-ai/dev
Browse files Browse the repository at this point in the history
Prod 서버 CD 테스트
  • Loading branch information
yujamint authored Jun 29, 2024
2 parents 365673a + 503c029 commit c90e39b
Show file tree
Hide file tree
Showing 46 changed files with 2,490 additions and 301 deletions.
62 changes: 62 additions & 0 deletions .github/workflows/deploy-prod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
name: deploy on prod server

on:
push:
branches: [test/main-cd]

jobs:
deploy:

runs-on: ubuntu-latest

steps:
- name: 체크아웃
uses: actions/checkout@v4
with:
submodules: true
token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}

- name: 서브모듈 업데이트
run: |
git submodule update --remote
- name: JDK 11 설치
uses: actions/setup-java@v4
with:
distribution: 'corretto'
java-version: '11'
cache: 'gradle'

- name: Gradle에 실행 권한 부여
run: chmod +x gradlew

- name: 빌드
run: ./gradlew build -x test

- name: DockerHub 로그인
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Docker 이미지 빌드 & DockerHub에 Push
uses: docker/build-push-action@v6
with:
context: .
file: ./docker/Dockerfile-prod
push: true
tags: ${{ secrets.DOCKERHUB_USERNAME }}/qtudy-server:prod

- name: EC2 서버에 배포
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.PROD_SERVER_HOST }}
username: ${{ secrets.PROD_SERVER_USERNAME }}
key: ${{ secrets.PROD_SERVER_PEM_KEY }}
envs: GITHUB_SHA
script: |
sudo docker stop $(sudo docker ps -a -q)
sudo docker rm $(sudo docker ps -a -q)
sudo docker pull ${{secrets.DOCKERHUB_USERNAME}}/qtudy-server:prod
sudo docker run -d -p 8080:8080 --name qtudy-server-container ${{secrets.DOCKERHUB_USERNAME}}/qtudy-server:prod
sudo docker image prune -f
62 changes: 62 additions & 0 deletions .github/workflows/deploy-stg.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
name: deploy on stage server

on:
push:
branches: [dev]

jobs:
deploy:

runs-on: ubuntu-latest

steps:
- name: 체크아웃
uses: actions/checkout@v4
with:
submodules: true
token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}

- name: 서브모듈 업데이트
run: |
git submodule update --remote
- name: JDK 11 설치
uses: actions/setup-java@v4
with:
distribution: 'corretto'
java-version: '11'
cache: 'gradle'

- name: Gradle에 실행 권한 부여
run: chmod +x gradlew

- name: 빌드
run: ./gradlew build -x test

- name: DockerHub 로그인
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Docker 이미지 빌드 & DockerHub에 Push
uses: docker/build-push-action@v6
with:
context: .
file: ./docker/Dockerfile-stg
push: true
tags: ${{ secrets.DOCKERHUB_USERNAME }}/qtudy-server:latest

- name: EC2 서버에 배포
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.STG_SERVER_HOST }}
username: ${{ secrets.STG_SERVER_USERNAME }}
key: ${{ secrets.STG_SERVER_PEM_KEY }}
envs: GITHUB_SHA
script: |
sudo docker stop $(sudo docker ps -a -q)
sudo docker rm $(sudo docker ps -a -q)
sudo docker pull ${{secrets.DOCKERHUB_USERNAME}}/qtudy-server
sudo docker run -d -p 8080:8080 --network="host" --name qtudy-server-container ${{secrets.DOCKERHUB_USERNAME}}/qtudy-server
sudo docker image prune -f
38 changes: 38 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: run test

on:
pull_request:
branches: [main, dev]
types: [opened, synchronize, reopened]

permissions:
checks: write
pull-requests: write

jobs:
test:

runs-on: ubuntu-latest

steps:
- name: 체크아웃
uses: actions/checkout@v4

- name: JDK 11 설치
uses: actions/setup-java@v4
with:
distribution: 'corretto'
java-version: '11'
cache: 'gradle'

- name: Gradle에 실행 권한 부여
run: chmod +x gradlew

- name: Test 실행
run: ./gradlew clean test

- name: Test 결과를 PR에 코멘트로 등록
uses: EnricoMi/publish-unit-test-result-action@v2
if: always()
with:
files: '**/build/test-results/test/TEST-*.xml'
23 changes: 23 additions & 0 deletions appspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
version: 0.0
os: linux
files:
- source: /
destination: /home/ubuntu
overwrite: yes

permissions:
- object: /home/ubuntu
pattern: "**"
owner: ubuntu
group: ubuntu

hooks:
AfterInstall:
- location: scripts/shutdown-prev-application.sh
timeout: 120
runas: ubuntu

ApplicationStart:
- location: scripts/build-and-run-new-application.sh
timeout: 120
runas: ubuntu
10 changes: 10 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ dependencies {
testImplementation 'com.theokanning.openai-gpt3-java:service:0.18.2'
testImplementation 'com.knuddels:jtokkit:1.0.0'
testImplementation 'com.fasterxml.jackson.module:jackson-module-jsonSchema-jakarta:2.15.0'

// Rest Assured
testImplementation 'io.rest-assured:rest-assured:4.5.1'
}

dependencyManagement {
Expand All @@ -67,9 +70,16 @@ tasks.named('test') {
}

processResources.dependsOn('copySecret')
processResources.dependsOn('copySecretToBuild')

tasks.register('copySecret', Copy) {
from './BE_config' // 서브 모듈 디렉토리 경로
include "*.yml" // 설정 파일 복사
into 'src/main/resources' // 붙여넣을 위치
}

tasks.register('copySecretToBuild', Copy) {
from './BE_config' // 서브 모듈 디렉토리 경로
include "*.yml" // 설정 파일 복사
into '$buildDir/resources/main' // 붙여넣을 위치
}
4 changes: 4 additions & 0 deletions docker/Dockerfile-prod
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM amazoncorretto:11-alpine-jdk
EXPOSE 8080
COPY ./build/libs/Qtudy-server-0.0.1-SNAPSHOT.jar /app.jar
CMD ["java", "-jar", "app.jar", "--spring.profiles.active=deploy"]
4 changes: 4 additions & 0 deletions docker/Dockerfile-stg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM amazoncorretto:11-alpine-jdk
EXPOSE 8080
COPY ./build/libs/Qtudy-server-0.0.1-SNAPSHOT.jar /app.jar
CMD ["java", "-jar", "app.jar", "--spring.profiles.active=stage"]
5 changes: 5 additions & 0 deletions scripts/build-and-run-new-application.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
PROJECT_PATH=/home/ubuntu/Qtudy-BE
BUILD_PATH=$PROJECT_PATH/build/libs
BUILD_JAR=$BUILD_PATH/Qtudy-server-0.0.1-SNAPSHOT.jar

nohup java -jar $BUILD_JAR --spring.profiles.active=stage > $PROJECT_PATH/nohup.out 2>&1 &
7 changes: 7 additions & 0 deletions scripts/shutdown-prev-application.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
JAVA_PID=`sudo lsof -i :8080 -t`
if [ -z $JAVA_PID ]
then echo "실행되고 있는 애플리케이션이 없습니다."
else
sudo kill -9 $JAVA_PID
echo "기존에 실행되고 있던 애플리케이션을 종료했습니다."
fi
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class CategorizedProblemDto {

@Getter
@Schema(name = "CategorizedProblemPost", description = "새 카테고리화 문제 생성 요청 DTO")
@AllArgsConstructor
public static class Post {

@Schema(description = "카테고리 ID 목록", example = "[1, 2]")
Expand All @@ -31,6 +32,7 @@ public static class Post {

@Getter
@Schema(name = "CategorizedProblemPatch", description = "카테고리화 문제 수정 요청 DTO")
@AllArgsConstructor
public static class Patch{
@Schema(description = "문제 이름", example = "수정된 문제 이름")
private String problemName;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package com.app.domain.categorizedproblem.repository;

import com.app.domain.categorizedproblem.entity.CategorizedProblem;
import feign.Param;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import org.springframework.data.jpa.repository.Query;

public interface CategorizedProblemRepository extends JpaRepository<CategorizedProblem, Long> {
boolean existsByCategoryCategoryIdAndProblemProblemId(Long categoryId, Long problemId);

boolean existsByProblemProblemId(Long problemId);

Page<CategorizedProblem> findByCategoryCategoryId(Long categoryId, Pageable pageable);
@Query(value = "SELECT cp FROM CategorizedProblem cp JOIN FETCH cp.problem p JOIN FETCH cp.category c WHERE c.categoryId = :categoryId",
countQuery = "SELECT count(cp) FROM CategorizedProblem cp WHERE cp.category.categoryId = :categoryId")
Page<CategorizedProblem> findByCategoryCategoryId(@Param("categoryId") Long categoryId, Pageable pageable);

List<CategorizedProblem> findByCategoryCategoryId(Long categoryId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -244,6 +245,7 @@ public CategorizedProblem updateCategorizedProblem(Long categorizedProblemId, Me
}

@Transactional(readOnly = true)
@Cacheable(value = "categorizedProblem", key = "#categoryId")
public Page<CategorizedProblem> findCategorizedProblemsByCategoryId(Long categoryId, int page, int size) {
PageRequest pageRequest = PageRequest.of(page, size);
return categorizedProblemRepository.findByCategoryCategoryId(categoryId, pageRequest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class CategorizedSummaryDto {

@Getter
@Schema(name = "CategorizedSummaryPost", description = "새 카테고리화 요약 생성 요청 DTO")
@AllArgsConstructor
public static class Post {
@Schema(description = "카테고리 ID 목록", example = "[1, 2]")
private List<Long> categoryIdList;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package com.app.domain.categorizedsummary.repository;

import com.app.domain.categorizedsummary.entity.CategorizedSummary;
import feign.Param;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

public interface CategorizedSummaryRepository extends JpaRepository<CategorizedSummary, Long> {
boolean existsByCategoryCategoryIdAndSummarySummaryId(Long categoryId, Long summaryId);

boolean existsBySummarySummaryId(Long summaryId);

Page<CategorizedSummary> findByCategoryCategoryId(Long categoryId, Pageable pageable);
@Query(value = "SELECT cs FROM CategorizedSummary cs JOIN FETCH cs.summary s JOIN FETCH cs.category c WHERE c.categoryId = :categoryId",
countQuery = "SELECT count(cs) FROM CategorizedSummary cs WHERE cs.category.categoryId = :categoryId")
Page<CategorizedSummary> findByCategoryCategoryId(@Param("categoryId") Long categoryId, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -104,6 +105,8 @@ public CategorizedSummary findVerifiedCategorizedSummaryByCategorizedSummaryId(L
.orElseThrow(() -> new EntityNotFoundException(ErrorCode.CATEGORIZED_SUMMARY_NOT_EXISTS));
}

@Transactional(readOnly = true)
@Cacheable(value = "categorizedSummary", key = "#categoryId")
public Page<CategorizedSummary> findCategorziedSummarysByCategoryId(Long categoryId, int page, int size) {
PageRequest pageRequest = PageRequest.of(page, size);
return categorizedSummaryRepository.findByCategoryCategoryId(categoryId, pageRequest);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/app/domain/category/dto/CategoryDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
public class CategoryDto {

@Getter
@AllArgsConstructor
@Schema(name = "CategoryRequestDto", description = "카테고리 요청 DTO")
public static class RequestDto {
@NotBlank
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/app/domain/category/entity/Category.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ public class Category extends BaseEntity {
@Column(nullable = false, length = 10)
private CategoryType categoryType;

@OneToMany(mappedBy = "category")
@OneToMany(mappedBy = "category", fetch = FetchType.LAZY)
private List<CategorizedSummary> categorizedSummaries;

@OneToMany(mappedBy = "category")
@OneToMany(mappedBy = "category", fetch = FetchType.LAZY)
private List<CategorizedProblem> categorizedProblems;

public void updateMember(Member member) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/app/domain/file/entity/File.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public abstract class File extends BaseEntity {
//@Column(name = "MEMBER_ID") //추후에 Members 엔티티와 연결
//private String memberId;
@JsonIgnore
@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "MEMBER_ID")
private Member member;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import lombok.AllArgsConstructor;
Expand All @@ -20,7 +21,7 @@
public class AiGeneratedProblem extends Problem {

@JsonIgnore
@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "FILE_ID")
private ProblemFile problemFile;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
public class ProblemFile extends File {


@OneToMany(mappedBy = "problemFile", cascade = CascadeType.ALL, orphanRemoval = true)
@OneToMany(mappedBy = "problemFile", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
private List<AiGeneratedProblem> aiQuestions;


Expand Down
Loading

0 comments on commit c90e39b

Please sign in to comment.