Skip to content

Commit

Permalink
Merge pull request #100 from AR-TTUBEOG/dev
Browse files Browse the repository at this point in the history
[CI/CD] Prod, Dev server 분리 test
  • Loading branch information
sanggae4133 authored May 6, 2024
2 parents 808056c + c3f1c06 commit bc19cdb
Show file tree
Hide file tree
Showing 225 changed files with 8,804 additions and 1,024 deletions.
116 changes: 116 additions & 0 deletions .github/workflows/gradle-prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
name: Java CI with Gradle - Product

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

env:
AWS_REGION: ap-northeast-2
S3_BUCKET_NAME: s3-ttubeog
CODE_DEPLOY_APPLICATION_NAME: ttubeog-codedeploy
CODE_DEPLOY_DEPLOYMENT_GROUP_NAME: ttubeog-deploy-group


permissions:
contents: read

jobs:
build:
name: Build
runs-on: ubuntu-22.04

steps:
# 작업 엑세스 가능하게 $GITHUB_WORKSPACE에서 저장소를 체크아웃
- name: Checkout branch
uses: actions/checkout@v3

# java 버전 세팅
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'zulu'

# git ignore한 yml 파일들 github secret에서 복사해 오기
- name: Copy secret
env:
OCCUPY_SECRET: ${{ secrets.OCCUPY_SECRET_PROD }}
OCCUPY_LOGBACK_SECRET: ${{ secrets.OCCUPY_LOGBACK_SECRET }}

OCCUPY_SECRET_DIR: ./src/main/resources
OCCUPY_LOGBACK_SECRET_DIR: ./src/main/resources

OCCUPY_SECRET_DIR_FILE_NAME: application.yml
OCCUPY_LOGBACK_SECRET_DIR_FILE_NAME: logback-test.xml
run: |
touch $OCCUPY_SECRET_DIR/$OCCUPY_SECRET_DIR_FILE_NAME
touch $OCCUPY_LOGBACK_SECRET_DIR/$OCCUPY_LOGBACK_SECRET_DIR_FILE_NAME
echo "$OCCUPY_SECRET" > $OCCUPY_SECRET_DIR/$OCCUPY_SECRET_DIR_FILE_NAME
echo "$OCCUPY_LOGBACK_SECRET" > $OCCUPY_LOGBACK_SECRET_DIR/$OCCUPY_LOGBACK_SECRET_DIR_FILE_NAME
# gradlew 실행 권한 부여
- name: Run chmod to make gradlew executable
run: chmod +x ./gradlew
shell: bash

# Build -> jar 파일 생성
- name: Build with Gradle
run: ./gradlew clean build -x test
shell: bash

- name: Upload Build artifacts
uses: actions/upload-artifact@v2
with:
name: build-artifacts
path: |
build/libs/*.jar
- name: Start redis-server
uses: appleboy/scp-action@master
with:
key: ${{ secrets.EC2_KEY_PROD }}
host: ${{ secrets.EC2_HOST_PROD }}
username: ${{ secrets.EC2_USER_PROD }}
target: /home/ubuntu/app/spring_source
source: .
command: sudo systemctl start redis-server




deploy:
name: CD with SSH
needs: build
runs-on: ubuntu-22.04

steps:
- name: Download build artifacts
uses: actions/download-artifact@v2
with:
name: build-artifacts

# JAR 파일을 EC2에 배포하는 step
- name: SCP JAR to EC2
uses: appleboy/scp-action@master
with:
key: ${{ secrets.EC2_KEY_PROD }}
host: ${{ secrets.EC2_HOST_PROD }}
username: ${{ secrets.EC2_USER_PROD }}
source: "*.jar"
target: "/home/ubuntu/app"

# EC2에 SSH로 배포 커맨드를 입력하는 step
- name: Deploy SSH
uses: appleboy/ssh-action@master
with:
key: ${{ secrets.EC2_KEY_PROD }}
host: ${{ secrets.EC2_HOST_PROD }}
username: ${{ secrets.EC2_USER_PROD }}
# 기존 실행 중인 서버 종료 후 jar 파일 실행
script: |
sudo fuser -k -n tcp 8080
sleep 15
sudo nohup java -jar /home/ubuntu/app/*.jar > ./nohup.out 2>&1 &
116 changes: 116 additions & 0 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
name: Java CI with Gradle

on:
push:
branches: [ "dev" ]
pull_request:
branches: [ "dev" ]

env:
AWS_REGION: ap-northeast-2
S3_BUCKET_NAME: s3-ttubeog
CODE_DEPLOY_APPLICATION_NAME: ttubeog-codedeploy
CODE_DEPLOY_DEPLOYMENT_GROUP_NAME: ttubeog-deploy-group


permissions:
contents: read

jobs:
build:
name: Build
runs-on: ubuntu-22.04

steps:
# 작업 엑세스 가능하게 $GITHUB_WORKSPACE에서 저장소를 체크아웃
- name: Checkout branch
uses: actions/checkout@v3

# java 버전 세팅
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'zulu'

# git ignore한 yml 파일들 github secret에서 복사해 오기
- name: Copy secret
env:
OCCUPY_SECRET: ${{ secrets.OCCUPY_SECRET_DEV }}
OCCUPY_LOGBACK_SECRET: ${{ secrets.OCCUPY_LOGBACK_SECRET }}

OCCUPY_SECRET_DIR: ./src/main/resources
OCCUPY_LOGBACK_SECRET_DIR: ./src/main/resources

OCCUPY_SECRET_DIR_FILE_NAME: application.yml
OCCUPY_LOGBACK_SECRET_DIR_FILE_NAME: logback-test.xml
run: |
touch $OCCUPY_SECRET_DIR/$OCCUPY_SECRET_DIR_FILE_NAME
touch $OCCUPY_LOGBACK_SECRET_DIR/$OCCUPY_LOGBACK_SECRET_DIR_FILE_NAME
echo "$OCCUPY_SECRET" > $OCCUPY_SECRET_DIR/$OCCUPY_SECRET_DIR_FILE_NAME
echo "$OCCUPY_LOGBACK_SECRET" > $OCCUPY_LOGBACK_SECRET_DIR/$OCCUPY_LOGBACK_SECRET_DIR_FILE_NAME
# gradlew 실행 권한 부여
- name: Run chmod to make gradlew executable
run: chmod +x ./gradlew
shell: bash

# Build -> jar 파일 생성
- name: Build with Gradle
run: ./gradlew clean build -x test
shell: bash

- name: Upload Build artifacts
uses: actions/upload-artifact@v2
with:
name: build-artifacts
path: |
build/libs/*.jar
- name: Start redis-server
uses: appleboy/scp-action@master
with:
key: ${{ secrets.EC2_KEY_DEV }}
host: ${{ secrets.EC2_HOST_DEV }}
username: ${{ secrets.EC2_USER_DEV }}
target: /home/ubuntu/app/spring_source
source: .
command: sudo systemctl start redis-server




deploy:
name: CD with SSH
needs: build
runs-on: ubuntu-22.04

steps:
- name: Download build artifacts
uses: actions/download-artifact@v2
with:
name: build-artifacts

# JAR 파일을 EC2에 배포하는 step
- name: SCP JAR to EC2
uses: appleboy/scp-action@master
with:
key: ${{ secrets.EC2_KEY_DEV }}
host: ${{ secrets.EC2_HOST_DEV }}
username: ${{ secrets.EC2_USER_DEV }}
source: "*.jar"
target: "/home/ubuntu/app"

# EC2에 SSH로 배포 커맨드를 입력하는 step
- name: Deploy SSH
uses: appleboy/ssh-action@master
with:
key: ${{ secrets.EC2_KEY_DEV }}
host: ${{ secrets.EC2_HOST_DEV }}
username: ${{ secrets.EC2_USER_DEV }}
# 기존 실행 중인 서버 종료 후 jar 파일 실행
script: |
sudo fuser -k -n tcp 8080
sleep 15
sudo nohup java -jar /home/ubuntu/app/*.jar > ./nohup.out 2>&1 &
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,14 @@ out/
.LSOverride

### Project security ###
**/resources/
**/resources/application.yml
**/resources/database/
**/resources/oauth2/
**/resources/logback.xml
**/applicatioin.yml
**/application-database.yml
**/application-oauth2.yml
**/logback.yml
Dockerfile
docker-compose.yaml
deploy_*.sh
Expand Down
27 changes: 27 additions & 0 deletions appspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# appspec.yml

version: 0.0
os: linux

files:
- source: /
destination: /home/ubuntu/app # (ec2)인스턴스에서 파일이 저장될 위치
overwrite: true
file_exists_behavior: OVERWRITE

permissions:
- object: /
pattern: "**"
owner: ubuntu
group: ubuntu
mode: 775

hooks:
AfterInstall:
- location: scripts/stop.sh
timeout: 60
runas: ubuntu
ApplicationStart:
- location: scripts/start.sh
timeout: 600
runas: ubuntu
38 changes: 37 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,33 @@ sourceCompatibility = '17'

repositories {
mavenCentral()

// discord log back
maven { url 'https://jitpack.io' }
}

dependencyManagement {

imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:2022.0.5"
}
}


dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-webflux'
implementation 'org.springframework.boot:spring-boot-starter-data-redis'


implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:3.0.2'

implementation group: 'org.springdoc', name: 'springdoc-openapi-starter-webmvc-ui', version: '2.3.0'
implementation 'org.jetbrains:annotations:24.0.0'
testImplementation group: 'org.springdoc', name: 'springdoc-openapi-starter-webmvc-api', version: '2.3.0'

implementation group: 'io.jsonwebtoken', name: 'jjwt-api', version: '0.11.5'
Expand All @@ -30,12 +47,31 @@ dependencies {
annotationProcessor 'org.projectlombok:lombok'
annotationProcessor "org.springframework.boot:spring-boot-configuration-processor"

runtimeOnly 'org.mariadb.jdbc:mariadb-java-client'
runtimeOnly 'com.mysql:mysql-connector-j'

testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'

// discord log back
implementation 'com.github.napstr:logback-discord-appender:1.0.0'

implementation platform("org.springframework.cloud:spring-cloud-dependencies:2022.0.5")
implementation "org.springframework.cloud:spring-cloud-starter-openfeign"

if (System.getProperty("os.name") == "Mac OS X" && System.getProperty("os.arch") == "aarch64") {
runtimeOnly("io.netty:netty-resolver-dns-native-macos:4.1.94.Final:osx-aarch_64")
}

implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE'

}



tasks.named('test') {
useJUnitPlatform()
}

jar {
enabled = false
}
31 changes: 31 additions & 0 deletions scripts/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash

REPOSITORY=/home/ubuntu/app
cd $REPOSITORY

# 1) 애플리케이션이 구동중인지 확인하기 위한 애플리케이션 이름, jar 파일의 이름
APP_NAME=RecordOfMemory
JAR_NAME=$(ls $REPOSITORY/build/libs/ | grep '.jar' | tail -n 1) # `이 아닌 ' 사용해야함...
JAR_PATH=$REPOSITORY/build/libs/$JAR_NAME

# 2) 현재 인스턴스에서 애플리케이션이 구동중인지 확인, 구동중이면 종료
CURRENT_PID=$(pgrep -f $APP_NAME)

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

# 3) app 디렉토리 권한 수정
cd /home/ubuntu
sudo chmod 775 app
sudo chown ubuntu app

# 4) jar 파일을 배포
echo "> $JAR_PATH 배포"
# nohup java -jar $JAR_PATH &
nohup java -jar $JAR_PATH > /dev/null 2>&1 &
Loading

0 comments on commit bc19cdb

Please sign in to comment.