-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
19 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,69 +45,27 @@ jobs: | |
push: true | ||
tags: ${{ secrets.DOCKER_USERNAME }}/blog-backend:${{ github.sha }} | ||
|
||
deploy: | ||
gitops-update: | ||
needs: docker-build | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: 블루 그린 배포 | ||
uses: appleboy/ssh-action@master | ||
- name: GitOps 레포지토리 클론 | ||
uses: actions/checkout@v4 | ||
with: | ||
host: ${{ secrets.NCP_HOST }} | ||
username: ${{ secrets.NCP_USERNAME }} | ||
password: ${{ secrets.NCP_PASSWORD }} | ||
port: ${{ secrets.NCP_PORT }} | ||
script: | | ||
sudo docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} | ||
# 현재 실행 중인 컨테이너 확인 | ||
CURRENT_PORT=$(sudo docker ps --format "{{.Ports}}" | grep -oP '(\d+)->7777' | grep -oP '^\d+' | sort -u) | ||
if [ "${CURRENT_PORT}" == "7776" ]; then | ||
NEW_PORT=7777 | ||
echo "현재 실행중인 포트는 7776 이고 7777로 실행할거임" | ||
else | ||
NEW_PORT=7776 | ||
echo "현재 실행중인 포트는 7777 이고 7776로 실행할거임" | ||
fi | ||
# 새로운 이미지로 컨테이너 실행 | ||
sudo docker run -d --name blog-backend-${NEW_PORT} -p ${NEW_PORT}:7777 --network my-local-network \ | ||
-v ~/blog/application.yml:/app/config/application.yml \ | ||
jinia91/blog-backend:${{ github.sha }} java -jar blog-backend.jar --spring.config.location=file:/app/config/application.yml | ||
# 새로운 컨테이너의 Health Check | ||
success=false | ||
for i in {1..100}; do | ||
if curl -s http://localhost:${NEW_PORT}/actuator/health | grep 'UP'; then | ||
echo "새로운 인스턴스를 ${NEW_PORT} 에서 실행하고 헬스 체크 완료" | ||
success=true | ||
break | ||
fi | ||
echo "새로운 인스턴스 ${NEW_PORT} 헬스체크 기다리는중" | ||
sleep 3 | ||
done | ||
if [ "$success" = true ]; then | ||
echo "배포 성공. NGINX 리로드 및 이전 버전 리소스 정리 중..." | ||
echo "NGINX 설정 변경 중: 포트 ${CURRENT_PORT}에서 ${NEW_PORT}로 변경" | ||
sudo sed -i "s/localhost:${CURRENT_PORT}/localhost:${NEW_PORT}/" /etc/nginx/sites-available/default | ||
sudo nginx -s reload | ||
# 이전 컨테이너 중지 및 제거 | ||
PREVIOUS_CONTAINER="blog-backend-${CURRENT_PORT}" | ||
sudo docker stop ${PREVIOUS_CONTAINER} | ||
sudo docker rm ${PREVIOUS_CONTAINER} | ||
CURRENT_IMAGE=$(sudo docker images jinia91/blog-backend:${{ github.sha }} --format "{{.ID}}") | ||
PREVIOUS_IMAGE=$(sudo docker images jinia91/blog-backend --format "{{.ID}}" | sed -n '2p') | ||
sudo docker images --format "{{.Repository}}:{{.Tag}} {{.ID}}" | grep 'jinia91/blog-backend' | grep -v "${CURRENT_IMAGE}\|${PREVIOUS_IMAGE}" | awk '{print $2}' | xargs -r sudo docker rmi -f | ||
else | ||
echo "배포 실패. 새로운 컨테이너 제거 및 기존 상태 유지 중..." | ||
sudo docker stop ${CONTAINER_NAME} | ||
sudo docker rm ${CONTAINER_NAME} | ||
exit 1 | ||
fi | ||
# 도커 이미지 청소 | ||
sudo docker image prune -f | ||
repository: jinia91/blog-gitops | ||
path: gitops-repo | ||
|
||
- name: 배포 태그 업데이트 | ||
working-directory: ./gitops-repo | ||
run: | | ||
yq -i '.spec.template.spec.containers[0].image = "jinia91/blog-backend:${{ github.sha }}"' deploy/jiniaslog/monolith/jiniaslog-main.yml | ||
- name: GitOps 레포지토리 커밋 및 푸시 | ||
working-directory: ./gitops-repo | ||
run: | | ||
git config --global user.name "GitOps Bot" | ||
git config --global user.email "[email protected]" | ||
git add . | ||
git commit -m "업데이트 blog-backend:${{ github.sha }}" | ||
git push |