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

[KAN-3] 백엔드 무중단배포 cd 세팅 #2

Merged
merged 4 commits into from
Mar 26, 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
88 changes: 88 additions & 0 deletions .github/workflows/deploy-api-to-prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
name: Deploy API To Prod

on:
pull_request:
workflow_dispatch:
push:
branches:
- main

jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set Up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'adopt'
- name: Grant Execute Permission For Gradlew
run: chmod +x gradlew
- name: Make Prod1 Yml
run: |
cd ./src/main/resources
touch ./application-prod1.yml
echo "${{ secrets.PROPERTIES_PROD1 }}" > ./application-prod1.yml
shell: bash
- name: Make Prod2 Yml
run: |
cd ./src/main/resources
touch ./application-prod2.yml
echo "${{ secrets.PROPERTIES_PROD2 }}" > ./application-prod2.yml
- name: Build With Gradle
run: ./gradlew :bootJar -x test
- name: Docker Build And Push
run: |
docker login -u ${{ secrets.USERNAME }} -p ${{ secrets.PASSWORD }}
docker build -f docker/api.Dockerfile -t skku-api .
docker tag skku-api sinkyoungdeok/skku-api
docker push sinkyoungdeok/skku-api
- name: Deploy Prod
uses: appleboy/[email protected]
with:
key: ${{ secrets.SSH_KEY }}
host: ${{ secrets.HOST_NAME }}
username: ubuntu
port: 22
script: |
docker login -u ${{ secrets.USERNAME }} -p ${{ secrets.PASSWORD }}
docker pull sinkyoungdeok/skku-api

echo "> 구동중인 Profile 확인"
CURRENT_PROFILE=$(curl -s http://localhost/profile)
echo "> $CURRENT_PROFILE"

echo "> profile setting"
if [ "$CURRENT_PROFILE" == "prod1" ]
then
IDLE_PROFILE=prod2
IDLE_PORT=8082
elif [ "$CURRENT_PROFILE" == "prod2" ]
then
IDLE_PROFILE=prod1
IDLE_PORT=8081
else
echo "> 일치하는 Profile이 없음."
IDLE_PROFILE=prod1
IDLE_PORT=8081
fi

echo "> 배포"
if [ "$IDLE_PROFILE" == "prod1" ]
then
docker-compose build skku-api-1
docker-compose up -d skku-api-1
else
docker-compose build skku-api-2
docker-compose up -d skku-api-2
fi

sleep 60

echo "> Change Nginx Proxy Port"
echo "set \$service_url http://127.0.0.1:${IDLE_PORT};" |sudo tee /etc/nginx/conf.d/service-url.inc


echo "> Nginx Reload"
sudo service nginx reload
4 changes: 4 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@ tasks.withType<KotlinCompile> {
tasks.withType<Test> {
useJUnitPlatform()
}

tasks.jar { enabled = false }

tasks.bootJar { enabled = true }
27 changes: 27 additions & 0 deletions config/nginx/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
worker_processes auto;

events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;
sendfile on;

server {
listen 80;
server_name example.com;

include /etc/nginx/default.d/*.conf;

include /etc/nginx/conf.d/service-url.inc;

location / {
proxy_pass $service_url;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
}
}
}
1 change: 1 addition & 0 deletions config/nginx/service-url.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
set $service_url http://127.0.0.1:8081;
4 changes: 4 additions & 0 deletions docker/api.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM openjdk:17-jdk-slim
ARG JAR_FILE=build/libs/*SNAPSHOT.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-Dspring.profiles.active=${USE_PROFILE}","-Dio.netty.tryReflectionSetAccessible=true","--add-opens","java.base/jdk.internal.misc=ALL-UNNAMED","-jar","/app.jar"]
47 changes: 47 additions & 0 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
version: "3.7"
services:
skku-api-1:
depends_on:
- skku-db
image: sinkyoungdeok/skku-api
ports:
- "8081:8081"
environment:
- USE_PROFILE=prod1
restart: always
skku-api-2:
depends_on:
- skku-db
image: sinkyoungdeok/skku-api
ports:
- "8082:8082"
environment:
- USE_PROFILE=prod2
restart: always
skku-db:
image: mysql:8.0
platform: linux/amd64
ports:
- "3306:3306"
environment:
- MYSQL_DATABASE=skku
- MYSQL_ROOT_PASSWORD=skku-be
- MYSQL_USER=skku-user
- MYSQL_PASSWORD=skku-pw
- TZ=UTC
command:
- --character-set-server=utf8mb4
- --collation-server=utf8mb4_unicode_ci
- --lower_case_table_names=1
- --default-authentication-plugin=mysql_native_password
volumes:
- ./mysql:/var/lib/mysql
skku-redis:
image: redis:6.0.2
command: redis-server /usr/local/etc/redis/redis.conf --port 6379
ports:
- 6379:6379
platform: linux/amd64
volumes:
- ./redis/redis.conf:/usr/local/etc/redis/redis.conf:rw
- ./redis/users.acl:/usr/local/etc/redis/users.acl:rw
16 changes: 16 additions & 0 deletions src/main/kotlin/com/restaurant/be/WebRestController.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.restaurant.be

import org.springframework.core.env.Environment
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController

@RestController
class WebRestController(
private val env: Environment,
) {

@GetMapping("/profile")
fun getProfile(): String {
return env.activeProfiles.firstOrNull() ?: "No Active Profile"
}
}
3 changes: 3 additions & 0 deletions src/main/resources/application-prod1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
server:
port: 8081

2 changes: 2 additions & 0 deletions src/main/resources/application-prod2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
server:
port: 8082
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
server:
port: 8080

spring:
application:
name: be
Expand Down
Loading