Skip to content

Commit

Permalink
setting: githubActions와 codeDeploy를 이용한 자동배포 스크립트 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
seminchoi committed Aug 20, 2024
1 parent 3d31f68 commit 5ce652e
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
55 changes: 55 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: CD

on:
push:
branches: [ "main", "feature/deploy" ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v3
with:
token: ${{secrets.PRIVATE_TOKEN}}
submodules: true
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'

- name: Grant execute permission for gradlew
run: chmod +x gradlew

- name: Make directory for deliver
run: mkdir deploy

- name: Build with Gradle
run: ./gradlew clean build

- name: Copy jar
run: cp ./build/libs/*.jar ./$GITHUB_SHA.jar

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ap-northeast-2

- name: Upload to S3
run: aws s3 cp --region ap-northeast-2 ./$GITHUB_SHA.jar s3://team3rdparty-bucket/deploy/$GITHUB_SHA.jar

- name: Deploy
run: |
aws deploy create-deployment \
--application-name ticketing \
--deployment-config-name CodeDeployDefault.AllAtOnce \
--deployment-group-name ticketing \
--file-exists-behavior OVERWRITE \
--s3-location bucket=team3rdparty-bucket,bundleType=zip,key=deploy/$GITHUB_SHA.zip \
--region ap-northeast-2 \
--ec2-tag-filter Key=Name,Value=team3-ticket,Type=KEY_AND_VALUE
11 changes: 11 additions & 0 deletions appspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: 0.0
os: linux
files:
- source: /*.jar
destination: /home/ubuntu/deploy/ticketing.jar

hooks:
ApplicationStart:
- location: scripts/deploy.sh
timeout: 300
runas: ubuntu
19 changes: 19 additions & 0 deletions scripts/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

# 변수 설정
build_jar=$(ls /home/ubuntu/app/deploy/ticketing.jar)
jar_name=$(basename "$build_jar")
deploy_log=/home/ubuntu/app/deploy/deloy.log

echo ">>> 현재 실행중인 애플리케이션 pid 확인" >> $deploy_log
CURRENT_PID=$(pgrep -f "$jar_name")

if [ -z "$CURRENT_PID" ]
then
echo ">>> 현재 구동중인 애플리케이션이 없으므로 종료하지 않습니다." >> $deploy_log
else
kill -9 "$CURRENT_PID"
fi

echo ">>> DEPLOY_JAR 배포" >> $deploy_log
nohup java -jar "$build_jar" --spring.profiles.active=prod

0 comments on commit 5ce652e

Please sign in to comment.