Skip to content

Commit

Permalink
feat: prod CI/CD 변경 (#672)
Browse files Browse the repository at this point in the history
* refactor: 로컬 스토리지 서비스로 변경

* refactor: docker 이미지 cpu 아키텍처 변경

* chore: ci/cd 스크립트 변경

* chore: 인프라 환경 변경 (ssl, 로컬 이미지서버 업로드)

* style: eol 제거

* fix: 설정 오류 해결

* chore: local health-check를 위해 80포트 활성화

* chore: local 헬스 체크를 위해 도커에 로컬 80포트 활성화

* fix: 호스트명 다시 변경

* fix: nginx 설정 변경

* chore: nginx 이미지서버 설정 변경

* refactor: 저장 로직 분리

* chore: ssl을 로드벨런서가 처리하도록 변경

* chore: 이미지 경로 환경별로 다르게 변경

* chore: dev.default.conf 추가

* chore: ci/cd dev.default.conf 설정 분리

* fix: 테스트 프로파일을 추가하여 테스트 깨짐 수정

* style: 개행추가

* chore: image dev 경로 변경

* refactor: default 이미지 Url 변경

* chore: 브랜치 변경으로 인한 ci/cd 트리거 브랜치 수정

* chore: 브랜치 변경으로 인한 ci/cd 트리거 브랜치 수정
  • Loading branch information
fromitive authored Dec 19, 2024
1 parent a1d7967 commit b9b2020
Show file tree
Hide file tree
Showing 16 changed files with 110 additions and 44 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/backend-dev-ci-cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ on:
- "backend/**"
- ".github/workflows/backend-dev-ci-cd.yml"
- "Dockerfile"
# pull_request:
# branches: [ "chongdae" ]
# paths:
# - "backend/**"
# - ".github/workflows/backend-dev-ci-cd.yml"
# - "Dockerfile"
# pull_request:
# branches: [ "develop" ]
# paths:
# - "backend/**"
# - ".github/workflows/backend-dev-ci-cd.yml"
# - "Dockerfile"

jobs:

Expand Down Expand Up @@ -80,5 +80,5 @@ jobs:
steps:
- name: Switch from old to new container
run: |
bash switch_blue_green_container.sh
bash switch_blue_green_container.sh dev.default.conf
working-directory: backend/deploy
33 changes: 14 additions & 19 deletions .github/workflows/backend-prod-ci-cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ on:
- "backend/**"
- ".github/workflows/backend-prod-ci-cd.yml"
- "Dockerfile"
# pull_request:
# branches: [ "develop" ]
# paths:
# - "backend/**"
# - ".github/workflows/backend-prod-ci-cd.yml"
# - "Dockerfile"
# pull_request:
# branches: [ "develop" ]
# paths:
# - "backend/**"
# - ".github/workflows/backend-prod-ci-cd.yml"
# - "Dockerfile"

jobs:

Expand Down Expand Up @@ -62,28 +62,23 @@ jobs:
docker push ${{ secrets.BE_DOCKERHUB_USERNAME }}/${{ secrets.BE_DOCKER_IMAGE_NAME_PROD }}:${GITHUB_SHA::7}
deploy-new-container:
needs: build-and-test
strategy:
matrix:
runner: [prod-a, prod-b]
runs-on: [ self-hosted, ${{ matrix.runner }} ]
needs: build-and-test
runs-on: [ self-hosted, prod ]
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Deploy new container on ${{ matrix.runner }}
- name: Deploy new container
run: |
bash launch_next_container.sh ${GITHUB_SHA::7} prod ${{ secrets.BE_DOCKERHUB_USERNAME }} ${{ secrets.BE_DOCKER_IMAGE_NAME_PROD }}
working-directory: backend/deploy

switch-new-container:
needs: deploy-new-container
strategy:
matrix:
runner: [prod-a, prod-b]
runs-on: [ self-hosted, ${{ matrix.runner }} ]
runs-on: [ self-hosted, prod ]

steps:
- name: Switch from old to new container on ${{ matrix.runner }}
- name: Switch from old to new container
run: |
bash switch_blue_green_container.sh
working-directory: backend/deploy
bash switch_blue_green_container.sh default.conf
working-directory: backend/deploy
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM --platform=linux/arm64 amazoncorretto:17
FROM --platform=linux/amd64 amazoncorretto:17

ENV TZ=Asia/Seoul

Expand Down
16 changes: 14 additions & 2 deletions backend/deploy/default.conf
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
upstream active_server {
server chongdae_backend:8080;
server chongdae_backend:8080;
}

server {
listen 80;
server_name chongdae.site;

location / {
proxy_pass http://active_server;
proxy_pass http://active_server;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

server {
listen 80;
server_name image.chongdae.site;

root /uploads;

location / {
try_files $uri $uri/ =404;
}
}
26 changes: 26 additions & 0 deletions backend/deploy/dev.default.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
upstream active_server {
server chongdae_backend:8080;
}

server {
listen 80;
server_name dev.chongdae.site;

location / {
proxy_pass http://active_server;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

server {
listen 80;
server_name image.dev.chongdae.site;

root /uploads;

location / {
try_files $uri $uri/ =404;
}
}
1 change: 1 addition & 0 deletions backend/deploy/launch_next_container.sh
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ docker run -d \
--network ${INITIAL_BLUE_GREEN_NETWORK_NAME} \
--name ${NEXT_CONTAINER} \
-v /logs:/logs \
-v /uploads:/uploads \
-e SPRING_PROFILES_ACTIVE=${PROFILE_ACTIVE} \
${DOCKERHUB_USER_NAME}/${DOCKER_IMAGE_NAME}:${GITHUB_SHA:0:7}

Expand Down
9 changes: 6 additions & 3 deletions backend/deploy/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ GITHUB_SHA=$1
PROFILE_ACTIVE=$2
DOCKERHUB_USER_NAME=$3
DOCKER_IMAGE_NAME=$4
NGINX_DEFAULT_CONF_NAME=$5


# parameter check
if [ -z "$GITHUB_SHA" ] || [ -z "$PROFILE_ACTIVE" ] || [ -z "$DOCKERHUB_USER_NAME" ] || [ -z "$DOCKER_IMAGE_NAME" ]; then
if [ -z "$GITHUB_SHA" ] || [ -z "$PROFILE_ACTIVE" ] || [ -z "$DOCKERHUB_USER_NAME" ] || [ -z "$DOCKER_IMAGE_NAME" ] || [ -z "$NGINX_DEFAULT_CONF_NAME" ]; then
echo "사용법: $0 <GITHUB_SHA> <PROFILE_ACTIVE> <DOCKERHUB_USER_NAME> <DOCKER_IMAGE_NAME>"
exit 1
fi

# intialize blue & gren
INITIAL_INSTANCE_NAME="chongdae_backend_green"
INITIAL_BLUE_GREEN_NETWORK_NAME="blue_green_network"
NGINX_DEFAULT_CONF_PATH="default.conf"
NGINX_NEW_CONF_PATH="new-default.conf"

# 1. ADD DOCKER NETWORK
Expand All @@ -31,6 +32,7 @@ docker run -d \
--network ${INITIAL_BLUE_GREEN_NETWORK_NAME} \
--name ${INITIAL_INSTANCE_NAME} \
-v /logs:/logs \
-v /uploads:/uploads \
-e SPRING_PROFILES_ACTIVE=${PROFILE_ACTIVE} \
${DOCKERHUB_USER_NAME}/${DOCKER_IMAGE_NAME}:${GITHUB_SHA:0:7}

Expand All @@ -39,7 +41,7 @@ if [ $? -ne 0 ]; then
fi

# 3. CHANGE INITIAL UP STREAM SERVER
cp ${NGINX_DEFAULT_CONF_PATH} ${NGINX_NEW_CONF_PATH}
cp ${NGINX_DEFAULT_CONF_NAME} ${NGINX_NEW_CONF_PATH}

if grep -q "server chongdae_backend:" "${NGINX_NEW_CONF_PATH}"; then
sed -i "s/server chongdae_backend:/server ${INITIAL_INSTANCE_NAME}:/" "${NGINX_NEW_CONF_PATH}"
Expand All @@ -51,6 +53,7 @@ docker run -d \
--network ${INITIAL_BLUE_GREEN_NETWORK_NAME} \
--name nginx \
-p 80:80 \
-v /uploads:/uploads \
nginx:latest

# 5. setup proxy in nginx container
Expand Down
10 changes: 9 additions & 1 deletion backend/deploy/switch_blue_green_container.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
#!/bin/bash
NGINX_DEFAULT_CONF=$1

# parameter check
if [ -z "$NGINX_DEFAULT_CONF" ]; then
echo "사용법: $0 <default.conf 이름>"
exit 1
fi


NGINX_CONTAINER_NAME="nginx"
BLUE_CONTAINER="chongdae_backend_blue"
GREEN_CONTAINER="chongdae_backend_green"
NGINX_DEFAULT_CONF="default.conf"
NGINX_CHANGED_DEFAULT_CONF="new-default.conf"

get_active_container() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import com.zzang.chongdae.offering.repository.entity.OfferingEntity;

public record OfferingMetaResponse(String title, String thumbnailUrl) {
private static final String DEFAULT_THUMBNAIL_URL = "https://d3a5rfnjdz82qu.cloudfront.net/chongdae-market/images/common/no-image.png";

private static final String DEFAULT_THUMBNAIL_URL = "https://image.chongdae.site/common/no-image.png";

public OfferingMetaResponse(OfferingEntity offering) {
this(offering.getTitle(), getOrDefault(offering.getThumbnailUrl()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.zzang.chongdae.storage.service.AmazonS3StorageService;
import com.zzang.chongdae.storage.service.LocalStorageService;
import com.zzang.chongdae.storage.service.StorageService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -14,7 +14,7 @@ public class StorageConfig {

@Bean
public StorageService storageService() {
return new AmazonS3StorageService(amazonS3());
return new LocalStorageService();
}

private AmazonS3 amazonS3() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,25 @@ public class LocalStorageService implements StorageService {
@Value("${storage.path}")
private String storagePath;

LocalStorageService(String redirectUrl, String storagePath) {
this.redirectUrl = redirectUrl;
this.storagePath = storagePath;
}

@Override
public String uploadFile(MultipartFile file) {
String extension = getFileExtension(file);
validateFileExtension(extension);
String newFilename = UUID.randomUUID().toString();
storeFile(file, newFilename);
return createUri(newFilename);
}

private void storeFile(MultipartFile file, String newFilename) {
try {
String extension = getFileExtension(file);
validateFileExtension(extension);
String newFilename = UUID.randomUUID() + "." + extension;
Path uploadPath = Paths.get(storagePath);
Path filePath = uploadPath.resolve(newFilename);
Files.copy(file.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);
return createUri(filePath.toString());
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
4 changes: 4 additions & 0 deletions backend/src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ security:
token:
access-secret-key: ${JWT_ACCESS_SECRET_KEY}
refresh-secret-key: ${JWT_REFRESH_SECRET_KEY}

storage:
path: /uploads
redirectUrl: image.dev.chongdae.site
4 changes: 4 additions & 0 deletions backend/src/main/resources/application-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ security:
token:
access-secret-key: ${JWT_ACCESS_SECRET_KEY}
refresh-secret-key: ${JWT_REFRESH_SECRET_KEY}

storage:
path: /uploads
redirectUrl: image.chongdae.site
4 changes: 0 additions & 4 deletions backend/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ amazon:
redirectUrl: d3a5rfnjdz82qu.cloudfront.net
storagePath: chongdae-market/images/offerings/product/

storage:
path: /uploads
redirectUrl: image.chongdae.site

security:
jwt:
token:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.test.context.ActiveProfiles;

@ActiveProfiles("test")
@SpringBootTest(webEnvironment = WebEnvironment.NONE, classes = {TestNicknameWordPickerConfig.class})
public class NicknameGeneratorTest {

Expand Down
5 changes: 5 additions & 0 deletions backend/src/test/resources/application-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ spring:
properties:
hibernate:
format_sql: true

security:
jwt:
token:
Expand All @@ -22,3 +23,7 @@ security:
fcm:
secret-key:
path: /fcm/chongdaemarket-fcm-key.json

storage:
path: /uploads
redirectUrl: fake.image.chongdae.site

0 comments on commit b9b2020

Please sign in to comment.