Skip to content

Commit

Permalink
Merge branch 'main' into production
Browse files Browse the repository at this point in the history
  • Loading branch information
SSung023 committed May 13, 2024
2 parents 312613e + 31863e2 commit c9f8fac
Show file tree
Hide file tree
Showing 85 changed files with 1,261 additions and 766 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Build and Deploy to EC2

on:
push:
branches: [ "production", "deploy-test" ]
branches: [ "production" ]
pull_request:
branches: [ "production", "deploy-test" ]

Expand Down
52 changes: 52 additions & 0 deletions .github/workflows/prTest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: Run gradlew clean test when PR

on:
pull_request:
branches: [ "main" ]

jobs:
PRTest:
runs-on: ubuntu-latest
permissions: write-all
steps:
- uses: actions/checkout@v4

- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'

- name: make application.yml
run: |
mkdir -p ./src/main/resources
cd ./src/main/resources
touch ./application.yml
touch ./application-common.yml
touch ./application-prod.yml
echo "${{ secrets.APPLICATION }}" > ./application.yml
echo "${{ secrets.COMMON }}" > ./application-common.yml
echo "${{ secrets.PROD }}" > ./application-prod.yml
- name: make test application.yml
run: |
mkdir -p ./src/test/resources
cd ./src/test/resources
touch ./application.yml
touch ./application-test.yml
echo "${{ secrets.APPLICATION_TEST }}" > ./application.yml
echo "${{ secrets.TEST }}" > ./application-test.yml
- name: Grant execute permission for gradlew
run: chmod +x ./gradlew
shell: bash

- name: Build and Test
run: ./gradlew clean test

# Test 후 Report 생성
- name: Publish Test Results
uses: EnricoMi/publish-unit-test-result-action@v2
if: always()
with:
junit_files: '**/build/test-results/test/TEST-*.xml'
25 changes: 18 additions & 7 deletions appspec.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
version: 0.0
os: linux
version: 0.0 # CodeDeploy Version.

os: linux # 배포할 서버의 운영체제

files:
- source: /
destination: /home/ubuntu/app
overwrite: yes
- source: / # CodeDeploy에서 전달해 준 파일 중 destination으로 이동시킬 대상을 지정 (루트 경로 : 전체 파일)
destination: /home/ubuntu/app # source에서 지정된 파일을 받을 위치
overwrite: yes # 기존 파일들을 덮어 쓰기

# CodeDeploy에서 EC2로 넘겨준 파일들을 모두 ec2-user 권한 부여.
permissions:
- object: /
pattern: "**"
owner: ubuntu
group: ubuntu

# CodeDeploy 배포 단계에서 실행할 명령어를 지정 (차례대로 스크립트들이 실행)
hooks:
ApplicationStart:
- location: scripts/deploy.sh
timeout: 60
- location: scripts/run_new_was.sh
timeout: 180
runas: ubuntu
- location: scripts/health.sh
timeout: 180
runas: ubuntu
- location: scripts/switch.sh
timeout: 180
runas: ubuntu
11 changes: 7 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group = 'com.genius'
version = '0.0.1-SNAPSHOT'
version = '0.0.1-SNAPSHOT' + new Date().format("yyyyMMddHHmmss")

java {
sourceCompatibility = '17'
Expand All @@ -28,6 +28,9 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-web'

// AWS
implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.1.RELEASE'

// json
implementation 'com.googlecode.json-simple:json-simple:1.1.1'

Expand All @@ -53,9 +56,9 @@ dependencies {
// testImplementation 'de.flapdoodle.embed:de.flapdoodle.embed.mongo'

// H2
implementation 'com.h2database:h2'
runtimeOnly 'com.h2database:h2:2.2.222'
testRuntimeOnly 'com.h2database:h2'
//implementation 'com.h2database:h2'
//runtimeOnly 'com.h2database:h2:2.2.222'
testRuntimeOnly 'com.h2database:h2:2.2.222'

// Github API for Java
implementation 'org.kohsuke:github-api:1.318'
Expand Down
2 changes: 1 addition & 1 deletion scripts/deploy.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
##!/bin/bash

BUILD_JAR=$(ls /home/ubuntu/app/build/libs/*.jar)
JAR_NAME=$(basename $BUILD_JAR)
Expand Down
43 changes: 43 additions & 0 deletions scripts/health.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# health_check.sh

#!/bin/bash

# Crawl current connected port of WAS
CURRENT_PORT=$(cat /home/ubuntu/service_url.inc | grep -Po '[0-9]+' | tail -1)
TARGET_PORT=0

# Toggle port Number
if [ ${CURRENT_PORT} -eq 8081 ]; then
TARGET_PORT=8082
elif [ ${CURRENT_PORT} -eq 8082 ]; then
TARGET_PORT=8081
else
echo "> No WAS is connected to nginx"
exit 1
fi


echo "> Start health check of WAS at http://localhost:${TARGET_PORT}/api/auth/health-check ..."

for RETRY_COUNT in 1 2 3 4 5 6 7 8 9 10
do
echo "> #${RETRY_COUNT} trying..."
RESPONSE_CODE=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:${TARGET_PORT}/api/auth/health-check)

if [ ${RESPONSE_CODE} -eq 200 ]; then
echo "> New WAS successfully running"
exit 0
elif [ ${RETRY_COUNT} -eq 10 ]; then
echo "> Health check failed."
exit 1
fi
sleep 10

# if [ ${CURRENT_PORT} -eq ${TARGET_PORT} ]; then
# echo "> Health Check Failed."
# exit 1
# else
# echo "> New WAS Successfully running."
# exit 0
# fi
done
30 changes: 30 additions & 0 deletions scripts/run_new_was.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# run_new_was.sh

#!/bin/bash

# 현재 포트 읽어오기
CURRENT_PORT=$(cat /home/ubuntu/service_url.inc | grep -Po '[0-9]+' | tail -1)
TARGET_PORT=0

echo "> Current port of running WAS is ${CURRENT_PORT}."

# 현재 포트가 8081이면 새로 WAS를 띄울 타겟 포트는 8082, 반대라면 8081
if [ ${CURRENT_PORT} -eq 8081 ]; then
TARGET_PORT=8082
elif [ ${CURRENT_PORT} -eq 8082 ]; then
TARGET_PORT=8081
else
echo "> No WAS is connected to nginx"
fi

TARGET_PID=$(lsof -Fp -i TCP:${TARGET_PORT} | grep -Po 'p[0-9]+' | grep -Po '[0-9]+')

# 만약 타겟 포트에도 WAS가 떠있다면, kill하고 새롭게 WAS를 띄움
if [ ! -z ${TARGET_PID} ]; then
echo "> Kill WAS running at ${TARGET_PORT}."
sudo kill ${TARGET_PID}
fi

nohup java -jar -Dserver.port=${TARGET_PORT} /home/ubuntu/app/build/libs/* > /home/ubuntu/nohup.out 2>&1 &
echo "> Now new WAS runs at ${TARGET_PORT}."
exit 0
21 changes: 0 additions & 21 deletions scripts/start.sh

This file was deleted.

14 changes: 0 additions & 14 deletions scripts/stop.sh

This file was deleted.

29 changes: 29 additions & 0 deletions scripts/switch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# switch.sh

#!/bin/bash

# Crawl current connected port of WAS
CURRENT_PORT=$(cat /home/ubuntu/service_url.inc | grep -Po '[0-9]+' | tail -1)
TARGET_PORT=0

echo "> Nginx currently proxies to ${CURRENT_PORT}."

# Toggle port number
if [ ${CURRENT_PORT} -eq 8081 ]; then
TARGET_PORT=8082
elif [ ${CURRENT_PORT} -eq 8082 ]; then
TARGET_PORT=8081
else
echo "> No WAS is connected to nginx"
exit 1
fi

# Change proxying port into target port
echo "set \$service_url http://127.0.0.1:${TARGET_PORT};" | tee /home/ubuntu/service_url.inc

echo "> Now Nginx proxies to ${TARGET_PORT}."

# Reload nginx
sudo service nginx reload

echo "> Nginx reloaded."
1 change: 1 addition & 0 deletions src/main/java/com/genius/gitget/GitgetApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
@EnableJpaAuditing
@EnableMongoRepositories
public class GitgetApplication {

public static void main(String[] args) {
SpringApplication.run(GitgetApplication.class, args);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package com.genius.gitget.admin.topic.controller;

import static com.genius.gitget.global.util.exception.SuccessCode.CREATED;
import static com.genius.gitget.global.util.exception.SuccessCode.SUCCESS;

import com.genius.gitget.admin.topic.dto.TopicCreateRequest;
import com.genius.gitget.admin.topic.dto.TopicDetailResponse;
import com.genius.gitget.admin.topic.dto.TopicIndexResponse;
import com.genius.gitget.admin.topic.dto.TopicPagingResponse;
import com.genius.gitget.admin.topic.dto.TopicUpdateRequest;
import com.genius.gitget.admin.topic.service.TopicService;
import com.genius.gitget.global.util.exception.SuccessCode;
import com.genius.gitget.global.util.response.dto.CommonResponse;
import com.genius.gitget.global.util.response.dto.PagingResponse;
import com.genius.gitget.global.util.response.dto.SingleResponse;
import java.io.IOException;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand All @@ -21,10 +23,9 @@
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequiredArgsConstructor
Expand All @@ -40,40 +41,42 @@ public ResponseEntity<PagingResponse<TopicPagingResponse>> getAllTopics(
Page<TopicPagingResponse> allTopics = topicService.getAllTopics(pageable);

return ResponseEntity.ok().body(
new PagingResponse<>(SuccessCode.SUCCESS.getStatus(), SuccessCode.SUCCESS.getMessage(), allTopics)
new PagingResponse<>(SUCCESS.getStatus(), SUCCESS.getMessage(), allTopics)
);
}

// 토픽 상세 정보 요청
@GetMapping("/{id}")
public ResponseEntity<SingleResponse<TopicDetailResponse>> getTopicById(@PathVariable Long id) throws IOException {
public ResponseEntity<SingleResponse<TopicDetailResponse>> getTopicById(@PathVariable Long id) {
TopicDetailResponse topicDetail = topicService.getTopicById(id);
return ResponseEntity.ok().body(
new SingleResponse<>(SuccessCode.SUCCESS.getStatus(), SuccessCode.SUCCESS.getMessage(), topicDetail)
new SingleResponse<>(SUCCESS.getStatus(), SUCCESS.getMessage(), topicDetail)
);
}

// 토픽 생성 요청
@PostMapping
public ResponseEntity<CommonResponse> createTopic(
@RequestPart(value = "data") TopicCreateRequest topicCreateRequest,
@RequestPart(value = "files", required = false) MultipartFile multipartFile,
@RequestPart(value = "type") String type) {
topicService.createTopic(topicCreateRequest, multipartFile, type);
public ResponseEntity<SingleResponse<TopicIndexResponse>> createTopic(
@RequestBody TopicCreateRequest topicCreateRequest) {
Long topicId = topicService.createTopic(topicCreateRequest);
TopicIndexResponse topicUpdateResponse = new TopicIndexResponse(topicId);

return ResponseEntity.ok().body(
new CommonResponse(SuccessCode.CREATED.getStatus(), SuccessCode.CREATED.getMessage())
new SingleResponse<>(
CREATED.getStatus(), CREATED.getMessage(), topicUpdateResponse)
);
}

// 토픽 수정 요청
@PatchMapping("/{id}")
public ResponseEntity<CommonResponse> updateTopic(@PathVariable Long id,
@RequestPart(value = "data") TopicUpdateRequest topicUpdateRequest,
@RequestPart(value = "files", required = false) MultipartFile multipartFile,
@RequestPart(value = "type") String type) {
topicService.updateTopic(id, topicUpdateRequest, multipartFile, type);
public ResponseEntity<SingleResponse<TopicIndexResponse>> updateTopic(
@PathVariable Long id,
@RequestBody TopicUpdateRequest topicUpdateRequest) {
Long topicId = topicService.updateTopic(id, topicUpdateRequest);
TopicIndexResponse topicUpdateResponse = new TopicIndexResponse(topicId);

return ResponseEntity.ok().body(
new CommonResponse(SuccessCode.SUCCESS.getStatus(), SuccessCode.SUCCESS.getMessage())
new SingleResponse<>(SUCCESS.getStatus(), SUCCESS.getMessage(), topicUpdateResponse)
);
}

Expand All @@ -82,7 +85,7 @@ public ResponseEntity<CommonResponse> updateTopic(@PathVariable Long id,
public ResponseEntity<CommonResponse> deleteTopic(@PathVariable Long id) {
topicService.deleteTopic(id);
return ResponseEntity.ok().body(
new CommonResponse(SuccessCode.SUCCESS.getStatus(), SuccessCode.SUCCESS.getMessage())
new CommonResponse(SUCCESS.getStatus(), SUCCESS.getMessage())
);
}
}
Loading

0 comments on commit c9f8fac

Please sign in to comment.