From 814825ccd9da7e5a592d34dfefc3dd673e347dd1 Mon Sep 17 00:00:00 2001 From: Jimin Lim <50178026+jimin3263@users.noreply.github.com> Date: Mon, 12 Aug 2024 10:32:32 +0900 Subject: [PATCH] =?UTF-8?q?[feat=20#58]=20=EB=B0=B0=EC=B9=98=EC=84=9C?= =?UTF-8?q?=EB=B2=84=20=EC=84=B8=ED=8C=85=20(#83)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * chore: 배치서버 세팅 * chore: 배치 ci/cd 설정 * chore: 배치 ci/cd 설정 * chore: 배치 ci/cd 설정 * chore: 배치 ci/cd 설정 * chore: Dockerfile 포트 수정 * chore: batch 서버 정보 수정 * chore: batch 서버 정보 수정 * chore: 수정사항 있을때 배포되도록 옵션 추가 --- .github/workflows/batch-deploy.yml | 93 +++++++++++++++++++ adapters/in-batch/build.gradle.kts | 27 ++++++ entry/batch/Dockerfile | 8 ++ entry/batch/build.gradle.kts | 23 +++++ .../com/pokit/batch/BatchApplication.kt | 14 +++ settings.gradle.kts | 2 + 6 files changed, 167 insertions(+) create mode 100644 .github/workflows/batch-deploy.yml create mode 100644 adapters/in-batch/build.gradle.kts create mode 100644 entry/batch/Dockerfile create mode 100644 entry/batch/build.gradle.kts create mode 100644 entry/batch/src/main/kotlin/com/pokit/batch/BatchApplication.kt diff --git a/.github/workflows/batch-deploy.yml b/.github/workflows/batch-deploy.yml new file mode 100644 index 0000000..4476fb5 --- /dev/null +++ b/.github/workflows/batch-deploy.yml @@ -0,0 +1,93 @@ +name: batch deploy + +on: + push: + branches: + - feature/#58 + - main + - develop + paths: + - 'adapters/in-batch/**' + +env: + DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} + DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }} + DOCKERHUB_BATCH_IMAGE: ${{ secrets.DOCKERHUB_BATCH_IMAGE }} + +jobs: + build: + runs-on: ubuntu-latest + + strategy: + matrix: + kotlin-version: [ "1.9.23" ] + java-version: [ "17" ] + + steps: + - name: Check Out The Repository + uses: actions/checkout@v3 + + - name: Set up Kotlin + uses: actions/setup-java@v3 + with: + java-version: ${{ matrix.java-version }} + kotlin-version: ${{ matrix.kotlin-version }} + distribution: 'corretto' + + - name: Grant execute permission for gradlew + run: chmod +x ./gradlew + + - name: Create config file + run: | + mkdir -p ./adapters/out-persistence/src/main/resources + echo "${{ secrets.OUT_PERSISTENCE_YML }}" > ./adapters/out-persistence/src/main/resources/application-out-persistence.yml + mkdir -p ./adapters/out-web/src/main/resources + echo "${{ secrets.OUT_WEB_GOOGLE }}" | base64 --decode > ./adapters/out-web/src/main/resources/google-services.json + echo "${{ secrets.OUT_WEB_YML }}" > ./adapters/out-web/src/main/resources/application-out-web.yml + mkdir -p ./application/src/main/resources + echo "${{ secrets.APPLICATION_CORE }}" > ./application/src/main/resources/application-core.yml + + - name: Build with Gradle + run: ./gradlew :entry:batch:build --no-daemon + + - name: Docker build and push + run: | + docker login -u $DOCKERHUB_USERNAME -p $DOCKERHUB_TOKEN + docker build -f entry/batch/Dockerfile -t $DOCKERHUB_USERNAME/$DOCKERHUB_BATCH_IMAGE:latest . + docker push $DOCKERHUB_USERNAME/$DOCKERHUB_BATCH_IMAGE:latest + + - name: Get Public IP + id: publicip + run: | + response=$(curl -s canhazip.com) + echo "ip='$response'" >> $GITHUB_OUTPUT + + - name: Configure AWS Credentials + uses: aws-actions/configure-aws-credentials@v1 + with: + aws-access-key-id: ${{ secrets.AWS_BATCH_ACCESS_KEY_ID }} + aws-secret-access-key: ${{ secrets.AWS_BATCH_SECRET_ACCESS_KEY }} + aws-region: ${{ secrets.AWS_REGION }} + + - name: Add GitHub IP to AWS + run: | + aws ec2 authorize-security-group-ingress --group-id ${{ secrets.AWS_BATCH_SG_ID }} --protocol tcp --port ${{ secrets.PORT }} --cidr ${{ steps.publicip.outputs.ip }}/32 + + - name: executing remote ssh commands using ssh key + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.BATCH_HOST }} + username: ${{ secrets.BATCH_USERNAME }} + key: ${{ secrets.BATCH_KEY }} + port: ${{ secrets.PORT }} + script: | + cd pokit-batch + + sudo docker-compose pull + sudo docker-compose down + sudo docker-compose up --force-recreate --remove-orphans -d + sudo docker image prune -f + + - name: Remove IP FROM security group + run: | + aws ec2 revoke-security-group-ingress --group-id ${{ secrets.AWS_BATCH_SG_ID }} --protocol tcp --port ${{ secrets.PORT }} --cidr ${{ steps.publicip.outputs.ip }}/32 diff --git a/adapters/in-batch/build.gradle.kts b/adapters/in-batch/build.gradle.kts new file mode 100644 index 0000000..a2069bc --- /dev/null +++ b/adapters/in-batch/build.gradle.kts @@ -0,0 +1,27 @@ +import org.springframework.boot.gradle.tasks.bundling.BootJar + +plugins { + id("org.springframework.boot") version "3.3.0" + id("io.spring.dependency-management") version "1.1.5" + kotlin("plugin.spring") version "1.9.24" +} + +dependencies { + // 모듈 + implementation(project(":domain")) + implementation(project(":application")) + + // 라이브러리 + implementation("org.springframework.boot:spring-boot-starter-web") + implementation("org.springframework.boot:spring-boot-starter-actuator") + implementation("org.springframework.boot:spring-boot-starter-batch") + + // 테스팅 + testImplementation("io.kotest.extensions:kotest-extensions-spring:1.1.1") + testImplementation("org.springframework.boot:spring-boot-starter-test") +} + +tasks { + withType { enabled = true } + withType { enabled = false } +} diff --git a/entry/batch/Dockerfile b/entry/batch/Dockerfile new file mode 100644 index 0000000..08214f5 --- /dev/null +++ b/entry/batch/Dockerfile @@ -0,0 +1,8 @@ +FROM amazoncorretto:17 + +ARG JAR_FILE=entry/batch/build/libs/*.jar +COPY ${JAR_FILE} app.jar + +ENV SERVER_PORT=8081 + +ENTRYPOINT ["java", "-Dserver.port=${SERVER_PORT}", "-jar", "/app.jar"] diff --git a/entry/batch/build.gradle.kts b/entry/batch/build.gradle.kts new file mode 100644 index 0000000..044f399 --- /dev/null +++ b/entry/batch/build.gradle.kts @@ -0,0 +1,23 @@ +import org.springframework.boot.gradle.tasks.bundling.BootJar + +plugins { + id("org.springframework.boot") version "3.3.0" + id("io.spring.dependency-management") version "1.1.5" + kotlin("plugin.spring") version "1.9.24" +} + +dependencies { + // 모듈 + implementation(project(":adapters:in-batch")) + implementation(project(":adapters:out-persistence")) + implementation(project(":adapters:out-web")) + implementation(project(":domain")) + + // 라이브러리 + implementation("org.springframework.boot:spring-boot-starter-web") +} + +tasks { + withType { enabled = false } + withType { enabled = true } +} diff --git a/entry/batch/src/main/kotlin/com/pokit/batch/BatchApplication.kt b/entry/batch/src/main/kotlin/com/pokit/batch/BatchApplication.kt new file mode 100644 index 0000000..20a3372 --- /dev/null +++ b/entry/batch/src/main/kotlin/com/pokit/batch/BatchApplication.kt @@ -0,0 +1,14 @@ +package com.pokit.batch + +import org.springframework.boot.autoconfigure.SpringBootApplication +import org.springframework.boot.context.properties.ConfigurationPropertiesScan +import org.springframework.boot.runApplication + +@SpringBootApplication +@ConfigurationPropertiesScan +class BatchApplication + +fun main(args: Array) { + System.setProperty("spring.config.name", "application-out-web, application-core, application-out-persistence") + runApplication(*args) +} diff --git a/settings.gradle.kts b/settings.gradle.kts index ae386ed..725546d 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -3,8 +3,10 @@ include("domain") include("application") include("entry") include("entry:web") +include("entry:batch") include("adapters") include("adapters:in-web") +include("adapters:in-batch") include("adapters:out-persistence") include("adapters:out-cache") include("adapters:out-web")