From 34b7404a3ee8d5acee9db9b593a0e6ce85cd438f Mon Sep 17 00:00:00 2001 From: MinhoJJang Date: Fri, 16 Aug 2024 13:34:34 +0900 Subject: [PATCH] fix: CD workflow --- .github/workflows/nextjs-prod-CD.yml | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/.github/workflows/nextjs-prod-CD.yml b/.github/workflows/nextjs-prod-CD.yml index 9c839bef..89f56d3f 100644 --- a/.github/workflows/nextjs-prod-CD.yml +++ b/.github/workflows/nextjs-prod-CD.yml @@ -46,42 +46,46 @@ jobs: chmod 600 ~/.ssh/id_rsa IFS=',' read -r -a HOSTS <<< "${{ secrets.WEB_IPS }}" + KCR_REGISTRY="${{ env.KCR_REGISTRY }}" + KCR_REPOSITORY="${{ env.KCR_REPOSITORY }}" + VERSION="${{ steps.get_version.outputs.version }}" for host in "${HOSTS[@]}" do echo "Deploying to $host" - ssh -i ~/.ssh/id_rsa -o StrictHostKeyChecking=no ${{ secrets.USER }}@$host << 'EOF' + ssh -i ~/.ssh/id_rsa -o StrictHostKeyChecking=no ${{ secrets.USER }}@$host << EOF set -e echo "Stopping and removing existing containers" - docker ps --format '{{.ID}} {{.Image}}' | grep 'dkation.kr-central-2.kcr.dev/dkation-prod-front/dkation-prod-fe' | awk '{print $1}' | xargs -r docker stop && \ - docker ps -a --format '{{.ID}} {{.Image}}' | grep 'dkation.kr-central-2.kcr.dev/dkation-prod-front/dkation-prod-fe' | awk '{print $1}' | xargs -r docker rm && \ - + docker ps --format '{{.ID}} {{.Image}}' | grep '$KCR_REGISTRY/$KCR_REPOSITORY' | awk '{print \$1}' | xargs -r docker stop + docker ps -a --format '{{.ID}} {{.Image}}' | grep '$KCR_REGISTRY/$KCR_REPOSITORY' | awk '{print \$1}' | xargs -r docker rm + echo "Removing old Docker images" - docker images --format '{{.Repository}}:{{.Tag}}' | grep 'dkation.kr-central-2.kcr.dev/dkation-prod-front/dkation-prod-fe' | xargs -r docker rmi + docker images --format '{{.Repository}}:{{.Tag}}' | grep '$KCR_REGISTRY/$KCR_REPOSITORY' | xargs -r docker rmi echo "Pulling new Docker image" - docker pull $KCR_REGISTRY/$KCR_REPOSITORY:${{ steps.get_version.outputs.version }} + docker pull $KCR_REGISTRY/$KCR_REPOSITORY:$VERSION echo "Running new Docker container" - docker run -d -p 80:3000 --name dkation-frontend $KCR_REGISTRY/$KCR_REPOSITORY:${{ steps.get_version.outputs.version }} + docker run -d -p 80:3000 --name dkation-frontend $KCR_REGISTRY/$KCR_REPOSITORY:$VERSION echo "Checking container health" max_retries=5 retries=0 - until docker ps | grep frontend | grep -q "Up" || [ $retries -eq $max_retries ] + until docker ps | grep dkation-frontend | grep -q "Up" || [ \$retries -eq \$max_retries ] do echo "Waiting for container to be healthy..." sleep 5 - retries=$((retries+1)) + retries=\$((retries+1)) done - if [ $retries -eq $max_retries ]; then + if [ \$retries -eq \$max_retries ]; then echo "Container failed to start properly" exit 1 fi echo "Container is up and running" + EOF if [ $? -ne 0 ]; then echo "Deployment to $host failed" @@ -89,6 +93,4 @@ jobs: fi echo "Deployment completed successfully" - - EOF done \ No newline at end of file