Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: 배포자동화 및 테스트 서버 CI/CD 구축 #5

Merged
merged 4 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ name: CI

on:
push:
branches: [ "develop" ]
branches: [ "main" ]

env:
PROJECT_NAME: CS-Quiz-BE
BUCKET_NAME: cotato-ci-cd-bucket
CODE_DEPLOY_APP_NAME: cotato-deploy
DEPLOYMENT_GROUP_NAME: cotato-deploy-group
RESOURCE_PATH: ./src/main/resources/application.yml
RESOURCE_PATH: ./src/main/resources/application-prod.yml

jobs:
build:
Expand Down Expand Up @@ -51,7 +51,7 @@ jobs:

# [3] 프로젝트 빌드
- name: Build with Gradle Wrapper
run: ./gradlew build
run: ./gradlew prodJar

# [4] Zip 파일 만들기
- name: Make Zip File
Expand Down
75 changes: 75 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
name: TEST-CI

on:
push:
branches: [ "release" ]

env:
PROJECT_NAME: COTATO-BE-TEST
BUCKET_NAME: cotato-ci-cd-bucket-test
CODE_DEPLOY_APP_NAME: cotato-deploy-test
DEPLOYMENT_GROUP_NAME: cotato-deploy-group-test
RESOURCE_PATH: ./src/main/resources/application-stage.yml

jobs:
build:
runs-on: ubuntu-latest

steps:
# [0] JDK 세팅
- uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'zulu'

# [1] Set up Yaml File
- name: Set up Yaml File
uses: microsoft/variable-substitution@v1
with:
files: ${{ env.RESOURCE_PATH }}
env:
spring.datasource.url: ${{ secrets.TEST_DB_PATH }}
spring.datasource.username: ${{ secrets.TEST_DB_USERNAME }}
spring.datasource.password: ${{ secrets.TEST_DB_PWD }}
spring.jpa.hibernate.ddl-auto: ${{ secrets.TEST_DDL_AUTO }}
spring.mail.username: ${{ secrets.SENDER_EMAIL }}
spring.mail.password: ${{ secrets.SENDER_PASSWORD }}
jwt.secretKey: ${{ secrets.JWT_SECRET_KEY}}
cloud.aws.s3.bucket: ${{ secrets.S3_BUCKET_NAME }}
cloud.aws.credentials.accessKey: ${{ secrets.S3_ACCESS_KEY }}
cloud.aws.credentials.secretKey: ${{ secrets.S3_ACCESS_PASSWORD }}
aes.secret.key: ${{ secrets.AES_SECRET_KEY }}
aes.secret.salt: ${{ secrets.AES_SECRET_SALT }}


# [2] 실행 권한 부여
- name: Grant permission for gradlew
run: chmod +x ./gradlew
shell: bash

# [3] 프로젝트 빌드
- name: Build with Gradle Wrapper
run: ./gradlew stageJar

# [4] Zip 파일 만들기
- name: Make Zip File
run: zip -r $GITHUB_SHA.zip .
shell: bash

# [5] AWS 키 설정
- 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 }}

# [6] 빌드한 jar파일 S3업로드
- name: Upload to s3
run: aws s3 cp --region ap-northeast-2 ./$GITHUB_SHA.zip s3://${{ env.BUCKET_NAME }}/$GITHUB_SHA.zip

# [7] CodeDeploy에 S3에서 파일 받고 배포
- name: Code Deploy
run: aws deploy create-deployment --deployment-config-name CodeDeployDefault.AllAtOnce --application-name ${{ env.CODE_DEPLOY_APP_NAME }} --deployment-group-name ${{ env.DEPLOYMENT_GROUP_NAME }} --s3-location bucket=${{ env.BUCKET_NAME }},bundleType=zip,key=$GITHUB_SHA.zip
10 changes: 10 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,13 @@ tasks.named('test') {
jar {
enabled = false
}

task stageJar(type: Jar) {
archiveBaseName.set("${project.name}-stage")
from sourceSets.main.output
}

task prodJar(type: Jar) {
archiveBaseName.set("${project.name}-prod")
from sourceSets.main.output
}
31 changes: 25 additions & 6 deletions scripts/deploy.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
#!/bin/bash

# 현재 브랜치 이름 가져오기
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)

# 기본 환경 변수 세팅
PROJECT_NAME="CS-Quiz-BE"
JAR_PATH="/home/ubuntu/backend/$PROJECT_NAME/build/libs/*.jar"
DEPLOY_PATH=/home/ubuntu/backend/$PROJECT_NAME/
DEPLOY_LOG_PATH="/home/ubuntu/backend/$PROJECT_NAME/deploy_$(date +%Y%m%d).log"
DEPLOY_ERR_LOG_PATH="/home/ubuntu/backend/$PROJECT_NAME/deploy_err_$(date +%Y%m%d).log"
APPLICATION_LOG_PATH="/home/ubuntu/backend/$PROJECT_NAME/application_$(date +%Y%m%d).log"

# 브랜치에 따른 설정 값 분리
if [ "$CURRENT_BRANCH" == "main" ]; then
PROFILE="prod"
DIRECTORY="production"
PORT=8080
elif [ "$CURRENT_BRANCH" == "release" ]; then
PROFILE="stage"
DIRECTORY="release"
PORT=8082
else
echo "지원되지 않는 브랜치입니다: $CURRENT_BRANCH" >&2
exit 1
fi

JAR_PATH="/home/ubuntu/backend/$PROJECT_NAME/build/libs/*$PROFILE.jar"
DEPLOY_PATH=/home/ubuntu/backend/$DIRECTORY/$PROJECT_NAME/ #jar 파일이 복사되고 실행될 경로
DEPLOY_LOG_PATH="/home/ubuntu/backend/$DIRECTORY/log/deploy/$PROJECT_NAME/deploy_$(date +%Y%m%d).log"
DEPLOY_ERR_LOG_PATH="/home/ubuntu/backend/$DIRECTORY/log/deploy/$PROJECT_NAME/deploy_err_$(date +%Y%m%d).log"
APPLICATION_LOG_PATH="/home/ubuntu/backend/$DIRECTORY/log/$PROJECT_NAME/application_$(date +%Y%m%d).log"
BUILD_JAR=$(ls $JAR_PATH)
JAR_NAME=$(basename $BUILD_JAR)

Expand All @@ -32,7 +51,7 @@ fi
DEPLOY_JAR=$DEPLOY_PATH$JAR_NAME
echo "> DEPLOY_JAR 배포" >> $DEPLOY_LOG_PATH

nohup java -jar -Dspring.profiles.active=local $DEPLOY_JAR --server.port=8080 >> $APPLICATION_LOG_PATH 2> $DEPLOY_ERR_LOG_PATH &
nohup java -jar -Dspring.profiles.active=$PROFILE $DEPLOY_JAR --server.port=$PORT >> $APPLICATION_LOG_PATH 2> $DEPLOY_ERR_LOG_PATH &

sleep 3
echo "> 배포 종료 : $(date +%c)" >> $DEPLOY_LOG_PATH
23 changes: 23 additions & 0 deletions src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# dev-local

server:
port: 8080

spring:

datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://${DB_PATH}
username: ${DB_USERNAME}
password: ${DB_PWD}

jpa:
hibernate:
ddl-auto: update
properties:
hibernate:
format_sql: true

springdoc:
swagger-ui:
enabled: true
19 changes: 19 additions & 0 deletions src/main/resources/application-prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
server:
port: 8080

spring:
jpa:
hibernate:
ddl-auto: ${DDL_AUTO}
show_sql: true

datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://${DB_PATH}
username: ${DB_USERNAME}
password: ${DB_PWD}


springdoc:
swagger-ui:
enabled: false
24 changes: 24 additions & 0 deletions src/main/resources/application-stage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# test - server

server:
port: 8082

spring:
jpa:
hibernate:
ddl-auto: ${TEST_DDL_AUTO}
show_sql: true
properties:
hibernate:
format_sql: true


datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://${TEST_DB_PATH}
username: ${TEST_DB_USERNAME}
password: ${TEST_DB_PWD}

springdoc:
swagger-ui:
enabled: true
16 changes: 3 additions & 13 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://${DB_PATH}/cotato
username: ${DB_USERNAME}
password: ${DB_PWD}
profiles:
active: local

jpa:
hibernate:
ddl-auto: ${DDL_AUTO}
properties:
hibernate:
format_sql: true
show_sql: true
# 로컬, 개발, 운영 환경의 공통된 설정

data:
redis:
Expand Down Expand Up @@ -62,7 +53,6 @@ cloud:
springdoc:
show-login-endpoint: true


server:
servlet:
session:
Expand Down
Loading