Skip to content

Commit

Permalink
#95 feat: ci 액션 구축 및 배포간 오류 수정 (#109)
Browse files Browse the repository at this point in the history
* feat: CI 테스크 구현

* fix: 테스트에 구현 미반영 사항 반영

* feat: code-ci 액션 구현

* feat: 환경 변수 추가

* feat: info 로그 레벨 추가

* feat: 빌드시 테스트 제외 추가

* feat: stacktrace 로그 레밸 변경

* refactor: 기존 도커이미지 빌드 테스크 제거

* feat: jooqCodegen step 추가

* fix: 오타 수정 :api -> api

* refactor: dockerBuildImage -> buildDockerImage로 테스크 수정

* feat: Docker Login 스텝 추가

* refactor: code-ci 트리거 조건 수정 pr -> push

* feat: integration-test 액션 추가

* feat: docker-compose-api 추가

* fix: 공통으로 적용되던 build 설정 제거

* fix: Dockerfile java 버전 업

* refactor: docker-compose-api 노출 포트 수정 8081 -> 8080

* fix: @EnableWebMvc가 없어서 배포 이미지가 동작하지 않는 문제 해결

- todo: 로컬에서는 왜 없어도 동작하는지 확인 필요
  • Loading branch information
belljun3395 authored Jun 25, 2024
1 parent cbea94a commit ffd4eb2
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 25 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/code-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: CI Process

on:
push:
branches: [ "main" ]
workflow_dispatch:

permissions:
contents: read

env:
RELEASE_VERSION: ${{ github.sha }}
MAIL_PASSWORD: ${{ secrets.MAIL_PASSWORD }}

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'

- name: Jooq Code Generation
run: |
./gradlew --info jooqCodegen
- name: Build with Gradle
run: |
./gradlew --info api:build
- name: Test with Gradle
run: |
./gradlew --info test
- name : Docker Login
run: docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}

- name: Build Docker Image
run: |
./gradlew --info api:buildDockerImage
35 changes: 35 additions & 0 deletions .github/workflows/integration-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Integration Test

on:
pull_request:
branches: [ "main" ]
workflow_dispatch:

permissions:
contents: read

env:
MAIL_PASSWORD: ${{ secrets.MAIL_PASSWORD }}

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'

- name: Jooq Code Generation
run: |
./gradlew --info jooqCodegen
- name: Build with Gradle
run: |
./gradlew --info api:build
- name: Test with Gradle
run: |
./gradlew --info test
17 changes: 17 additions & 0 deletions api/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM openjdk:18-oracle

RUN mkdir -p /logs

ENV PROFILE default
ENV TZ=Asia/Seoul
EXPOSE 8080

ARG JAVA_OPTS

ARG RELEASE_VERSION
ENV DD_VERSION=${RELEASE_VERSION}

ARG JAR_FILE="build/libs/api.jar"
COPY ${JAR_FILE} app.jar

ENTRYPOINT java -XX:MaxGCPauseMillis=100 -XX:InitialRAMPercentage=50.0 -XX:MaxRAMPercentage=80.0 -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005 $JAVA_OPTS -jar app.jar
37 changes: 37 additions & 0 deletions api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,41 @@ tasks.register("generateApiSwaggerUI", Copy::class) {
val generateSwaggerUISampleTask = tasks.named("generateSwaggerUIApi", GenerateSwaggerUI::class).get()
from(generateSwaggerUISampleTask.outputDir)
into("$projectDir/src/main/resources/static/docs/swagger-ui")
}

val imageName = project.hasProperty("imageName").let {
if (it) {
project.property("imageName") as String
} else {
"api"
}
}
val releaseVersion = project.hasProperty("releaseVersion").let {
if (it) {
project.property("releaseVersion") as String
} else {
"latest"
}
}

tasks.register("buildDockerImage") {
dependsOn("bootJar")
dependsOn("generateApiSwaggerUI")

doLast {
exec {
workingDir(".")
commandLine("docker", "run", "--privileged", "--rm", "tonistiigi/binfmt", "--install", "all")
}

exec {
workingDir(".")
commandLine("docker", "buildx", "create", "--use")
}

exec {
workingDir(".")
commandLine("docker", "buildx", "build", "--platform=linux/amd64,linux/arm64", "-t", "fewletter/$imageName", "--build-arg", "RELEASE_VERSION=$releaseVersion", ".", "--push")
}
}
}
2 changes: 2 additions & 0 deletions api/src/main/kotlin/com/few/api/config/ApiConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import com.few.batch.config.BatchConfig
import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Import
import org.springframework.web.servlet.config.annotation.EnableWebMvc

@Configuration
@ComponentScan(basePackages = [ApiConfig.BASE_PACKAGE])
@Import(ApiRepoConfig::class, BatchConfig::class)
@EnableWebMvc
class ApiConfig {
companion object {
const val BASE_PACKAGE = "com.few.api"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class SubscriptionControllerTest : ControllerTestSpec() {
val workbookId = 1L
val memberId = 1L

val useCaseIn = SubscribeWorkbookUseCaseIn(workbookId = workbookId, email = email, memberId = memberId)
val useCaseIn = SubscribeWorkbookUseCaseIn(workbookId = workbookId, email = email)
doNothing().`when`(subscribeWorkbookUseCase).execute(useCaseIn)

// when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class WorkBookControllerTest : ControllerTestSpec() {
`when`(readWorkbookUseCase.execute(ReadWorkbookUseCaseIn(workbookId))).thenReturn(
ReadWorkbookUseCaseOut(
id = 1L,
mainImageUrl = "/main_img.png",
mainImageUrl = URL("http://localhost:8080/api/v1/workbooks/1/image"),
title = "재태크, 투자 필수 용어 모음집",
description = "사회 초년생부터, 직장인, 은퇴자까지 모두가 알아야 할 기본적인 재태크, 투자 필수 용어 모음집 입니다.",
category = "경제",
Expand Down
23 changes: 0 additions & 23 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -94,32 +94,9 @@ subprojects {
swaggerUI("org.webjars:swagger-ui:${DependencyVersion.SWAGGER_UI}")
}

tasks.getByName("bootJar") {
enabled = false
}

tasks.getByName("jar") {
enabled = true
}

defaultTasks("bootRun")
}

/** Build Docker Image */
val imageName = project.hasProperty("imageName") ?: "api:local"
val releaseVersion = project.findProperty("releaseVersion")

tasks.create("buildDockerImage") {
dependsOn(":bootJar")

doLast {
exec {
workingDir(".")
commandLine("docker", "build", "-t", imageName, "--build-arg", "RELEASE_VERSION=$releaseVersion", ".")
}
}
}

/** git hooks */
tasks.register("gitExecutableHooks") {
doLast {
Expand Down
14 changes: 14 additions & 0 deletions resources/docker/docker-compose-api.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: '3.1'
services:
api:
container_name: api
image: fewletter/api:latest
ports:
- "8080:8080"
environment:
SPRING_PROFILES_ACTIVE: ${API_SPRING_PROFILES_ACTIVE}
DB_HOSTNAME: ${DB_HOSTNAME}
DB_USERNAME: ${DB_USERNAME}
DB_PASSWORD: ${DB_PASSWORD}
EMAIL_USERNAME: ${EMAIL_USERNAME}
EMAIL_PASSWORD: ${EMAIL_PASSWORD}
6 changes: 6 additions & 0 deletions resources/docker/scripts/api-start
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh

cd ..
docker-compose -f docker-compose-api.yml down
docker-compose -f docker-compose-api.yml up -d
sleep 10

0 comments on commit ffd4eb2

Please sign in to comment.