diff --git a/.github/workflows/ci-pipeline.yml b/.github/workflows/ci-pipeline.yml index b8a85a1..e4c632e 100644 --- a/.github/workflows/ci-pipeline.yml +++ b/.github/workflows/ci-pipeline.yml @@ -5,6 +5,12 @@ on: branches: - main +env: + PROJECT_NAME: feed-project + BUCKET_NAME: feed-cd-bucket + CODE_DEPLOY_APP_NAME: feed_cd + DEPLOYMENT_GROUP_NAME: feed_instance + permissions: contents: read checks: write @@ -49,3 +55,24 @@ jobs: with: webhook-url: ${{ secrets.WEBHOOK_URL }} content: ":love_letter: 새로운 PR이 날아왔습니다 ~ :love_letter:\n:link: 리뷰하러 가기: ${{ github.event.pull_request.html_url }}" + + - name: ZIP 파일 생성 + run: zip -qq -r ./$GITHUB_SHA.zip . + shell: bash + + - 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_PRIVATE_ACCESS_KEY }} + aws-region: ap-northeast-2 + + - name: Upload to S3 + run: aws s3 cp --region ap-northeast-2 ./$GITHUB_SHA.zip s3://$BUCKET_NAME/$PROJECT_NAME/$GITHUB_SHA.zip + + - name: Code Deploy To EC2 instance + run: aws deploy create-deployment + --application-name $CODE_DEPLOY_APP_NAME + --deployment-config-name CodeDeployDefault.AllAtOnce + --deployment-group-name $DEPLOYMENT_GROUP_NAME + --s3-location bucket=$BUCKET_NAME,bundleType=zip,key=$PROJECT_NAME/$GITHUB_SHA.zip diff --git a/appspec.yml b/appspec.yml new file mode 100644 index 0000000..799e37b --- /dev/null +++ b/appspec.yml @@ -0,0 +1,20 @@ +version: 0.0 +os: linux + +# 배포할 파일 및 디렉토리를 정의합니다. +files: + - source: / + destination: /home/ubuntu/feed + +# 권한을 정의합니다. +permissions: + - object: /home/ubuntu/feed/ + owner: ubuntu + group: ubuntu + +# 배포단계에서 진행하는 훅 스크립트를 정의합니다. +hooks: + AfterInstall: + - location: scripts/deploy.sh + timeout: 60 + runas: ubuntu \ No newline at end of file diff --git a/build.gradle b/build.gradle index f1bf61c..f04aba5 100644 --- a/build.gradle +++ b/build.gradle @@ -7,6 +7,7 @@ plugins { group = 'team05' version = '0.0.1-SNAPSHOT' +jar.enabled = false java { toolchain { diff --git a/scripts/deploy.sh b/scripts/deploy.sh new file mode 100644 index 0000000..6db89d3 --- /dev/null +++ b/scripts/deploy.sh @@ -0,0 +1,21 @@ + +REPOSITORY=/home/ubuntu/plug +cd $REPOSITORY + +APP_NAME=plug +JAR_NAME=$(ls $REPOSITORY/build/libs/ | grep 'SNAPSHOT.jar' | tail -n 1) +JAR_PATH=$REPOSITORY/build/libs/$JAR_NAME + +CURRENT_PID=$(pgrep -f $APP_NAME) + +if [ -z $CURRENT_PID ] +then + echo "> 종료할 애플리케이션이 없습니다." +else + echo "> kill -9 $CURRENT_PID" + kill -15 $CURRENT_PID + sleep 5 +fi + +echo "> Deploy - $JAR_PATH " +nohup java -jar $JAR_PATH > /dev/null 2> /dev/null < /dev/null & \ No newline at end of file