Skip to content

Commit

Permalink
Merge pull request #7 from goormthon-Univ/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
HyungJu authored Mar 17, 2024
2 parents a5a88b1 + 5860a87 commit c58f251
Show file tree
Hide file tree
Showing 13 changed files with 276 additions and 80 deletions.
13 changes: 8 additions & 5 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
## <i>PULL REQUEST</i>

### 🎋 작업 중인 브랜치
### 🎋 Issue Ticket
-

### 🔑 주요 작업사항
-

### 🏞 (Optional) 참고 자료
- 스크린샷을 첨부해주세요.

### 관련 이슈
-

### (중요) 서브모듈이 수정되었나요?
- 기존 커밋 :
- 변경 커밋 :
- 변경 사항 :
- [] 해당 서브모듈 변경사항이 PR에 잘 반영되었나요?

### 꼭 확인해 주세요!!
-
-
76 changes: 35 additions & 41 deletions .github/workflows/CD.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ on:
branches:
- master

permissions:
contents: read
actions: read
id-token: write

jobs:
deploy:
runs-on: ubuntu-latest
Expand All @@ -15,59 +20,48 @@ jobs:
with:
token: ${{ secrets.GIT_TOKEN }}
submodules: true

- name: Setup Java 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'adopt'

- name: Update Git submodules
run: git submodule update --remote --recursive

- name: Grant execute permission for gradlew
run: chmod +x gradlew

- name: Build with Gradle
run: ./gradlew clean build --debug

- name: Get current time
uses: 1466587594/get-current-time@v2
id: current-time
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
format: YYYY-MM-DDTHH-mm-ss
utcOffset: "+09:00"

- name: Show Current Time
run: echo "CurrentTime=${{steps.current-time.outputs.formattedTime}}"

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
role-to-assume: arn:aws:iam::125183404358:role/VacgomGithubActionAssumeRole
aws-region: ap-northeast-2

- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1

- name: Invoke Gradle
uses: gradle/gradle-build-action@v2
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Build, tag, and push image to Amazon ECR
env:
PROFILE: dev
IMAGE_REPO_URL: ${{ steps.login-ecr.outputs.registry }}/vacgom
IMAGE_TAG: ${{ github.sha }}
run: ./gradlew jib --parallel
- name: Download Task Definition
run: |
docker build -t vacgom:${{steps.current-time.outputs.formattedTime}} .
docker tag vacgom:${{steps.current-time.outputs.formattedTime}} ${{ secrets.ECR_URI }}:${{steps.current-time.outputs.formattedTime}}
docker push ${{ secrets.ECR_URI }}:${{steps.current-time.outputs.formattedTime}}
- name: SSH into EC2 instance
uses: appleboy/ssh-action@master
aws ecs describe-task-definition \
--task-definition vacgom-taskdef \
--query taskDefinition \
> task-definition.json
- name: Update Task Definition
id: task-def
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: task-definition.json
container-name: backend
image: ${{ steps.login-ecr.outputs.registry }}/vacgom:${{ github.sha }}
- name: Deploy Amazon ECS task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.EC2_PRIVATE_KEY }}
port: ${{ secrets.EC2_SSH_PORT }}
script: |
aws ecr get-login-password | docker login --username AWS --password-stdin ${{ secrets.ECR_URI }}
docker pull ${{ secrets.ECR_URI }}:${{ steps.current-time.outputs.formattedTime }}
docker ps -f name=vacgom-api -q | xargs --no-run-if-empty docker container stop
docker ps -a -f name=vacgom-api -q | xargs --no-run-if-empty docker container rm
docker run -d --name vacgom-api -p 80:8080 ${{ secrets.ECR_URI }}:${{ steps.current-time.outputs.formattedTime }}
task-definition: ${{ steps.task-def.outputs.task-definition }}
service: vacgom-best-service
cluster: vacgom-cluster
wait-for-service-stability: true
32 changes: 14 additions & 18 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
@@ -1,42 +1,38 @@
name: 빌드 테스트 / 리뷰어 할당
name: 테스트 코드 및 빌드

on:
pull_request:
types: [opened, synchronize, closed]
types: [ opened, synchronize, closed ]
branches:
- 'develop'
- 'master'

jobs:
build_and_review_assign:
name: "[CI] Check Build/Testcases and Assign Reviewer"
test:
name: "[CI] Check Tests"
runs-on: ubuntu-latest

steps:
- name: (Set Up) checkout
- name: Checkout code
uses: actions/checkout@v3
with:
token: ${{ secrets.GIT_TOKEN }}
submodules: true

- name: (Set Up) Set up JDK 17
- name: Setup Java 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
distribution: 'adopt'

- name: Setup Gradle
uses: gradle/gradle-build-action@v2

- name: (Set Up) Grant Execute permission for gradlew
run: chmod 777 gradlew

- name: (Build) Build with Gradle
id: build
run: ./gradlew test -i
- name: Grant execute permission for gradlew
run: chmod +x gradlew

- name: (Assign Reviewer)
if: steps.build.outcome == 'success'
uses: hkusu/review-assign-action@v1
with:
assignees: ${{ github.actor }}
reviewers: HyungJu, h-beeen
ready-comment: '코드 리뷰 요청합니다 🙆 <reviewers>'
merged-comment: '성공적으로 Merge 되었습니다. Shout out to <reviewers> :wink:'
- name: Test with Gradle
run: ./gradlew test
23 changes: 23 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

val activeProfile = System.getenv("PROFILE") ?: "dev"
val imageTag = System.getenv("IMAGE_TAG") ?: "latest"
val repoURL: String? = System.getenv("IMAGE_REPO_URL")

plugins {
id("org.springframework.boot") version "3.2.3"
id("io.spring.dependency-management") version "1.1.4"
id("com.google.cloud.tools.jib") version "3.4.1"
kotlin("jvm") version "1.9.22"
kotlin("plugin.spring") version "1.9.22"
kotlin("plugin.jpa") version "1.9.22"
Expand Down Expand Up @@ -56,3 +61,21 @@ tasks.register<Copy>("initConfig") {
include("*.yml")
into("./src/main/resources")
}

jib {
from {
image = "amazoncorretto:17-alpine3.18"
}
to {
image = repoURL
tags = setOf(imageTag)
}
container {
jvmFlags = listOf(
"-Dspring.profiles.active=${activeProfile}",
"-Dserver.port=8080",
"-XX:+UseContainerSupport",
)
ports = listOf("8080")
}
}
21 changes: 21 additions & 0 deletions src/main/kotlin/com/vacgom/backend/domain/member/Member.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.vacgom.backend.domain.member

import com.vacgom.backend.global.auditing.BaseEntity
import jakarta.persistence.*

@Entity
@Table(name = "t_member")
class Member(nickname: String) : BaseEntity() {

@Id
@Column(name = "member_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
var id: Long? = null

@Column(name = "nickname")
val nickname: String

init {
this.nickname = nickname
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.vacgom.backend.global.exception

import com.vacgom.backend.global.exception.error.BusinessException
import com.vacgom.backend.global.exception.error.ErrorCode
import com.vacgom.backend.global.exception.error.ErrorResponse
import com.vacgom.backend.global.exception.error.GlobalError
import com.vacgom.backend.global.logger.CommonLogger
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.MethodArgumentNotValidException
import org.springframework.web.bind.MissingServletRequestParameterException
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.ResponseStatus
import org.springframework.web.bind.annotation.RestControllerAdvice

@RestControllerAdvice
class ApiExceptionHandler {
companion object : CommonLogger();

@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MethodArgumentNotValidException::class)
fun handleMethodArgumentNotValidException(ex: MethodArgumentNotValidException): ErrorResponse? {
return ErrorResponse(ex.fieldErrors)
}

@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MissingServletRequestParameterException::class)
fun missingServletRequestParameterException(): ErrorResponse? {
return ErrorResponse(GlobalError.INVALID_REQUEST_PARAM)
}

@ExceptionHandler(BusinessException::class)
fun handleBusinessException(
exception: BusinessException
): ResponseEntity<ErrorResponse?> {
logBusinessException(exception)
return convert(exception.errorCode)
}

private fun convert(
errorCode: ErrorCode
): ResponseEntity<ErrorResponse?> {
return ResponseEntity
.status(errorCode.status)
.body(ErrorResponse(errorCode))
}

private fun logBusinessException(exception: BusinessException) {
if (exception.errorCode.status.is5xxServerError) {
log.error("", exception)
} else {
log.error("Error Message = {}", exception.message)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.vacgom.backend.global.exception

import com.fasterxml.jackson.databind.ObjectMapper
import com.vacgom.backend.global.exception.error.BusinessException
import com.vacgom.backend.global.exception.error.ErrorResponse
import jakarta.servlet.FilterChain
import jakarta.servlet.ServletException
import jakarta.servlet.http.HttpServletRequest
import jakarta.servlet.http.HttpServletResponse
import org.springframework.http.HttpStatus
import org.springframework.http.MediaType
import org.springframework.stereotype.Component
import org.springframework.web.filter.OncePerRequestFilter
import java.io.IOException


@Component
class ApiExceptionHandlingFilter(
private val om: ObjectMapper
) : OncePerRequestFilter() {

@Throws(ServletException::class, IOException::class)
override fun doFilterInternal(
request: HttpServletRequest,
response: HttpServletResponse,
chain: FilterChain
) {
try {
chain.doFilter(request, response)
} catch (exception: BusinessException) {
setErrorResponse(response, exception)
}
}

private fun setErrorResponse(
response: HttpServletResponse,
exception: BusinessException
) = try {
response.contentType = MediaType.APPLICATION_JSON_VALUE
response.status = HttpStatus.UNAUTHORIZED.value()
om.writeValue(response.outputStream, ErrorResponse(exception.errorCode))
} catch (exception: IOException) {
throw RuntimeException(exception)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.vacgom.backend.global.exception.error


class BusinessException(
val errorCode: ErrorCode
) : RuntimeException(errorCode.message)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.vacgom.backend.global.exception.error

import org.springframework.http.HttpStatus

interface ErrorCode {
val message: String
val status: HttpStatus
val code: String
}
Loading

0 comments on commit c58f251

Please sign in to comment.