From 6bbaffb5010c7161ffa5e9149486a72ac5a77cb2 Mon Sep 17 00:00:00 2001 From: wnsrl Date: Wed, 29 May 2024 17:48:49 +0900 Subject: [PATCH] =?UTF-8?q?cicd:=20=EB=B0=B0=ED=8F=AC=20=EA=B4=80=EB=A0=A8?= =?UTF-8?q?=20=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/server.yml | 41 ++++++++++++++++++++++++++++++------ appspec.yml | 23 ++++++++++++++++++++ build.gradle | 4 ++++ scripts/start.sh | 19 +++++++++++++++++ scripts/stop.sh | 19 +++++++++++++++++ 5 files changed, 100 insertions(+), 6 deletions(-) create mode 100644 appspec.yml create mode 100644 scripts/start.sh create mode 100644 scripts/stop.sh diff --git a/.github/workflows/server.yml b/.github/workflows/server.yml index 0daae9e..f285487 100644 --- a/.github/workflows/server.yml +++ b/.github/workflows/server.yml @@ -8,10 +8,20 @@ on: branches: - 'master' +env: + AWS_REGION: ${{secrets.AWS_REGION}} + S3_BUCKET_NAME: ${{secrets.S3_BUCKET_NAME}} + CODE_DEPLOY_APPLICATION_NAME: ${{secrets.CODE_DEPLOY_APPLICATION_NAME}} + CODE_DEPLOY_DEPLOYMENT_GROUP_NAME: ${{secrets.CODE_DEPLOY_DEPLOYMENT_GROUP_NAME}} + +permissions: + contents: read + jobs: - build: - name: spring boot build & deploy + deploy: + name: Deploy runs-on: ubuntu-latest + environment: production steps: # 레포 불러오기 @@ -33,8 +43,27 @@ jobs: - name: Build with Gradle run : ./gradlew clean build --exclude-task test - - name: test - run: ls -al src/main/resources/meme-title-security/ + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v1 + with: + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_KEY }} + aws-region: ${{ env.AWS_REGION }} + + # 빌드 결과물을 S3 버킷에 업로드 + - name: Upload to AWS S3 + run: | + aws deploy push \ + --application-name ${{ env.CODE_DEPLOY_APPLICATION_NAME }} \ + --ignore-hidden-files \ + --s3-location s3://$S3_BUCKET_NAME/$GITHUB_SHA.zip \ + --source . - - name: test - run: ls -al src/main/resources/ \ No newline at end of file + # S3 버킷에 있는 파일을 대상으로 CodeDeploy 실행 + - name: Deploy to AWS EC2 from S3 + run: | + aws deploy create-deployment \ + --application-name ${{ env.CODE_DEPLOY_APPLICATION_NAME }} \ + --deployment-config-name CodeDeployDefault.AllAtOnce \ + --deployment-group-name ${{ env.CODE_DEPLOY_DEPLOYMENT_GROUP_NAME }} \ + --s3-location bucket=$S3_BUCKET_NAME,key=$GITHUB_SHA.zip,bundleType=zip \ No newline at end of file diff --git a/appspec.yml b/appspec.yml new file mode 100644 index 0000000..9882a4f --- /dev/null +++ b/appspec.yml @@ -0,0 +1,23 @@ +version: 0.0 +os: linux + +files: + - source: / + destination: /home/ubuntu/app + overwrite: yes + +permissions: + - object: / + pattern: "**" + owner: ubuntu + group: ubuntu + +hooks: + AfterInstall: + - location: scripts/stop.sh + timeout: 60 + runas: ubuntu + ApplicationStart: + - location: scripts/start.sh + timeout: 60 + runas: ubuntu \ No newline at end of file diff --git a/build.gradle b/build.gradle index f9f0eb3..1bde523 100644 --- a/build.gradle +++ b/build.gradle @@ -52,4 +52,8 @@ tasks.register('copySecret', Copy) { from './src/main/resources/meme-title-security' include 'application*.yml' into './src/main/resources' +} + +jar { + enabled = false } \ No newline at end of file diff --git a/scripts/start.sh b/scripts/start.sh new file mode 100644 index 0000000..2698fd4 --- /dev/null +++ b/scripts/start.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env bash + +PROJECT_ROOT="/home/ubuntu/app" +JAR_FILE="$PROJECT_ROOT/memetitle.jar" + +DEPLOY_LOG="$PROJECT_ROOT/deploy.log" + +TIME_NOW=$(date +%c) + +# build 파일 복사 +echo "$TIME_NOW > $JAR_FILE 파일 복사" >> $DEPLOY_LOG +cp $PROJECT_ROOT/build/libs/*.jar $JAR_FILE + +# jar 파일 실행 +echo "$TIME_NOW > $JAR_FILE 파일 실행" >> $DEPLOY_LOG +nohup java -jar -Dspring.profiles.active=prod $JAR_FILE 1> /dev/null 2>&1 & + +CURRENT_PID=$(pgrep -f $JAR_FILE) +echo "$TIME_NOW > 실행된 프로세스 아이디 $CURRENT_PID 입니다." >> $DEPLOY_LOG \ No newline at end of file diff --git a/scripts/stop.sh b/scripts/stop.sh new file mode 100644 index 0000000..15d5ddc --- /dev/null +++ b/scripts/stop.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env bash + +PROJECT_ROOT="/home/ubuntu/app" +JAR_FILE="$PROJECT_ROOT/memetitle.jar" + +DEPLOY_LOG="$PROJECT_ROOT/deploy.log" + +TIME_NOW=$(date +%c) + +# 현재 구동 중인 애플리케이션 pid 확인 +CURRENT_PID=$(pgrep -f $JAR_FILE) + +# 프로세스가 켜져 있으면 종료 +if [ -z $CURRENT_PID ]; then + echo "$TIME_NOW > 현재 실행중인 애플리케이션이 없습니다" >> $DEPLOY_LOG +else + echo "$TIME_NOW > 실행중인 $CURRENT_PID 애플리케이션 종료 " >> $DEPLOY_LOG + kill -15 $CURRENT_PID +fi \ No newline at end of file