pipeline {
agent any
stages {
stage('clone') {
steps {
echo 'git clone'
git branch: 'master',
credentialsId: '[비밀키]',
url: 'https://lab.ssafy.com/s10-bigdata-recom-sub2/S10P22B302.git'
}
}
stage('API Server build') {
steps {
dir('back') {
sh 'docker build -t newstar_back .'
}
}
}
stage('GPU Server build') {
steps {
dir('pydata') {
sh 'docker build -t fastapi_back .'
}
}
}
stage('Web Server build') {
steps {
dir('front/newstar') {
sh 'docker build -t newstar_front .'
}
}
}
stage('Blue-Green Deploy'){
steps{
dir('exec/deploy'){
sh 'chmod +x deploy.sh'
sh './deploy.sh'
}
}
}
}
}
WebHook에 의해 파이프라인이 실행되는데 GitLab에 있는 Repo로부터 코드를 Clone 받아온다.
API, GPU, Web Server를 순으로 신버전의 도커 이미지를 빌드한다.
deploy.sh를 통해 Blue-Green 배포를 실시한다.
2.5. deploy.sh
#!/bin/bash
echo 'CI/CD Deploy Start'
cd ../exec/deploy
# Working container check
EXIST_BLUE=$(docker compose -p deploy-blue -f docker-compose.blue.yaml ps | grep Up)
if [ -z "$EXIST_BLUE" ]; then
# blue
docker compose -p deploy-blue -f docker-compose.blue.yaml up -d
BEFORE_COLOR="green"
AFTER_COLOR="blue"
BEFORE_SPRING_PORT=8081
BEFORE_REACT_PORT=3001
BEFORE_FASTAPI_PORT=8001
AFTER_SPRING_PORT=8080
AFTER_REACT_PORT=3000
AFTER_FASTAPI_PORT=8000
else
# green
docker compose -p deploy-green -f docker-compose.green.yaml up -d
BEFORE_COLOR="blue"
AFTER_COLOR="green"
BEFORE_SPRING_PORT=8080
BEFORE_REACT_PORT=3000
BEFORE_FASTAPI_PORT=8000
AFTER_SPRING_PORT=8081
AFTER_REACT_PORT=3001
AFTER_FASTAPI_PORT=8001
fi
# Spring Server health checking
for retry_count in {1..60}
do
response=$(curl -s http://172.17.0.1:${AFTER_SPRING_PORT}/api/actuator/health)
up_count=$(echo $response | grep 'UP' | wc -l)
if [ $up_count -ge 1 ]
then
echo "=========================="
echo "> Spring Server is working"
echo "=========================="
break
else
echo "> Spring Health is not working: ${response}"
fi
# about 10 minuetes
if [ $retry_count -eq 60 ]
then
echo "> Spring Server working failed"
docker compose -p deploy-${AFTER_COLOR} -f docker-compose.${AFTER_COLOR}.yaml down
exit 1;
fi
# wating 10 seconds
sleep 10
done
# Fastapi Server health checking
for retry_count in {1..60}
do
response=$(curl -s http://172.17.0.1:${AFTER_FASTAPI_PORT}/api/data/health)
up_count=$(echo $response | grep 'UP' | wc -l)
if [ $up_count -ge 1 ]
then
echo "=========================="
echo "> Fastapi Server is working"
echo "=========================="
break
else
echo "> Fastapi Health is not working: ${response}"
fi
# about 10 minuetes
if [ $retry_count -eq 60 ]
then
echo "> Fastapi Server working failed"
docker compose -p deploy-${AFTER_COLOR} -f docker-compose.${AFTER_COLOR}.yaml down
exit 1;
fi
# wating 10 seconds
sleep 20
done
echo "${AFTER_COLOR} server up(spring_port:${AFTER_SPRING_PORT}, react_port:${AFTER_REACT_PORT}, fastapi_port:${AFTER_FASTAPI_PORT})"
EXIST_AFTER=$(docker compose -p deploy-${AFTER_COLOR} -f docker-compose.${AFTER_COLOR}.yaml ps | grep Up)
if [ -n "$EXIST_AFTER" ]; then
echo "nginx Setting"
docker exec -i nginx /bin/bash -c "echo -e 'set \$spring_url http://172.17.0.1:${AFTER_SPRING_PORT};\nset \$react_url http://172.17.0.1:${AFTER_REACT_PORT};\nset \$fastapi_url http://172.17.0.1:${AFTER_FASTAPI_PORT};' | tee /etc/nginx/conf.d/service-url.inc && nginx -s reload"
echo "Completed Deploy!"
echo "$BEFORE_COLOR server down(spring_port:${BEFORE_SPRING_PORT}, react_port:${BEFORE_REACT_PORT}, fastapi_port:${BEFORE_FASTAPI_PORT})"
docker compose -p deploy-${BEFORE_COLOR} -f docker-compose.${BEFORE_COLOR}.yaml down
fi
현재 실행중인 Docker-compose의 색깔을 확인(Blue or Green)
만약, Blue일 경우 Green의 Docker-compose를 띄우고 port번호를 Green에 맞게 저장
Green일 경우 Blue의 Docker-compose를 띄우고 port번호를 Blue에 맞게 저장
각 어플리케이션 API, GPU Server의 Health Check API를 통해 요청을 날려보며 신버전의 코드가 정상적으로 운영되고 있는지 확인
만약, 정상적으로 운영되지 않으면 신버전의 서버 다운 후 스크립트 종료
신버전의 Docker-compose가 정상적으로 띄워져 있다면 service-url.inc에 있는 각 어플리케이션 port를 신버전으로 변경 후 Nginx 재시작