Skip to content

Commit

Permalink
[FEAT] 운영 개발 서버 분리 및 배포자동화 업데이트 (#372)
Browse files Browse the repository at this point in the history
* [CHORE] gitignore 수정

* [ADD] github action dev & prod 용 추가

* [DEL] 기존 deploy github action 제거

* [CHORE] build github action 실행 포인트 변경

* [ADD] 개발용 도커파일 및 도커 컴포즈 추가

* [ADD] 개발용 도커파일 및 도커 컴포즈 추가 및 기존 파일 제거

* [CHORE] 배포 경로 수정

* [CHORE] code deploy 배포 스크립트 수정

* [CHORE] 깃헙 액션에서 yml 파일명 수정

* [CHORE] 깃헙 액션 job 이름 변경

* [CHORE] 깃헙 액션 시크릿 변수 변경
  • Loading branch information
Goder-0 authored Jul 31, 2024
1 parent 93d1173 commit bafaab4
Show file tree
Hide file tree
Showing 11 changed files with 336 additions and 113 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,16 @@ name: BackEnd - CI/CD(build)

on:
pull_request:
branches: [ "main","develop" ]
branches: [ "develop", "release/v**", "main" ]
push:
branches: [ "develop" ]
branches: [ "develop", "release/v**" ]

permissions:
contents: read

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
# JDK를 17 버전으로 셋팅한다.
Expand Down
56 changes: 29 additions & 27 deletions .github/workflows/deploy.yml → .github/workflows/deploy-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,33 @@ name: BackEnd - CI/CD(deploy)

on:
pull_request:
types: [ closed ]
branches: [ "develop" ]
types: [ closed ]


permissions:
contents: read

jobs:
deploy:
deploy-dev:
if: github.event.pull_request.merged == true
runs-on: ubuntu-latest

environment: develop
env:
DEPLOYMENT_GROUP_NAME: meeteam-dev
S3_BUCKET_DIR_NAME: /dev
steps:
- uses: actions/checkout@v3
- name: ✅ Checkout branch
uses: actions/checkout@v3
# JDK를 17 버전으로 셋팅한다.
- name: Set up JDK 17
- name: ⚙️ Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'

# Gradle을 캐싱한다 -> 빌드 속도가 증가하는 효과가 있다.
- name: Gradle 캐싱
- name: Gradle 캐싱
uses: actions/cache@v3
with:
path: |
Expand All @@ -41,7 +46,7 @@ jobs:
${{ runner.os }}-gradle-
# 프로젝트 저장소에 업로드하면 안되는 설정 파일들을 만들어줍니다.
- name: Make application.yml & KEYS & ApproveMail.html
- name: 🗂️ Make config
run: |
# src/main/resources 경로 이동
cd ./src/main/resources
Expand Down Expand Up @@ -71,47 +76,44 @@ jobs:
MAIL_APPLICATION_NOTIFICATION_TEMPLATE: ${{ secrets.MAIL_APPLICATION_NOTIFICATION_TEMPLATE }}
shell: bash

- name: Gradle 권한 부여
- name: ⚙️ Gradle 권한 부여
run: chmod +x gradlew

- name: Gradle로 빌드 실행
- name: ⚙️ Gradle로 빌드 실행
run: ./gradlew bootjar


# 배포에 필요한 여러 설정 파일과 프로젝트 빌드파일을 zip 파일로 모아줍니다.
- name: zip file 생성
# 배포에 필요한 여러 설정 파일과 프로젝트 빌드파일을 zip 파일로 모아준다.
- name: 📦 zip file 생성
run: |
mkdir deploy
cp ./docker/docker-compose.blue.yml ./deploy/
cp ./docker/docker-compose.green.yml ./deploy/
cp ./docker/dev/docker-compose.dev.yml ./deploy/
cp ./docker/dev/Dockerfile ./deploy/
cp ./appspec.yml ./deploy/
cp ./docker/Dockerfile ./deploy/
cp ./scripts/*.sh ./deploy/
cp ./scripts/deploy.sh ./deploy/
cp ./build/libs/*.jar ./deploy/
zip -r -qq -j ./spring-build.zip ./deploy
zip -r -qq -j ./spring-app.zip ./deploy

# AWS에 연결해줍니다.
- name: AWS 연결
# AWS에 연결해준다.
- name: 🌎 AWS 연결
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 }}

# S3에 프로젝트를 업로드 합니다.
- name: S3에 프로젝트 업로드
# S3에 프로젝트를 업로드 한다.
- name: 🚛 S3에 프로젝트 업로드
run: |
aws s3 cp \
--region ap-northeast-2 \
./spring-build.zip s3://meeteam-backend-bucket
./spring-app.zip s3://meeteam-backend-bucket
# CodeDelploy에 배포를 요청합니다.
- name: Code Deploy 배포 요청
# CodeDelploy에 배포를 요청한다.
- name: 🚀 Code Deploy 배포 요청
run: aws deploy create-deployment --application-name meeteam-app
--deployment-config-name CodeDeployDefault.OneAtATime
--deployment-group-name meeteam-app
--s3-location bucket=meeteam-backend-bucket,bundleType=zip,key=spring-build.zip
--deployment-group-name ${{ env.DEPLOYMENT_GROUP_NAME }}
--s3-location bucket=${{ secrets.S3_BUCKET_NAME }},bundleType=zip,key=${{ env.S3_BUCKET_DIR_NAME }}/spring-app.zip

- name: Discord 알림 봇
uses: sarisia/actions-status-discord@v1
Expand Down
124 changes: 124 additions & 0 deletions .github/workflows/deploy-prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.
# This workflow will build a Java project with Gradle and cache/restore any dependencies to improve the workflow execution time
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-gradle

name: BackEnd - CI/CD(deploy)

on:
pull_request:
branches: [ "main" ]
types: [ closed ]


permissions:
contents: read

jobs:
deploy-prod:
if: github.event.pull_request.merged == true
runs-on: ubuntu-latest
environment: production
env:
DEPLOYMENT_GROUP_NAME: meeteam-app
S3_BUCKET_DIR_NAME: /prod
steps:
- name: ✅ Checkout branch
uses: actions/checkout@v3
# JDK를 17 버전으로 셋팅한다.
- name: ⚙️ Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'

# Gradle을 캐싱한다 -> 빌드 속도가 증가하는 효과가 있다.
- name: ✅ Gradle 캐싱
uses: actions/cache@v3
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
# 프로젝트 저장소에 업로드하면 안되는 설정 파일들을 만들어줍니다.
- name: 🗂️ Make config
run: |
# src/main/resources 경로 이동
cd ./src/main/resources
# yml 파일 생성
touch ./application-prod.yml
echo "$APPLICATION_PROD" > ./application-prod.yml
# 폴더 생성
mkdir templates
# 메일 관련된 html 파일 생성
cd ./templates
touch ./ApproveMail.html
echo "$MAIL_APPROVE_TEMPLATE" > ./ApproveMail.html
touch ./UniversityAuthMail.html
echo "$MAIL_VERIFY_TEMPLATE" > ./UniversityAuthMail.html
touch ./ApplicationNotificationMail.html
echo "$MAIL_APPLICATION_NOTIFICATION_TEMPLATE" > ./ApplicationNotificationMail.html
env:
APPLICATION_PROD: ${{ secrets.APPLICATION_PROD }}
MAIL_APPROVE_TEMPLATE: ${{ secrets.MAIL_APPROVE_TEMPLATE }}
MAIL_VERIFY_TEMPLATE: ${{ secrets.MAIL_VERIFY_TEMPLATE }}
MAIL_APPLICATION_NOTIFICATION_TEMPLATE: ${{ secrets.MAIL_APPLICATION_NOTIFICATION_TEMPLATE }}
shell: bash

- name: ⚙️ Gradle 권한 부여
run: chmod +x gradlew

- name: ⚙️ Gradle로 빌드 실행
run: ./gradlew bootjar
# 배포에 필요한 여러 설정 파일과 프로젝트 빌드파일을 zip 파일로 모아준다.
- name: 📦 zip file 생성
run: |
mkdir deploy
cp ./docker/prod/docker-compose.prod.yml ./deploy/
cp ./docker/prod/Dockerfile ./deploy/
cp ./appspec.yml ./deploy/
cp ./scripts/deploy.sh ./deploy/
cp ./build/libs/*.jar ./deploy/
zip -r -qq -j ./spring-app.zip ./deploy

# AWS에 연결해준다.
- name: 🌎 AWS 연결
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 }}

# S3에 프로젝트를 업로드 한다.
- name: 🚛 S3에 프로젝트 업로드
run: |
aws s3 cp \
--region ap-northeast-2 \
./spring-app.zip s3://meeteam-backend-bucket
# CodeDelploy에 배포를 요청한다.
- name: 🚀 Code Deploy 배포 요청
run: aws deploy create-deployment --application-name meeteam-app
--deployment-config-name CodeDeployDefault.OneAtATime
--deployment-group-name ${{ env.DEPLOYMENT_GROUP_NAME }}
--s3-location bucket=${{ secrets.S3_BUCKET_NAME }},bundleType=zip,key=${{ env.S3_BUCKET_DIR_NAME }}/spring-app.zip

- name: Discord 알림 봇
uses: sarisia/actions-status-discord@v1
if: ${{ failure() }}
with:
title: ❗️ Backend CD failed ❗️
webhook: ${{ secrets.DISCORD_WEBHOOK }}
color: FF0000
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ replay_pid*

# End of https://www.toptal.com/developers/gitignore/api/intellij,java

*.yml
src/**/*.yml
*.sql
*.html
src/main/generated
Expand Down
File renamed without changes.
26 changes: 26 additions & 0 deletions docker/dev/docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
version: '3.8'

networks:
development_network:
driver: bridge

services:
redis:
image: "redis:alpine"
container_name: redis-dev
networks:
- development_network
expose:
- "6379"

spring-app:
build:
context: .
dockerfile: Dockerfile
container_name: dev-spring-app
environment:
- SPRING_PROFILES_ACTIVE=dev
networks:
- development_network
ports:
- "1821:8080"
18 changes: 0 additions & 18 deletions docker/docker-compose.blue.yml

This file was deleted.

14 changes: 0 additions & 14 deletions docker/docker-compose.green.yml

This file was deleted.

10 changes: 10 additions & 0 deletions docker/prod/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
### Docker 이미지를 생성할 때 기반이 되는 베이스 이미지를 설정한다.
FROM openjdk:17
### Dockerfile 내에서 사용할 변수 JAR_FILE을 정의한다.
ARG JAR_FILE=*.jar
### JAR_FILE 경로에 해당하는 파일을 Docker 이미지 내부로 복사한다.
COPY ${JAR_FILE} meeteam.jar
### CloudFront Private Key 복사
COPY private_key.pem /app/private_key.pem
### Docker 컨테이너가 시작될 때 실행할 명령을 지정한다.
ENTRYPOINT ["java","-jar","-Dspring.profiles.active=prod","-Duser.timezone=Asia/Seoul","meeteam.jar"]
44 changes: 44 additions & 0 deletions docker/prod/docker-compose.prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
version: '3.8'

networks:
production_network:
name: production_network
driver: bridge

services:
redis:
image: "redis:alpine"
container_name: redis
restart: always
networks:
- production_network
expose:
- "6379"
volumes:
- redis_data:/data

spring-app-blue:
build: .
container_name: prod-blue
networks:
- production_network
ports:
- "8081:8080"
volumes:
- /home/ubuntu/app/log:/log
depends_on:
- redis

spring-app-green:
build: .
container_name: prod-green
networks:
- production_network
ports:
- "8082:8080"
volumes:
- /home/ubuntu/app/log:/log
depends_on:
- redis
volumes:
redis_data:
Loading

0 comments on commit bafaab4

Please sign in to comment.