From 37dae04b0ee7a3fc3314f5791a4b80fb26c1ac7d Mon Sep 17 00:00:00 2001 From: dragontaek-lee Date: Sat, 11 May 2024 01:06:28 +0900 Subject: [PATCH 1/7] =?UTF-8?q?fix:=20codeDeploy=20script=20=EC=9E=84?= =?UTF-8?q?=EC=8B=9C=20=EC=A0=9C=EA=B1=B0,=20cd-prod=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/{CD.yml => cd-develop.yml} | 0 .github/workflows/cd-prod.yml | 93 ++++++++++++++++++++ scripts/deploy.sh | 72 +++++++-------- scripts/switch.sh | 68 +++++++------- 4 files changed, 163 insertions(+), 70 deletions(-) rename .github/workflows/{CD.yml => cd-develop.yml} (100%) create mode 100644 .github/workflows/cd-prod.yml diff --git a/.github/workflows/CD.yml b/.github/workflows/cd-develop.yml similarity index 100% rename from .github/workflows/CD.yml rename to .github/workflows/cd-develop.yml diff --git a/.github/workflows/cd-prod.yml b/.github/workflows/cd-prod.yml new file mode 100644 index 00000000..1a37efb3 --- /dev/null +++ b/.github/workflows/cd-prod.yml @@ -0,0 +1,93 @@ +# 워크플로우의 이름 지정 +name: Gam-prod-server CD + +# 해당 workflow가 언제 실행될 것인지에 대한 트리거를 지정 +on: + push: + branches: [ main ] + +jobs: + build: + name: Code deployment + + # 실행 환경 + runs-on: ubuntu-latest + + steps: + + # 1) 워크플로우 실행 전 기본적으로 체크아웃 필요 + - name: checkout + uses: actions/checkout@v3 + + # 2) JDK 11버전 설치, 다른 JDK 버전을 사용하다면 수정 + - name: Set up JDK 17 + uses: actions/setup-java@v3 + with: + java-version: '17' + distribution: 'temurin' + + # 3) 환경변수 파일 생성 + - name: make application.properties 파일 생성 + run: | + ## create application.yml + cd ./src/main/resources + + # application.yml 파일 생성 + touch ./application-prod.yml + + # GitHub-Actions 에서 설정한 값을 application.yml 파일에 쓰기 + echo "${{ secrets.GAM_PROD_APPLICATION }}" >> ./application-prod.yml + + # application.yml 파일 확인 + cat ./application-prod.yml + shell: bash + + # 이 워크플로우는 gradle build + - name: Grant execute permission for gradlew + run: chmod +x gradlew + + - name: Build with Gradle + run: ./gradlew build -x test + + # 디렉토리 생성 + - name: Make Directory + run: mkdir -p deploy + + # Jar 파일 복사 + - name: Copy Jar + run: cp ./build/libs/*.jar ./deploy + + # appspec.yml 파일 복사 + - name: Copy appspec.yml + run: cp appspec.yml ./deploy + + # script files 복사 + - name: Copy script + run: cp ./scripts/*.sh ./deploy + + - name: Make zip file + run: zip -r ./gam-prod-server.zip ./deploy + shell: bash + + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v1 + with: + aws-access-key-id: ${{ secrets.AWS_CONFIG_ACCESS_KEY }} + aws-secret-access-key: ${{ secrets.AWS_CONFIG_SECRET_KEY }} + aws-region: ap-northeast-2 + + - name: Upload to S3 + run: aws s3 cp --region ap-northeast-2 ./gam-prod-server.zip s3://${{ secrets.AWS_S3_BUCKET_NAME }}/prod + + # Deploy + - name: Deploy + env: + AWS_ACCESS_KEY_ID: ${{ secrets.AWS_CONFIG_ACCESS_KEY }} + AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_CONFIG_SECRET_KEY }} + run: + aws deploy create-deployment + --application-name gam-codeDeploy + --deployment-group-name prod + --file-exists-behavior OVERWRITE + --s3-location bucket=$S3_BUCKET_NAME/prod,bundleType=zip,key=gam-prod-server.zip + --region ap-northeast-2 diff --git a/scripts/deploy.sh b/scripts/deploy.sh index e8f65838..46e9e0e0 100644 --- a/scripts/deploy.sh +++ b/scripts/deploy.sh @@ -1,36 +1,36 @@ -#!/bin/bash -REPOSITORY=/home/ubuntu/app -BUILD_PATH=$(ls /home/ubuntu/app/*SNAPSHOT.jar) -JAR_NAME=$(basename $BUILD_PATH) -echo "> build 파일명: $JAR_NAME" -IDLE_APPLICATION_PATH=/home/ubuntu/app/${JAR_NAME} -CURRENT_PORT=$(cat /etc/nginx/conf.d/service-url.inc | grep -Po '[0-9]+' | tail -1) -TARGET_PORT=0 - -echo "> Current port of running WAS is ${CURRENT_PORT}." - -if [ ${CURRENT_PORT} -eq 8081 ]; then - TARGET_PORT=8082 -elif [ ${CURRENT_PORT} -eq 8082 ]; then - TARGET_PORT=8081 -else - echo "> No WAS is connected to nginx" -fi -echo "> Target port is ${TARGET_PORT}." - -TARGET_PID=$(lsof -Fp -i TCP:${TARGET_PORT} | grep -Po 'p[0-9]+' | grep -Po '[0-9]+') -echo "> Target PID is ${TARGET_PID}." - -if [ ! -z ${TARGET_PID} ]; then - echo "> Kill WAS running at ${TARGET_PORT}." - sudo kill ${TARGET_PID} -fi - -nohup java -jar -Duser.timezone=Asia/Seoul -Dserver.port=${TARGET_PORT} -Dspring.profiles.active=dev $IDLE_APPLICATION_PATH >> /home/ubuntu/app/nohup.out 2>&1 & -echo "> Running port is ${TARGET_PORT}." - -echo "> Running PID is" -lsof -i TCP:${TARGET_PORT} - - -echo "> Now new WAS runs at ${TARGET_PORT}." +##!/bin/bash +#REPOSITORY=/home/ubuntu/app +#BUILD_PATH=$(ls /home/ubuntu/app/*SNAPSHOT.jar) +#JAR_NAME=$(basename $BUILD_PATH) +#echo "> build 파일명: $JAR_NAME" +#IDLE_APPLICATION_PATH=/home/ubuntu/app/${JAR_NAME} +#CURRENT_PORT=$(cat /etc/nginx/conf.d/service-url.inc | grep -Po '[0-9]+' | tail -1) +#TARGET_PORT=0 +# +#echo "> Current port of running WAS is ${CURRENT_PORT}." +# +#if [ ${CURRENT_PORT} -eq 8081 ]; then +# TARGET_PORT=8082 +#elif [ ${CURRENT_PORT} -eq 8082 ]; then +# TARGET_PORT=8081 +#else +# echo "> No WAS is connected to nginx" +#fi +#echo "> Target port is ${TARGET_PORT}." +# +#TARGET_PID=$(lsof -Fp -i TCP:${TARGET_PORT} | grep -Po 'p[0-9]+' | grep -Po '[0-9]+') +#echo "> Target PID is ${TARGET_PID}." +# +#if [ ! -z ${TARGET_PID} ]; then +# echo "> Kill WAS running at ${TARGET_PORT}." +# sudo kill ${TARGET_PID} +#fi +# +#nohup java -jar -Duser.timezone=Asia/Seoul -Dserver.port=${TARGET_PORT} -Dspring.profiles.active=dev $IDLE_APPLICATION_PATH >> /home/ubuntu/app/nohup.out 2>&1 & +#echo "> Running port is ${TARGET_PORT}." +# +#echo "> Running PID is" +#lsof -i TCP:${TARGET_PORT} +# +# +#echo "> Now new WAS runs at ${TARGET_PORT}." diff --git a/scripts/switch.sh b/scripts/switch.sh index 1fe48ebd..d2aeacda 100644 --- a/scripts/switch.sh +++ b/scripts/switch.sh @@ -1,34 +1,34 @@ -#!/bin/bash - -echo "> lsof -i TCP:8080-8085" -lsof -i TCP:8080-8085 - -CURRENT_PORT=$(cat /etc/nginx/conf.d/service-url.inc | grep -Po '[0-9]+' | tail -1) -TARGET_PORT=0 - -echo "> Nginx currently proxies to ${CURRENT_PORT}." - -if [ ${CURRENT_PORT} -eq 8081 ]; then - TARGET_PORT=8082 -elif [ ${CURRENT_PORT} -eq 8082 ]; then - TARGET_PORT=8081 -else - echo "> No WAS is connected to nginx" - exit 1 -fi - -echo "set \$service_url http://127.0.0.1:${TARGET_PORT};" |sudo tee /etc/nginx/conf.d/service-url.inc - -echo "> Now Nginx proxies to ${TARGET_PORT}." - -sudo service nginx reload - -echo "> Nginx reloaded." - -CURRENT_PID=$(lsof -Fp -i TCP:${CURRENT_PORT} | grep -Po 'p[0-9]+' | grep -Po '[0-9]+') -echo "> kill ${CURRENT_PID}" - -sudo kill ${CURRENT_PID} - -echo "> lsof -i TCP:8080-8085" -lsof -i TCP:8080-8085 +##!/bin/bash +# +#echo "> lsof -i TCP:8080-8085" +#lsof -i TCP:8080-8085 +# +#CURRENT_PORT=$(cat /etc/nginx/conf.d/service-url.inc | grep -Po '[0-9]+' | tail -1) +#TARGET_PORT=0 +# +#echo "> Nginx currently proxies to ${CURRENT_PORT}." +# +#if [ ${CURRENT_PORT} -eq 8081 ]; then +# TARGET_PORT=8082 +#elif [ ${CURRENT_PORT} -eq 8082 ]; then +# TARGET_PORT=8081 +#else +# echo "> No WAS is connected to nginx" +# exit 1 +#fi +# +#echo "set \$service_url http://127.0.0.1:${TARGET_PORT};" |sudo tee /etc/nginx/conf.d/service-url.inc +# +#echo "> Now Nginx proxies to ${TARGET_PORT}." +# +#sudo service nginx reload +# +#echo "> Nginx reloaded." +# +#CURRENT_PID=$(lsof -Fp -i TCP:${CURRENT_PORT} | grep -Po 'p[0-9]+' | grep -Po '[0-9]+') +#echo "> kill ${CURRENT_PID}" +# +#sudo kill ${CURRENT_PID} +# +#echo "> lsof -i TCP:8080-8085" +#lsof -i TCP:8080-8085 From 2409d141ac93f32e66df5ee2fc09a8d60f22fb0e Mon Sep 17 00:00:00 2001 From: dragontaek-lee Date: Sat, 11 May 2024 01:13:02 +0900 Subject: [PATCH 2/7] =?UTF-8?q?fix:=20=EB=B2=84=ED=82=B7=20=EB=94=94?= =?UTF-8?q?=EB=A0=89=ED=86=A0=EB=A6=AC=20=EC=98=A4=EB=A5=98=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/cd-prod.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/cd-prod.yml b/.github/workflows/cd-prod.yml index 1a37efb3..3020691b 100644 --- a/.github/workflows/cd-prod.yml +++ b/.github/workflows/cd-prod.yml @@ -77,7 +77,7 @@ jobs: aws-region: ap-northeast-2 - name: Upload to S3 - run: aws s3 cp --region ap-northeast-2 ./gam-prod-server.zip s3://${{ secrets.AWS_S3_BUCKET_NAME }}/prod + run: aws s3 cp --region ap-northeast-2 ./gam-prod-server.zip s3://${{ secrets.AWS_S3_BUCKET_NAME }}/prod/ # Deploy - name: Deploy @@ -89,5 +89,5 @@ jobs: --application-name gam-codeDeploy --deployment-group-name prod --file-exists-behavior OVERWRITE - --s3-location bucket=$S3_BUCKET_NAME/prod,bundleType=zip,key=gam-prod-server.zip + --s3-location bucket=${{ secrets.AWS_S3_BUCKET_NAME }},bundleType=zip,key=gam-prod-server.zip --region ap-northeast-2 From 4564cc1167f352f7c2b04185454fe0932e66b299 Mon Sep 17 00:00:00 2001 From: dragontaek-lee Date: Sat, 11 May 2024 02:01:34 +0900 Subject: [PATCH 3/7] =?UTF-8?q?fix:=20=EB=B9=8C=EB=93=9C=20=ED=8C=8C?= =?UTF-8?q?=EC=9D=BC=EB=AA=85=20=EB=82=9C=EB=8F=85=ED=99=94,=20key?= =?UTF-8?q?=EA=B0=92=20=EC=A7=80=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/cd-prod.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/cd-prod.yml b/.github/workflows/cd-prod.yml index 3020691b..81f9fe7c 100644 --- a/.github/workflows/cd-prod.yml +++ b/.github/workflows/cd-prod.yml @@ -66,7 +66,7 @@ jobs: run: cp ./scripts/*.sh ./deploy - name: Make zip file - run: zip -r ./gam-prod-server.zip ./deploy + run: zip -r ./$GITHUB_SHA.zip ./deploy shell: bash - name: Configure AWS credentials @@ -77,7 +77,7 @@ jobs: aws-region: ap-northeast-2 - name: Upload to S3 - run: aws s3 cp --region ap-northeast-2 ./gam-prod-server.zip s3://${{ secrets.AWS_S3_BUCKET_NAME }}/prod/ + run: aws s3 cp --region ap-northeast-2 ./$GITHUB_SHA.zip s3://${{ secrets.AWS_S3_BUCKET_NAME }}/prod/ # Deploy - name: Deploy @@ -89,5 +89,5 @@ jobs: --application-name gam-codeDeploy --deployment-group-name prod --file-exists-behavior OVERWRITE - --s3-location bucket=${{ secrets.AWS_S3_BUCKET_NAME }},bundleType=zip,key=gam-prod-server.zip + --s3-location bucket=${{ secrets.AWS_S3_BUCKET_NAME }},bundleType=zip,key=/prod/$GITHUB_SHA.zip --region ap-northeast-2 From 0fce48b5cd79a8ba8f91ffc9e8a9adb356585439 Mon Sep 17 00:00:00 2001 From: dragontaek-lee Date: Sat, 11 May 2024 02:10:08 +0900 Subject: [PATCH 4/7] =?UTF-8?q?fix:=20s3=20=EC=97=85=EB=A1=9C=EB=93=9C=20?= =?UTF-8?q?=EC=8B=9C=20zip=20=ED=8C=8C=EC=9D=BC=EB=AA=85=20=EB=AA=85?= =?UTF-8?q?=EC=8B=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/cd-prod.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/cd-prod.yml b/.github/workflows/cd-prod.yml index 81f9fe7c..390bd3af 100644 --- a/.github/workflows/cd-prod.yml +++ b/.github/workflows/cd-prod.yml @@ -77,7 +77,7 @@ jobs: aws-region: ap-northeast-2 - name: Upload to S3 - run: aws s3 cp --region ap-northeast-2 ./$GITHUB_SHA.zip s3://${{ secrets.AWS_S3_BUCKET_NAME }}/prod/ + run: aws s3 cp --region ap-northeast-2 ./$GITHUB_SHA.zip s3://${{ secrets.AWS_S3_BUCKET_NAME }}/prod/$GITHUB_SHA.zip # Deploy - name: Deploy From 3e19ec76cdc486676a2b53848bbea036bf0074d3 Mon Sep 17 00:00:00 2001 From: dragontaek-lee Date: Sat, 11 May 2024 02:13:04 +0900 Subject: [PATCH 5/7] =?UTF-8?q?fix:=20aws=20deploy=20key=EA=B0=92=20?= =?UTF-8?q?=EC=98=A4=EB=A5=98=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/cd-prod.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/cd-prod.yml b/.github/workflows/cd-prod.yml index 390bd3af..1279ca51 100644 --- a/.github/workflows/cd-prod.yml +++ b/.github/workflows/cd-prod.yml @@ -89,5 +89,5 @@ jobs: --application-name gam-codeDeploy --deployment-group-name prod --file-exists-behavior OVERWRITE - --s3-location bucket=${{ secrets.AWS_S3_BUCKET_NAME }},bundleType=zip,key=/prod/$GITHUB_SHA.zip + --s3-location bucket=${{ secrets.AWS_S3_BUCKET_NAME }},bundleType=zip,key=prod/$GITHUB_SHA.zip --region ap-northeast-2 From ee4c6fdd1353e341dcd021e8e6d74d0340048a75 Mon Sep 17 00:00:00 2001 From: dragontaek-lee Date: Sat, 1 Jun 2024 03:13:20 +0900 Subject: [PATCH 6/7] =?UTF-8?q?fix:=20switch=20=ED=8C=8C=EC=9D=BC=20?= =?UTF-8?q?=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- appspec.yml | 3 --- scripts/deploy.sh | 52 +++++++++++++++-------------------------------- scripts/switch.sh | 34 ------------------------------- 3 files changed, 16 insertions(+), 73 deletions(-) delete mode 100644 scripts/switch.sh diff --git a/appspec.yml b/appspec.yml index 6e814a98..44c46f6a 100644 --- a/appspec.yml +++ b/appspec.yml @@ -17,6 +17,3 @@ hooks: - location: deploy.sh timeout: 60 runas: ubuntu - - location: switch.sh - timeout: 180 - runas: ubuntu diff --git a/scripts/deploy.sh b/scripts/deploy.sh index 46e9e0e0..bb0b06b8 100644 --- a/scripts/deploy.sh +++ b/scripts/deploy.sh @@ -1,36 +1,16 @@ -##!/bin/bash -#REPOSITORY=/home/ubuntu/app -#BUILD_PATH=$(ls /home/ubuntu/app/*SNAPSHOT.jar) -#JAR_NAME=$(basename $BUILD_PATH) -#echo "> build 파일명: $JAR_NAME" -#IDLE_APPLICATION_PATH=/home/ubuntu/app/${JAR_NAME} -#CURRENT_PORT=$(cat /etc/nginx/conf.d/service-url.inc | grep -Po '[0-9]+' | tail -1) -#TARGET_PORT=0 -# -#echo "> Current port of running WAS is ${CURRENT_PORT}." -# -#if [ ${CURRENT_PORT} -eq 8081 ]; then -# TARGET_PORT=8082 -#elif [ ${CURRENT_PORT} -eq 8082 ]; then -# TARGET_PORT=8081 -#else -# echo "> No WAS is connected to nginx" -#fi -#echo "> Target port is ${TARGET_PORT}." -# -#TARGET_PID=$(lsof -Fp -i TCP:${TARGET_PORT} | grep -Po 'p[0-9]+' | grep -Po '[0-9]+') -#echo "> Target PID is ${TARGET_PID}." -# -#if [ ! -z ${TARGET_PID} ]; then -# echo "> Kill WAS running at ${TARGET_PORT}." -# sudo kill ${TARGET_PID} -#fi -# -#nohup java -jar -Duser.timezone=Asia/Seoul -Dserver.port=${TARGET_PORT} -Dspring.profiles.active=dev $IDLE_APPLICATION_PATH >> /home/ubuntu/app/nohup.out 2>&1 & -#echo "> Running port is ${TARGET_PORT}." -# -#echo "> Running PID is" -#lsof -i TCP:${TARGET_PORT} -# -# -#echo "> Now new WAS runs at ${TARGET_PORT}." +#!/bin/bash +REPOSITORY=/home/ubuntu/app +BUILD_PATH=$(ls /home/ubuntu/app/*SNAPSHOT.jar) +JAR_NAME=$(basename $BUILD_PATH) +IDLE_APPLICATION_PATH=/home/ubuntu/app/${JAR_NAME} +TARGET_PORT=8080 + +if [ "$DEPLOYMENT_GROUP_NAME" == "prod" ] +then + nohup java -jar -Duser.timezone=Asia/Seoul -Dserver.port=${TARGET_PORT} -Dspring.profiles.active=prod $IDLE_APPLICATION_PATH >> /home/ubuntu/app/nohup.out 2>&1 & +fi + +if [ "$DEPLOYMENT_GROUP_NAME" == "dev" ] +then + nohup java -jar -Duser.timezone=Asia/Seoul -Dserver.port=${TARGET_PORT} -Dspring.profiles.active=dev $IDLE_APPLICATION_PATH >> /home/ubuntu/app/nohup.out 2>&1 & +fi diff --git a/scripts/switch.sh b/scripts/switch.sh deleted file mode 100644 index d2aeacda..00000000 --- a/scripts/switch.sh +++ /dev/null @@ -1,34 +0,0 @@ -##!/bin/bash -# -#echo "> lsof -i TCP:8080-8085" -#lsof -i TCP:8080-8085 -# -#CURRENT_PORT=$(cat /etc/nginx/conf.d/service-url.inc | grep -Po '[0-9]+' | tail -1) -#TARGET_PORT=0 -# -#echo "> Nginx currently proxies to ${CURRENT_PORT}." -# -#if [ ${CURRENT_PORT} -eq 8081 ]; then -# TARGET_PORT=8082 -#elif [ ${CURRENT_PORT} -eq 8082 ]; then -# TARGET_PORT=8081 -#else -# echo "> No WAS is connected to nginx" -# exit 1 -#fi -# -#echo "set \$service_url http://127.0.0.1:${TARGET_PORT};" |sudo tee /etc/nginx/conf.d/service-url.inc -# -#echo "> Now Nginx proxies to ${TARGET_PORT}." -# -#sudo service nginx reload -# -#echo "> Nginx reloaded." -# -#CURRENT_PID=$(lsof -Fp -i TCP:${CURRENT_PORT} | grep -Po 'p[0-9]+' | grep -Po '[0-9]+') -#echo "> kill ${CURRENT_PID}" -# -#sudo kill ${CURRENT_PID} -# -#echo "> lsof -i TCP:8080-8085" -#lsof -i TCP:8080-8085 From 6da68534cf948dd9acaa446092fceeb3598e38e4 Mon Sep 17 00:00:00 2001 From: dragontaek-lee Date: Thu, 6 Jun 2024 00:51:27 +0900 Subject: [PATCH 7/7] fix: merge error --- scripts/deploy.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/scripts/deploy.sh b/scripts/deploy.sh index bb0b06b8..180a0fd9 100644 --- a/scripts/deploy.sh +++ b/scripts/deploy.sh @@ -4,6 +4,10 @@ BUILD_PATH=$(ls /home/ubuntu/app/*SNAPSHOT.jar) JAR_NAME=$(basename $BUILD_PATH) IDLE_APPLICATION_PATH=/home/ubuntu/app/${JAR_NAME} TARGET_PORT=8080 +CURRENT_PORT=8080 + +CURRENT_PID=$(lsof -Fp -i TCP:${CURRENT_PORT} | grep -Po 'p[0-9]+' | grep -Po '[0-9]+') +sudo kill ${CURRENT_PID} if [ "$DEPLOYMENT_GROUP_NAME" == "prod" ] then