diff --git a/.github/workflows/deploy-admin.yml b/.github/workflows/deploy-admin.yml new file mode 100644 index 00000000..195b6519 --- /dev/null +++ b/.github/workflows/deploy-admin.yml @@ -0,0 +1,82 @@ +name: Dev Admin CI/CD + +on: + push: + branches: + - develop + paths: + - 'application-admin/**' + - 'common/**' + - 'domain-*/**' + - 'application-config/**' + - 'security-admin/**' + - 'build.gradle' + - 'settings.gradle' + - 'gradle/**' + +env: + PROJECT_NAME: NoWait + DIVISION: admin + AWS_REGION: ap-northeast-2 + AWS_S3_BUCKET: nowait-deploy-github-actions + AWS_CODE_DEPLOY_APPLICATION: nowaiting + AWS_CODE_DEPLOY_GROUP: nowaiting-deploy-dev-admin + +jobs: + build-with-gradle: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: JDK 17 설치 + uses: actions/setup-java@v3 + with: + java-version: '17' + distribution: 'zulu' + + - name: gradlew에 실행 권한 부여 + run: chmod +x ./gradlew + + - name: Set YML + run: | + mkdir -p application-admin/src/main/resources + echo "${{ secrets.APPLICATION_DEV_ADMIN_YML }}" | base64 --decode > application-admin/src/main/resources/application-admin.yml + find src + + - name: 프로젝트 빌드 + run: ./gradlew clean :application-admin:bootJar -x test + + - name: Prepare deployment package + run: | + rm -rf deploy && mkdir deploy + cp application-admin/build/libs/application-admin-*.jar deploy/application-admin.jar + cp scripts/start-admin.sh scripts/stop-admin.sh appspec-admin.yml deploy/appspec.yml + cd deploy + zip -r ../${{ env.PROJECT_NAME }}-${{ env.DIVISION }}-${{ github.sha }}.zip . + cd .. + + - name: AWS credential 설정 + uses: aws-actions/configure-aws-credentials@v1 + with: + aws-region: ${{ env.AWS_REGION }} + aws-access-key-id: ${{ secrets.DEVSERVER_CICD_ACCESS_KEY }} + aws-secret-access-key: ${{ secrets.DEVSERVER_CICD_SECRET_KEY }} + + - name: S3에 업로드 + run: aws deploy push \ + --application-name "${{ env.AWS_CODE_DEPLOY_APPLICATION }}" \ + --ignore-hidden-files \ + --s3-location "s3://${{ env.AWS_S3_BUCKET }}/${{ env.PROJECT_NAME }}/${{ env.DIVISION }}/${{ github.sha }}.zip" \ + --source "${{ env.PROJECT_NAME }}-${{ env.DIVISION }}-${{ github.sha }}.zip" + + - name: EC2에 배포 + run: | + aws deploy create-deployment \ + --application-name ${{ env.AWS_CODE_DEPLOY_APPLICATION }} \ + --deployment-config-name CodeDeployDefault.AllAtOnce \ + --deployment-group-name ${{ env.AWS_CODE_DEPLOY_GROUP }} \ + --s3-location bucket=${{ env.AWS_S3_BUCKET }},key=${{ env.PROJECT_NAME }}/${{ env.DIVISION }}/${{ github.sha }}.zip,bundleType=zip + + + diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml deleted file mode 100644 index 57f36953..00000000 --- a/.github/workflows/dev.yml +++ /dev/null @@ -1,57 +0,0 @@ -name: Dev CICD - -on: - push: - branches: - - develop -env: - PROJECT_NAME: NoWait - AWS_REGION: ap-northeast-2 - AWS_S3_BUCKET: nowait-deploy-github-actions - AWS_CODE_DEPLOY_APPLICATION: nowaiting - AWS_CODE_DEPLOY_GROUP: nowaiting-deploy-dev - -jobs: - build-with-gradle: - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v3 - - - name: JDK 17 설치 - uses: actions/setup-java@v3 - with: - java-version: '17' - distribution: 'zulu' - - - name: gradlew에 실행 권한 부여 - run: chmod +x ./gradlew - - - name: Set YML - run: | - mkdir -p src/main/resources - echo "${{ secrets.APPLICATION_DEV_YML }}" | base64 --decode > src/main/resources/application-dev.yml - find src - - - name: 프로젝트 빌드 - run: ./gradlew clean build -x test - - - name: AWS credential 설정 - uses: aws-actions/configure-aws-credentials@v1 - with: - aws-region: ${{ env.AWS_REGION }} - aws-access-key-id: ${{ secrets.DEVSERVER_CICD_ACCESS_KEY }} - aws-secret-access-key: ${{ secrets.DEVSERVER_CICD_SECRET_KEY }} - - - name: S3에 업로드 - run: aws deploy push --application-name ${{ env.AWS_CODE_DEPLOY_APPLICATION }} --ignore-hidden-files --s3-location s3://$AWS_S3_BUCKET/$PROJECT_NAME/$GITHUB_SHA.zip --source . - - - name: EC2에 배포 - run: | - aws deploy create-deployment \ - --application-name ${{ env.AWS_CODE_DEPLOY_APPLICATION }} \ - --deployment-config-name CodeDeployDefault.AllAtOnce \ - --deployment-group-name ${{ env.AWS_CODE_DEPLOY_GROUP }} \ - --s3-location bucket=$AWS_S3_BUCKET,key=$PROJECT_NAME/$GITHUB_SHA.zip,bundleType=zip - - diff --git a/appspec-admin.yml b/appspec-admin.yml new file mode 100644 index 00000000..49636f2b --- /dev/null +++ b/appspec-admin.yml @@ -0,0 +1,20 @@ +version: 0.0 +os: linux + +files: + - source: / + destination: /home/ubuntu/spring-github-action + overwrite: yes + +permissions: + - object: / + owner: ubuntu + group: ubuntu + +hooks: + AfterInstall: + - location: scripts/stop-admin.sh + timeout: 60 + ApplicationStart: + - location: scripts/start-admin.sh + timeout: 60 diff --git a/build.gradle b/build.gradle index 51bc6932..791934ae 100644 --- a/build.gradle +++ b/build.gradle @@ -18,6 +18,20 @@ subprojects { group = rootProject.group version = rootProject.version + // 실행 모듈만 따로 정의 + def applicationModules = ['application-admin', 'application-user'] + + if (applicationModules.contains(project.name)) { + // 실제 Service 실행 모듈 + apply plugin: 'org.springframework.boot' + bootJar { enabled = true } + jar { enabled = false } + } else { + // 라이브러리/설정/도메인 전용 모듈 + bootJar { enabled = false } + jar { enabled = true } + } + repositories { mavenCentral() } @@ -43,11 +57,4 @@ subprojects { test { useJUnitPlatform() } - - bootJar { - enabled = false - } - jar { - enabled = true - } } diff --git a/common/build.gradle b/common/build.gradle index 75d320e1..44e9a75f 100644 --- a/common/build.gradle +++ b/common/build.gradle @@ -1,5 +1,10 @@ -bootJar { enabled = false } -jar { enabled = true } +jar { + enabled = true +} +bootJar { + enabled = false +} + dependencies { } diff --git a/domain-token/src/test/java/com/nowait/token/DomainTokenApplicationTests.java b/domain-token/src/test/java/com/nowait/token/DomainTokenApplicationTests.java deleted file mode 100644 index 99f6cd30..00000000 --- a/domain-token/src/test/java/com/nowait/token/DomainTokenApplicationTests.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.nowait.token; - -import org.junit.jupiter.api.Test; -import org.springframework.boot.test.context.SpringBootTest; - -@SpringBootTest -class DomainTokenApplicationTests { - - @Test - void contextLoads() { - } - -} diff --git a/scripts/start-admin.sh b/scripts/start-admin.sh new file mode 100644 index 00000000..32b3ed6b --- /dev/null +++ b/scripts/start-admin.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +REPOSITORY="/home/ubuntu/spring-github-action" +cd $REPOSITORY + +APP_NAME=application-admin +JAR_NAME=$(ls $REPOSITORY/build/libs/ | grep '.jar' | tail -n 1) +JAR_PATH=$REPOSITORY/build/libs/$JAR_NAME + +START_LOG="$REPOSITORY/start.log" +ERROR_LOG="$REPOSITORY/error.log" +APP_LOG="$REPOSITORY/application.log" + +$ chmod 666 "$START_LOG" +$ chmod 666 "$ERROR_LOG" +$ chmod 666 "$APP_LOG" + +SERVICE_PID=$(pgrep -f ${APP_NAME}.*.jar) # 실행중인 Spring 서버의 PID +NOW=$(date +%c) + + +echo "[$NOW] > $JAR_PATH 실행" >> $START_LOG +nohup java -Xms256m -Xmx512m -XX:+UseG1GC -jar $JAR_PATH --spring.profiles.active=admin > $APP_LOG 2> $ERROR_LOG & + +echo "[$NOW] > 서비스 PID: $SERVICE_PID" >> $START_LOG diff --git a/scripts/start-user.sh b/scripts/start-user.sh new file mode 100644 index 00000000..eb4db17b --- /dev/null +++ b/scripts/start-user.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +REPOSITORY="/home/ubuntu/spring-github-action" +cd $REPOSITORY + +APP_NAME=application-admin +JAR_NAME=$(ls $REPOSITORY/build/libs/ | grep '.jar' | tail -n 1) +JAR_PATH=$REPOSITORY/build/libs/$JAR_NAME + +START_LOG="$REPOSITORY/start.log" +ERROR_LOG="$REPOSITORY/error.log" +APP_LOG="$REPOSITORY/application.log" + +$ chmod 666 $START_LOG +$ chmod 666 ERROR_LOG +$ chmod 666 APP_LOG + +SERVICE_PID=$(pgrep -f ${APP_NAME}.*.jar) # 실행중인 Spring 서버의 PID +NOW=$(date +%c) + + +echo "[$NOW] > $JAR_PATH 실행" >> $START_LOG +nohup java -Xms256m -Xmx512m -XX:+UseG1GC -jar $JAR_PATH --spring.profiles.active=user > $APP_LOG 2> $ERROR_LOG & + +echo "[$NOW] > 서비스 PID: $SERVICE_PID" >> $START_LOG diff --git a/scripts/stop-admin.sh b/scripts/stop-admin.sh new file mode 100644 index 00000000..6d6ad0e4 --- /dev/null +++ b/scripts/stop-admin.sh @@ -0,0 +1,22 @@ +#!/bin/bash + +REPOSITORY=/home/ubuntu/spring-github-action +cd $REPOSITORY + +APP_NAME=application-admin +JAR_NAME=$(ls $REPOSITORY/build/libs/ | grep '.jar' | tail -n 1) +JAR_PATH=$REPOSITORY/build/libs/$JAR_NAME +STOP_LOG="$REPOSITORY/stop.log" + +$ chmod 666 "$STOP_LOG" + +CURRENT_PID=$(pgrep -f ${APP_NAME}.*.jar) + +if [ -z $CURRENT_PID ] +then + echo "서비스 NouFound" >> $STOP_LOG +else + echo "> kill -15 $CURRENT_PID" + kill -15 $CURRENT_PID + sleep 5 +fi diff --git a/scripts/stop-user.sh b/scripts/stop-user.sh new file mode 100644 index 00000000..868f77b9 --- /dev/null +++ b/scripts/stop-user.sh @@ -0,0 +1,22 @@ +#!/bin/bash + +REPOSITORY=/home/ubuntu/spring-github-action +cd $REPOSITORY + +APP_NAME=application-admin +JAR_NAME=$(ls $REPOSITORY/build/libs/ | grep '.jar' | tail -n 1) +JAR_PATH=$REPOSITORY/build/libs/$JAR_NAME +STOP_LOG="$REPOSITORY/stop.log" + +$ chmod 666 STOP_LOG + +CURRENT_PID=$(pgrep -f ${APP_NAME}.*.jar) + +if [ -z $CURRENT_PID ] +then + echo "서비스 NouFound" >> $STOP_LOG +else + echo "> kill -15 $CURRENT_PID" + kill -15 $CURRENT_PID + sleep 5 +fi