From 65bdcb0b8f2316a55ec3e5b65fde6654144ed5fe Mon Sep 17 00:00:00 2001 From: JUNGHO KANG Date: Sun, 14 Jan 2024 17:14:16 +0900 Subject: [PATCH 1/8] cicd test --- .github/workflows/deploy.yaml | 70 +++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 .github/workflows/deploy.yaml diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml new file mode 100644 index 0000000..0c9b619 --- /dev/null +++ b/.github/workflows/deploy.yaml @@ -0,0 +1,70 @@ +name: CI-CD + +on: + push: + branches: + - main + - feat/cicd + +env: + S3_BUCKET_NAME: mara-s3bucket + RESOURCE_PATH: ./src/main/resources/application.yaml + CODE_DEPLOY_APPLICATION_NAME: mara-code-deploy + CODE_DEPLOY_DEPLOYMENT_GROUP_NAME: mara-deploy-group + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Set up JDK 11 + uses: actions/setup-java@v1 + with: + java-version: 11 + + # [1] + - name: Set yaml file + uses: microsoft/variable-substitution@v1 + with: + files: ${{ env.RESOURCE_PATH }} + env: +# override.value: ${{ secrets.DI_FROM_SECRET }} + override.value: 'from deploy.yaml' + + - name: Grant execute permission for gradlew + run: chmod +x ./gradlew + shell: bash + + # [2] + - name: Build with Gradle + run: ./gradlew build + shell: bash + + # [3] + - name: Make zip file + run: zip -r ./$GITHUB_SHA.zip . + shell: bash + + # [4] + - 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 }} + aws-region: ${{ secrets.AWS_REGION }} + + # [5] + - name: Upload to S3 + run: aws s3 cp --region ap-northeast-2 ./$GITHUB_SHA.zip s3://$S3_BUCKET_NAME/$GITHUB_SHA.zip + + # [6] + - name: Code Deploy + run: | + aws deploy create-deployment \ + --deployment-config-name CodeDeployDefault.AllAtOnce \ + --application-name ${{ env.CODE_DEPLOY_APPLICATION_NAME }} \ + --deployment-group-name ${{ env.CODE_DEPLOY_DEPLOYMENT_GROUP_NAME }} \ + --s3-location bucket=$S3_BUCKET_NAME,bundleType=zip,key=$GITHUB_SHA.zip \ No newline at end of file From 899aa5fbea5b509ff8a35cdc2d0ab4afcac85768 Mon Sep 17 00:00:00 2001 From: JUNGHO KANG Date: Sun, 14 Jan 2024 17:17:28 +0900 Subject: [PATCH 2/8] =?UTF-8?q?yaml=20=ED=99=95=EC=9E=A5=EC=9E=90=20yml=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: JUNGHO KANG --- .github/workflows/deploy.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml index 0c9b619..6e4f60c 100644 --- a/.github/workflows/deploy.yaml +++ b/.github/workflows/deploy.yaml @@ -8,7 +8,7 @@ on: env: S3_BUCKET_NAME: mara-s3bucket - RESOURCE_PATH: ./src/main/resources/application.yaml + RESOURCE_PATH: ./src/main/resources/application.yml CODE_DEPLOY_APPLICATION_NAME: mara-code-deploy CODE_DEPLOY_DEPLOYMENT_GROUP_NAME: mara-deploy-group @@ -20,10 +20,10 @@ jobs: - name: Checkout uses: actions/checkout@v2 - - name: Set up JDK 11 + - name: Set up JDK 17 uses: actions/setup-java@v1 with: - java-version: 11 + java-version: 17 # [1] - name: Set yaml file From 81c8a2fd8e2c118118e2f73d7da61c5a1cc69069 Mon Sep 17 00:00:00 2001 From: JUNGHO KANG Date: Sun, 14 Jan 2024 17:19:43 +0900 Subject: [PATCH 3/8] =?UTF-8?q?yaml=20=EC=9E=84=EC=8B=9C=20=EC=A3=BC?= =?UTF-8?q?=EC=84=9D=20=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/deploy.yaml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml index 6e4f60c..d6567ca 100644 --- a/.github/workflows/deploy.yaml +++ b/.github/workflows/deploy.yaml @@ -26,13 +26,13 @@ jobs: java-version: 17 # [1] - - name: Set yaml file - uses: microsoft/variable-substitution@v1 - with: - files: ${{ env.RESOURCE_PATH }} - env: -# override.value: ${{ secrets.DI_FROM_SECRET }} - override.value: 'from deploy.yaml' +# - name: Set yaml file +# uses: microsoft/variable-substitution@v1 +# with: +# files: ${{ env.RESOURCE_PATH }} +# env: +## override.value: ${{ secrets.DI_FROM_SECRET }} +# override.value: 'from deploy.yaml' - name: Grant execute permission for gradlew run: chmod +x ./gradlew From 34c4e8926b7ac83beb1787176e651676b1436f46 Mon Sep 17 00:00:00 2001 From: JUNGHO KANG Date: Sun, 14 Jan 2024 17:26:36 +0900 Subject: [PATCH 4/8] =?UTF-8?q?ktlint=20=EC=8A=A4=ED=83=80=EC=9D=BC=20?= =?UTF-8?q?=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: JUNGHO KANG --- src/main/kotlin/mara/server/domain/board/BoardDto.kt | 2 +- src/main/kotlin/mara/server/domain/user/UserController.kt | 6 +++--- src/main/kotlin/mara/server/domain/user/UserDto.kt | 4 ++-- src/main/kotlin/mara/server/domain/user/UserService.kt | 6 +++--- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main/kotlin/mara/server/domain/board/BoardDto.kt b/src/main/kotlin/mara/server/domain/board/BoardDto.kt index c572b40..ea4c60f 100644 --- a/src/main/kotlin/mara/server/domain/board/BoardDto.kt +++ b/src/main/kotlin/mara/server/domain/board/BoardDto.kt @@ -2,7 +2,7 @@ package mara.server.domain.board data class BoardRequest( var title: String, - val userId:Long, + val userId: Long, ) data class BoardResponse( diff --git a/src/main/kotlin/mara/server/domain/user/UserController.kt b/src/main/kotlin/mara/server/domain/user/UserController.kt index e827f53..b4a5c1e 100644 --- a/src/main/kotlin/mara/server/domain/user/UserController.kt +++ b/src/main/kotlin/mara/server/domain/user/UserController.kt @@ -9,10 +9,10 @@ import org.springframework.web.bind.annotation.RestController @RestController @RequestMapping("/users") -class UserController(private val userService:UserService) { +class UserController(private val userService: UserService) { @PostMapping - fun createUser(@RequestBody userRequest: UserRequest):CommonResponse{ + fun createUser(@RequestBody userRequest: UserRequest): CommonResponse { return success(userService.createUser(userRequest)) } -} \ No newline at end of file +} diff --git a/src/main/kotlin/mara/server/domain/user/UserDto.kt b/src/main/kotlin/mara/server/domain/user/UserDto.kt index eab399c..2a4159b 100644 --- a/src/main/kotlin/mara/server/domain/user/UserDto.kt +++ b/src/main/kotlin/mara/server/domain/user/UserDto.kt @@ -1,5 +1,5 @@ package mara.server.domain.user data class UserRequest( - val name:String -) \ No newline at end of file + val name: String +) diff --git a/src/main/kotlin/mara/server/domain/user/UserService.kt b/src/main/kotlin/mara/server/domain/user/UserService.kt index a1130ec..284e89e 100644 --- a/src/main/kotlin/mara/server/domain/user/UserService.kt +++ b/src/main/kotlin/mara/server/domain/user/UserService.kt @@ -5,10 +5,10 @@ import org.springframework.stereotype.Service @Service class UserService(private val userRepository: UserRepository) { - fun createUser(userRequest:UserRequest):Long{ - val user=User( + fun createUser(userRequest: UserRequest): Long { + val user = User( name = userRequest.name ) return userRepository.save(user).userId } -} \ No newline at end of file +} From 7f160918909e58e0a667a951fc4a7eb2eaca8f3d Mon Sep 17 00:00:00 2001 From: JUNGHO KANG Date: Sun, 14 Jan 2024 17:32:37 +0900 Subject: [PATCH 5/8] =?UTF-8?q?UserDto=20->=20UserRequest=20=ED=8C=8C?= =?UTF-8?q?=EC=9D=BC=EB=AA=85=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/mara/server/domain/user/{UserDto.kt => UserRequest.kt} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/main/kotlin/mara/server/domain/user/{UserDto.kt => UserRequest.kt} (100%) diff --git a/src/main/kotlin/mara/server/domain/user/UserDto.kt b/src/main/kotlin/mara/server/domain/user/UserRequest.kt similarity index 100% rename from src/main/kotlin/mara/server/domain/user/UserDto.kt rename to src/main/kotlin/mara/server/domain/user/UserRequest.kt From ad24788712f6fc941276d2a11959ceff1ba79636 Mon Sep 17 00:00:00 2001 From: JUNGHO KANG Date: Sun, 14 Jan 2024 17:48:25 +0900 Subject: [PATCH 6/8] =?UTF-8?q?appspec.yml=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- appspec.yml | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 appspec.yml diff --git a/appspec.yml b/appspec.yml new file mode 100644 index 0000000..4eed985 --- /dev/null +++ b/appspec.yml @@ -0,0 +1,18 @@ +version: 0.0 +os: linux +files: + - source: / + destination: /home/ubuntu/github_action + overwrite: yes + +permissions: + - object: / + pattern: "**" + owner: ubuntu + group: ubuntu + +hooks: + ApplicationStart: + - location: scripts/gh_deploy.sh + timeout: 60 + runas: ubuntu \ No newline at end of file From 70bba4074987bec8c7a3eca6aede55408bc5f130 Mon Sep 17 00:00:00 2001 From: JUNGHO KANG Date: Sun, 14 Jan 2024 18:26:35 +0900 Subject: [PATCH 7/8] =?UTF-8?q?=EB=B0=B0=ED=8F=AC=20=EC=8A=A4=ED=81=AC?= =?UTF-8?q?=EB=A6=BD=ED=8A=B8=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/gh_deploy.sh | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 scripts/gh_deploy.sh diff --git a/scripts/gh_deploy.sh b/scripts/gh_deploy.sh new file mode 100644 index 0000000..b085f0b --- /dev/null +++ b/scripts/gh_deploy.sh @@ -0,0 +1,36 @@ +#!/bin/bash +PROJECT_NAME="github_action" +JAR_PATH="/home/ubuntu/github_action/build/libs/*.jar" +DEPLOY_PATH=/home/ubuntu/$PROJECT_NAME/ +DEPLOY_LOG_PATH="/home/ubuntu/$PROJECT_NAME/deploy.log" +DEPLOY_ERR_LOG_PATH="/home/ubuntu/$PROJECT_NAME/deploy_err.log" +APPLICATION_LOG_PATH="/home/ubuntu/$PROJECT_NAME/application.log" +BUILD_JAR=$(ls $JAR_PATH) +JAR_NAME=$(basename $BUILD_JAR) + +echo "===== 배포 시작 : $(date +%c) =====" >> $DEPLOY_LOG_PATH + +echo "> build 파일명: $JAR_NAME" >> $DEPLOY_LOG_PATH +echo "> build 파일 복사" >> $DEPLOY_LOG_PATH +cp $BUILD_JAR $DEPLOY_PATH + +echo "> 현재 동작중인 어플리케이션 pid 체크" >> $DEPLOY_LOG_PATH +CURRENT_PID=$(pgrep -f $JAR_NAME) + +if [ -z $CURRENT_PID ] +then + echo "> 현재 동작중인 어플리케이션 존재 X" >> $DEPLOY_LOG_PATH +else + echo "> 현재 동작중인 어플리케이션 존재 O" >> $DEPLOY_LOG_PATH + echo "> 현재 동작중인 어플리케이션 강제 종료 진행" >> $DEPLOY_LOG_PATH + echo "> kill -9 $CURRENT_PID" >> $DEPLOY_LOG_PATH + kill -9 $CURRENT_PID +fi + +DEPLOY_JAR=$DEPLOY_PATH$JAR_NAME +echo "> DEPLOY_JAR 배포" >> $DEPLOY_LOG_PATH +nohup java -jar -Dspring.profiles.active=local $DEPLOY_JAR --server.port=8081 >> $APPLICATION_LOG_PATH 2> $DEPLOY_ERR_LOG_PATH & + +sleep 3 + +echo "> 배포 종료 : $(date +%c)" >> $DEPLOY_LOG_PATH \ No newline at end of file From 6354f9971aabd98afd4a7b000a34ee13d8ddadd8 Mon Sep 17 00:00:00 2001 From: JUNGHO KANG Date: Sun, 14 Jan 2024 22:37:42 +0900 Subject: [PATCH 8/8] =?UTF-8?q?on.push.branches=20feat/cicd=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/deploy.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml index d6567ca..fb932a8 100644 --- a/.github/workflows/deploy.yaml +++ b/.github/workflows/deploy.yaml @@ -4,7 +4,6 @@ on: push: branches: - main - - feat/cicd env: S3_BUCKET_NAME: mara-s3bucket