Skip to content

Commit

Permalink
fix: CD workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
MinhoJJang committed Aug 16, 2024
1 parent ad4d62b commit 34b7404
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions .github/workflows/nextjs-prod-CD.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,49 +46,51 @@ 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"
exit 1
fi
echo "Deployment completed successfully"
EOF
done

0 comments on commit 34b7404

Please sign in to comment.