Skip to content

Commit

Permalink
ci: deploy.sh 코드에 nginx reload 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingyum-Kim committed Aug 5, 2023
1 parent 35460b2 commit 0a52500
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 79 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ jobs:
--application-name dnd-9th-1-codedeploy \
--deployment-config-name CodeDeployDefault.OneAtATime \
--deployment-group-name dnd-9th-1-deployment-group \
--s3-location bucket=dnd-9th-1-s3,bundleType=zip,key=dnd-9th-1-backend.zip
--s3-location bucket=dnd-9th-1-s3,bundleType=zip,key=dnd-9th-1-backend.zip
6 changes: 0 additions & 6 deletions appspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,3 @@ hooks:
timeout: 180
# CodeDeploy 중 실행되는 스크립트 또는 명령을 실행할 사용자를 지정
runas: ubuntu
- location: health_check.sh
timeout: 180
runas: ubuntu
- location: switch.sh
timeout: 180
runas: ubuntu
8 changes: 8 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
### Docker 이미지를 생성할 때 기반이 되는 베이스 이미지를 설정한다.
FROM openjdk:17-alpine
### Dockerfile 내에서 사용할 변수 JAR_FILE을 정의한다.
ARG JAR_FILE=build/libs/*.jar
### JAR_FILE 경로에 해당하는 파일을 Docker 이미지 내부로 복사한다.
COPY ${JAR_FILE} dnd-9th-1-backend.jar
### Docker 컨테이너가 시작될 때 실행할 명령을 지정한다.
ENTRYPOINT ["java","-jar","-Dspring.profiles.active=prod", "dnd-9th-1-backend.jar"]
11 changes: 11 additions & 0 deletions docker/docker-compose.blue.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: '3'
services:
blue: # service_name
build: ./ # Dockerfile path
image: ${DOCKER_REGISTRY}/${DOCKER_APP_NAME}:${IMAGE_TAG}
container_name: ${DOCKER_APP_NAME}-blue #
environment:
- LANG=ko_KR.UTF-8
-
ports:
- '8081:80'
10 changes: 10 additions & 0 deletions docker/docker-compose.green.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: '3'
services:
green:
build: ./
image:
container_name: ${DOCKER_APP_NAME}-green
environment:
- LANG=ko_KR.UTF-8
ports:
- '8082:80'
48 changes: 27 additions & 21 deletions scripts/deploy.sh
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
#!/bin/bash

CURRENT_PORT=$(cat /home/ubuntu/service_url.inc | grep -Po '[0-9]+' | tail -1)
TARGET_PORT=0
cd /home/ubuntu/app
DOCKER_APP_NAME=milestone

echo "> ${CURRENT_PORT} port currently proxying by nginx."
EXIST_BLUE=$(sudo docker-compose -p ${DOCKER_APP_NAME}-blue -f docker-compose.blue.yml ps | grep Up)

if[ ${CURRENT_PORT} -eq 8081]; then
TARGET_PORT=8082;
elif [ ${CURRENT_PORT} -eq 8082]; then
TARGET_PORT=8081;
else
echo "> Currently Running WAS does not exist."
#Switching
if[ -z "$EXIST_BLUE"]; then # Blue isn't working
echo "> blue up"
docker-compose -p ${DOCKER_APP_NAME}-blue -f docker-compose.blue.yml up -d --build
BEFORE_COMPOSE_COLOR="green"
AFTER_COMPOSE_COLOR="blue"
else # Blue is working
echo "> green up"
docdker-compose -p ${DOCKER_APP_NAME}-green -f docker-compose.green.yml up -d --build
BEFORE_COMPOSE_COLOR="blue"
AFTER_COMPOSE_COLOR="green"
fi

TARGET_PID=$(lsof -Fp -i TCP:${TARGET_PORT} | grep -Po 'p[0-9]+' | grep -Po '[0-9]+')
sleep 10

if[ ! -z ${TARGET_PID}]; then
echo "> Exiting WAS for port ${TARGET_PORT}."
sudo kill ${TARGET_PID}
fi
EXIST_AFTER=$(docker-compose -p ${DOCKER_APP_NAME}-${AFTER_COMPOSE_COLOR} -f docker-compose.${AFTER_COMPOSE_COLOR}.yml ps | grep UP)

if[ -n "$EXIST_AFTER"]; then
echo "> nginx reload .. "
NGINX_CONT=$(docker ps -q --filter ancestor=nginx)
docker exec -it "$NGINX_CONT" bash -c "cp /etc/nginx/nginx.${AFTER_COMPOSE_COLOR}.conf /etc/nginx/nginx.conf;nginx -s reload"

source ~/.bash_profile
docker-compose -f docker-compose.${BEFORE_COMPOSE_COLOR}.yml down
docker rm $(docker ps -q -a -f "name=${BEFORE_COMPOSE_COLOR}")
echo "$BEFORE_COMPOSE_COLOR down"
fi

nohop java -jar \
-Dspring.profiles.active=prod \
-Dserver.port=${TARGET_PORT} \
/home/ubuntu/app/build/libs/*.SNAPSHOT.jar > /home/ubuntu/nohup.out 2>&1 &
echo "> A new WAS for ${TARGET_PORT} is running."
exit 0
echo "Start removing existing image"
docker rmi $(docker images -f "dangling=true" -q)
29 changes: 0 additions & 29 deletions scripts/health_check.sh

This file was deleted.

22 changes: 0 additions & 22 deletions scripts/switch.sh

This file was deleted.

0 comments on commit 0a52500

Please sign in to comment.