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

Spring 환경설정 #405

Merged
merged 12 commits into from
Feb 15, 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
96 changes: 96 additions & 0 deletions .github/workflows/server-cd-spring.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
name: Server Spring CD

on:
pull_request:
branches: ["develop", "main"]
paths:
- "server/spring/**"
types:
- closed

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: "17"
distribution: "adopt"

- name: Run chmod to make gradlew executable
run: |
cd server/spring
chmod +x ./gradlew

- name: Build with Gradle
run: |
cd server/spring
./gradlew clean build

- name: Create application.yml
env:
DB_HOST_IP: ${{ secrets.SERVER_ENV_DB_HOST_IP }}
DB_PORT: ${{ secrets.SERVER_ENV_DB_PORT }}
DB_USER_NAME: ${{ secrets.SERVER_ENV_DB_USER_NAME }}
DB_PASSWORD: ${{ secrets.SERVER_ENV_DB_PASSWORD }}
DB_DATABASE_NAME: ${{ secrets.SERVER_ENV_DB_DATABASE_NAME }}
ACCESS_ID: ${{ secrets.SERVER_ENV_ACCESS_ID }}
SECRET_ACCESS_KEY: ${{ secrets.SERVER_ENV_SECRET_ACCESS_KEY }}
JWT_SECRET_KEY: ${{ secrets.SERVER_ENV_JWT_SECRET_KEY }}
GREEN_EYE_SECRET_KEY: ${{secrets.GREEN_EYE_SECRET_KEY}}
GREEN_EYE_REQUEST_URL: ${{secrets.GREEN_EYE_REQUEST_URL}}
CLOUD_FUNCTIONS_EXECUTE_URL: ${{secrets.CLOUD_FUNCTIONS_EXECUTE_URL}}
CLOUD_FUNCTIONS_REQUEST_URL: ${{secrets.CLOUD_FUNCTIONS_REQUEST_URL}}
API_GW_ACCESS_KEY: ${{secrets.API_GW_ACCESS_KEY}}
REFRESH_SECRET_KEY: ${{secrets.REFRESH_SECRET_KEY}}
REDIS_HOST_IP: ${{secrets.REDIS_HOST_IP}}
REDIS_PASSWORD: ${{secrets.REDIS_PASSWORD}}
REDIS_PORT: ${{secrets.REDIS_PORT}}
REDIS_TTL: ${{secrets.REDIS_TTL}}
run: |
cd server
touch ./application.yml
echo "DB_HOST_IP=$DB_HOST_IP" >> application.yml
echo "DB_PORT=$DB_PORT" >> application.yml
echo "DB_USER_NAME=$DB_USER_NAME" >> application.yml
echo "DB_PASSWORD=$DB_PASSWORD" >> application.yml
echo "DB_DATABASE_NAME=$DB_DATABASE_NAME" >> application.yml
echo "ACCESS_ID=$ACCESS_ID" >> application.yml
echo "SECRET_ACCESS_KEY=$SECRET_ACCESS_KEY" >> application.yml
echo "JWT_SECRET_KEY=$JWT_SECRET_KEY" >> application.yml
echo "GREEN_EYE_SECRET_KEY=$GREEN_EYE_SECRET_KEY" >> application.yml
echo "GREEN_EYE_REQUEST_URL=$GREEN_EYE_REQUEST_URL" >> application.yml
echo "CLOUD_FUNCTIONS_EXECUTE_URL=$CLOUD_FUNCTIONS_EXECUTE_URL" >> application.yml
echo "CLOUD_FUNCTIONS_REQUEST_URL=$CLOUD_FUNCTIONS_REQUEST_URL" >> application.yml
echo "API_GW_ACCESS_KEY=$API_GW_ACCESS_KEY" >> application.yml
echo "REFRESH_SECRET_KEY=$REFRESH_SECRET_KEY" >> application.yml
echo "REDIS_HOST_IP=$REDIS_HOST_IP" >> application.yml
echo "REDIS_PASSWORD=$REDIS_PASSWORD" >> application.yml
echo "REDIS_PORT=$REDIS_PORT" >> application.yml
echo "REDIS_TTL=$REDIS_TTL" >> application.yml

- name: Build Docker image
run: docker build --platform linux/amd64 ./server -t ${{ secrets.NCP_SPRING_REGISTRY }}/catchy-tape-sub:latest -f ./server/Dockerfile.spring

- name: Login NCP container registry
run: docker login ${{ secrets.NCP_SPRING_REGISTRY }} -u ${{ secrets.NCP_DOCKER_ACCESS_KEY_ID }} -p ${{ secrets.NCP_DOCKER_SECRET_KEY }}

- name: Push Docker image to registry
run: docker push ${{ secrets.NCP_SPRING_REGISTRY }}/catchy-tape-sub:latest

- name: SSH into Server
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SERVER_SSH_HOST }}
username: ${{ secrets.SERVER_SSH_USER }}
password: ${{ secrets.SERVER_SSH_PASSWORD }}
port: ${{ secrets.SERVER_SSH_PORT }}
script: |
export NCP_SPRING_REGISTRY=${{ secrets.NCP_SPRING_REGISTRY }}
export NCP_DOCKER_ACCESS_KEY_ID=${{ secrets.NCP_DOCKER_ACCESS_KEY_ID }}
export NCP_DOCKER_SECRET_KEY=${{ secrets.NCP_DOCKER_SECRET_KEY }}
sh server/deploy-spring.sh
curl -X POST -H 'Content-type: application/json' --data '{"text":"스프링 서버 배포 성공!"}' ${{ secrets.SLACK_WEBHOOK_URL }}
6 changes: 3 additions & 3 deletions .github/workflows/server-cd.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name: Server CD
name: Server Nest CD

on:
pull_request:
branches: ["develop", "main"]
paths:
- "server/**"
- "server/nest/**"
types:
- closed

Expand Down Expand Up @@ -75,7 +75,7 @@ jobs:
echo "REDIS_TTL=$REDIS_TTL" >> prod.env

- name: Build Docker image
run: docker build --platform linux/amd64 ./server -t ${{ secrets.NCP_REGISTRY }}/catchy-tape:latest
run: docker build --platform linux/amd64 ./server -t ${{ secrets.NCP_REGISTRY }}/catchy-tape:latest -f ./server/Dockerfile.nest

- name: Login NCP container registry
run: docker login ${{ secrets.NCP_REGISTRY }} -u ${{ secrets.NCP_DOCKER_ACCESS_KEY_ID }} -p ${{ secrets.NCP_DOCKER_SECRET_KEY }}
Expand Down
72 changes: 72 additions & 0 deletions .github/workflows/server-ci-spring.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
name: Backend Spring CI

on:
pull_request:
branches: [develop]
paths:
- "server/spring/**"

defaults:
run:
working-directory: ./server/spring

jobs:
BACKEND-SPRING-CI:
runs-on: ubuntu-20.04

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: 17
distribution: "adopt"

- name: Create application.yml
env:
DB_HOST_IP: ${{ secrets.SERVER_ENV_DB_HOST_IP }}
DB_PORT: ${{ secrets.SERVER_ENV_DB_PORT }}
DB_USER_NAME: ${{ secrets.SERVER_ENV_DB_USER_NAME }}
DB_PASSWORD: ${{ secrets.SERVER_ENV_DB_PASSWORD }}
DB_DATABASE_NAME: ${{ secrets.SERVER_ENV_DB_DATABASE_NAME }}
ACCESS_ID: ${{ secrets.SERVER_ENV_ACCESS_ID }}
SECRET_ACCESS_KEY: ${{ secrets.SERVER_ENV_SECRET_ACCESS_KEY }}
JWT_SECRET_KEY: ${{ secrets.SERVER_ENV_JWT_SECRET_KEY }}
GREEN_EYE_SECRET_KEY: ${{secrets.GREEN_EYE_SECRET_KEY}}
GREEN_EYE_REQUEST_URL: ${{secrets.GREEN_EYE_REQUEST_URL}}
CLOUD_FUNCTIONS_EXECUTE_URL: ${{secrets.CLOUD_FUNCTIONS_EXECUTE_URL}}
CLOUD_FUNCTIONS_REQUEST_URL: ${{secrets.CLOUD_FUNCTIONS_REQUEST_URL}}
API_GW_ACCESS_KEY: ${{secrets.API_GW_ACCESS_KEY}}
REFRESH_SECRET_KEY: ${{secrets.REFRESH_SECRET_KEY}}
REDIS_HOST_IP: ${{secrets.REDIS_HOST_IP}}
REDIS_PASSWORD: ${{secrets.REDIS_PASSWORD}}
REDIS_PORT: ${{secrets.REDIS_PORT}}
REDIS_TTL: ${{secrets.REDIS_TTL}}
run: |
touch application.yml
echo "DB_HOST_IP=$DB_HOST_IP" >> application.yml
echo "DB_PORT=$DB_PORT" >> application.yml
echo "DB_USER_NAME=$DB_USER_NAME" >> application.yml
echo "DB_PASSWORD=$DB_PASSWORD" >> application.yml
echo "DB_DATABASE_NAME=$DB_DATABASE_NAME" >> application.yml
echo "ACCESS_ID=$ACCESS_ID" >> application.yml
echo "SECRET_ACCESS_KEY=$SECRET_ACCESS_KEY" >> application.yml
echo "JWT_SECRET_KEY=$JWT_SECRET_KEY" >> application.yml
echo "GREEN_EYE_SECRET_KEY=$GREEN_EYE_SECRET_KEY" >> application.yml
echo "GREEN_EYE_REQUEST_URL=$GREEN_EYE_REQUEST_URL" >> application.yml
echo "CLOUD_FUNCTIONS_EXECUTE_URL=$CLOUD_FUNCTIONS_EXECUTE_URL" >> application.yml
echo "CLOUD_FUNCTIONS_REQUEST_URL=$CLOUD_FUNCTIONS_REQUEST_URL" >> application.yml
echo "API_GW_ACCESS_KEY=$API_GW_ACCESS_KEY" >> application.yml
echo "REFRESH_SECRET_KEY=$REFRESH_SECRET_KEY" >> application.yml
echo "REDIS_HOST_IP=$REDIS_HOST_IP" >> application.yml
echo "REDIS_PASSWORD=$REDIS_PASSWORD" >> application.yml
echo "REDIS_PORT=$REDIS_PORT" >> application.yml
echo "REDIS_TTL=$REDIS_TTL" >> application.yml

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

- name: Test with Gradle
run: ./gradlew --info test
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
name: Backend Dev CI
name: Backend Nest CI

on:
pull_request:
branches: [develop]
paths:
- "server/**"
- "server/nest/**"

defaults:
run:
working-directory: ./server
working-directory: ./server/nest

jobs:
BACKEND-CI:
Expand Down
7 changes: 4 additions & 3 deletions server/.dockerignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.git
Dockerfile
node_modules
dist
Dockerfile.nest
Dockerfile.spring
nest/node_modules
nest/dist
6 changes: 3 additions & 3 deletions server/.gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# compiled output
/dist
/node_modules
/nest/dist
/nest/node_modules

# Logs
logs
/nest/logs
*.log
npm-debug.log*
pnpm-debug.log*
Expand Down
2 changes: 1 addition & 1 deletion server/Dockerfile → server/Dockerfile.nest
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ENV TZ=Asia/Seoul

WORKDIR /catchy-tape

COPY . .
COPY nest .

RUN npm install
RUN npm run build
Expand Down
17 changes: 17 additions & 0 deletions server/Dockerfile.spring
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM openjdk:17-alpine

ENV TZ=Asia/Seoul

WORKDIR /catchy-tape-spring

# 인자 설정 - JAR_File
ARG JAR_FILE=spring/build/libs/*.jar

# jar 파일 복제
COPY ${JAR_FILE} app.jar

# 해당 포트를 외부로 개방
EXPOSE 8080

# 실행 명령어
ENTRYPOINT ["java", "-jar", "app.jar"]
48 changes: 48 additions & 0 deletions server/deploy-spring.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/sh


# blue 컨테이너가 띄워져 있는지 확인 (8080 포트 == blue)
EXIST_BLUE=$(netstat -lntp | grep 8080)

if [ -n "$EXIST_BLUE" ]; then
TARGET_COLOR="green"
NOW_COLOR="blue"
WEB_SERVER_TARGET_PORT=8081
WEB_SERVER_STOP_PORT=8080
TARGET_SERVER_NAME="catchy-tape-sub-green"
NOW_SERVER_NAME="catchy-tape-sub-blue"
else
TARGET_COLOR="blue"
NOW_COLOR="green"
WEB_SERVER_TARGET_PORT=8080
WEB_SERVER_STOP_PORT=8081
TARGET_SERVER_NAME="catchy-tape-sub-blue"
NOW_SERVER_NAME="catchy-tape-sub-green"
fi

# 1 새로운 서버를 띄운다
docker login $NCP_SPRING_REGISTRY -u $NCP_DOCKER_ACCESS_KEY_ID -p $NCP_DOCKER_SECRET_KEY
docker pull $NCP_SPRING_REGISTRY/catchy-tape-sub:latest
docker run -v /home/hyung/logs:/catchy-tape-sub/logs -d -p $WEB_SERVER_TARGET_PORT:8080 --name $TARGET_SERVER_NAME $NCP_SPRING_REGISTRY/catchy-tape-sub:latest

# 2 nginx 포트 설정 변경 후 reload
NGINX_CONFIG="/etc/nginx/nginx.conf"
sed -i "s/$WEB_SERVER_STOP_PORT/$WEB_SERVER_TARGET_PORT/" $NGINX_CONFIG
nginx -s reload

# 3 원래 서버 종료 & 삭제
FIRST_DEPLOYED_SERVER="catchy-tape-sub"
FIRST_DEPLOYED_SERVER_ID=$(docker ps --filter "name=$FIRST_DEPLOYED_SERVER" -q)
if [ -n "$FIRST_DEPLOYED_SERVER_ID" ]; then
docker stop $FIRST_DEPLOYED_SERVER
docker rm $FIRST_DEPLOYED_SERVER
fi

STOP_SERVER_ID=$(docker ps --filter "name=$NOW_SERVER_NAME" -q)
if [ -n "$STOP_SERVER_ID" ]; then
docker stop $STOP_SERVER_ID
docker rm $NOW_SERVER_NAME
fi

# 사용하지 않는 도커 이미지 정리
docker image prune -a -f
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import axios from 'axios';
import * as fs from 'fs';
import { Injectable, Logger } from '@nestjs/common';
import { HTTP_STATUS_CODE } from 'src/codes/httpStatusCode.enum';
import { NcloudConfigService } from './../config/ncloud.config';
import { NcloudConfigService } from '../config/ncloud.config';
import { S3 } from 'aws-sdk';
import { contentTypeHandler, keyFlags } from './../constants';
import { contentTypeHandler, keyFlags } from '../constants';
import { CatchyException } from 'src/config/catchyException';
import { ERROR_CODE } from 'src/codes/errorCode.enum';
import { Readable } from 'stream';
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { InjectRepository } from '@nestjs/typeorm';
import { CatchyException } from 'src/config/catchyException';
import { ERROR_CODE } from 'src/codes/errorCode.enum';
import { Recent_Played } from 'src/entity/recent_played.entity';
import { UserUpdateDto } from './../dto/userUpdate.dto';
import { UserUpdateDto } from '../dto/userUpdate.dto';
import { MusicRepository } from 'src/repository/music.repository';
import { UserRepository } from '../repository/user.repository';
import { Recent_PlayedRepository } from 'src/repository/recent_played.repository';
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes
4 changes: 4 additions & 0 deletions server/nest/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "./tsconfig.json",
"exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
}
File renamed without changes.
37 changes: 37 additions & 0 deletions server/spring/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
HELP.md
.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
out/
!**/src/main/**/out/
!**/src/test/**/out/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

### VS Code ###
.vscode/
Loading
Loading