Skip to content
Merged
17 changes: 6 additions & 11 deletions .github/workflows/cd-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ jobs:
--port 22 \
--cidr ${{ steps.ip.outputs.ipv4 }}/32

- name: ✉️ Send docker-compose.yml
- name: ✉️ Send docker-compose.yml & deploy.sh
uses: appleboy/scp-action@master
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.EC2_KEY }}
source: "./docker-compose.yml"
source: "./docker-compose.yml, ./deploy.sh"
target: "/home/ec2-user/findyou/"

- name: 🚀 deploy to server
Expand All @@ -89,16 +89,11 @@ jobs:
echo "🗂️ Change Directory to EC2 Root"
cd /home/ec2-user/findyou

echo "✋🏻Stopping existing container and Cleaning up old images"
sudo docker compose down --rmi all
echo "👉🏻 Grant Permission to deploy.sh"
chmod +x ./deploy.sh

sudo docker ps -a

echo "🥳 Pulling new image"
sudo docker pull ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_IMAGE }}

echo "🌱 Starting new container"
sudo docker compose up -d
echo "✋🏻 Stopping Existing Container and Deploy New Container"
./deploy.sh

- name: ❌ Remove GitHub Actions IP
run: |
Expand Down
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ dependencies {
// Security
implementation("org.springframework.boot:spring-boot-starter-security")
testImplementation("org.springframework.security:spring-security-test")

// Actuator
implementation 'org.springframework.boot:spring-boot-starter-actuator'
}

tasks.named('test') {
Expand Down
71 changes: 71 additions & 0 deletions deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/bin/bash

IS_BLUE_RUNNING=$(docker ps | grep findyou_blue)
export NGINX_CONF="/etc/nginx/nginx.conf"

# blue 가 실행 중이면 green 을 up
if [ -n "$IS_BLUE_RUNNING" ]; then
echo "### BLUE => GREEN ####"

# 최신 이미지 강제 가져오기
echo ">>> 최신 green 이미지 가져오기"
docker compose pull findyou_green

echo ">>> green 컨테이너 실행 (기존 이미지 무시)"
docker compose up -d --force-recreate findyou_green
sleep 7

echo ">>> health check 진행..."
while true; do
RESPONSE=$(curl http://localhost:9002/actuator/health | grep UP)
if [ -n "$RESPONSE" ]; then
echo ">>> green health check 성공! "
break;
fi
sleep 3
done;

echo ">>> Nginx 설정 변경 (green)"
sudo sed -i 's/set $ACTIVE_APP findyou_blue;/set $ACTIVE_APP findyou_green;/' $NGINX_CONF
sudo nginx -s reload

echo ">>> blue 컨테이너 종료"
docker compose stop findyou_blue

# green 이 실행 중이면 blue 를 up
else
echo "### GREEN => BLUE ####"

# 최신 이미지 강제 가져오기
echo ">>> 최신 blue 이미지 가져오기"
docker compose pull findyou_blue

echo ">>> blue 컨테이너 실행 (기존 이미지 무시)"
docker compose up -d --force-recreate findyou_blue
sleep 7

echo ">>> health check 진행..."
while true; do
RESPONSE=$(curl http://localhost:9001/actuator/health | grep UP)
if [ -n "$RESPONSE" ]; then
echo ">>> blue health check 성공! "
break;
fi
sleep 3
done;

echo ">>> Nginx 설정 변경 (blue)"
sudo sed -i 's/set $ACTIVE_APP findyou_green;/set $ACTIVE_APP findyou_blue;/' $NGINX_CONF
sudo nginx -s reload

echo ">>> green 컨테이너 종료"
docker compose stop findyou_green
fi

echo ">>> 종료된 컨테이너들 정리"
docker container prune -f

echo ">>> 사용하지 않는 도커 이미지들 정리"
docker image prune -a -f # 모든 사용하지 않는 이미지 삭제


23 changes: 22 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
services:
findyou:
findyou_blue:
image: ksg1227/findyou:latest
env_file:
- .env
Expand All @@ -12,6 +12,27 @@ services:
- AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}
- AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}
- SERVICE_KEY=${SERVICE_KEY}
- CACHE_ENDPOINT=${CACHE_ENDPOINT}
- JWT_SECRET_KEY=${JWT_SECRET_KEY}
- TZ=Asia/Seoul
volumes:
- ${LOG_DIRECTORY}:/logs

findyou_green:
image: ksg1227/findyou:latest
env_file:
- .env
ports:
- "9002:9001"
environment:
- DEV_DATASOURCE_URL=${DEV_DATASOURCE_URL}
- DEV_DATASOURCE_USERNAME=${DEV_DATASOURCE_USERNAME}
- DEV_DATASOURCE_PASSWORD=${DEV_DATASOURCE_PASSWORD}
- AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}
- AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}
- SERVICE_KEY=${SERVICE_KEY}
- CACHE_ENDPOINT=${CACHE_ENDPOINT}
- JWT_SECRET_KEY=${JWT_SECRET_KEY}
- TZ=Asia/Seoul
volumes:
- ${LOG_DIRECTORY}:/logs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class SecurityConfig {

private static final String[] PERMIT_URL = {
LOGIN_ENDPOINT.getValue(), "api/v1/auth/signup", "/swagger-ui/**", "/api-docs", "/swagger-ui-custom.html",
"/v3/api-docs/**", "/api-docs/**", "/swagger-ui.html", "/swagger-ui/index.html"
"/v3/api-docs/**", "/api-docs/**", "/swagger-ui.html", "/swagger-ui/index.html", "/actuator/health"
};

@Bean
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ spring:
web:
resources:
add-mappings: false
management:
endpoints:
web:
exposure:
include: "health, info"
---
# 로컬에서 사용하는 DB
spring:
Expand Down
Loading