Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 20 additions & 17 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,27 @@ jobs:

- name: Deploy with Blue-Green
run: |
ssh ec2 <<'EOF'
ssh ec2 "DOCKER_HUB_USERNAME=${{ secrets.DOCKER_HUB_USERNAME }} bash -s" <<'EOF'

echo "📦 Pulling latest image..."
docker pull ${{ secrets.DOCKER_HUB_USERNAME }}/commit-api:latest
docker pull $DOCKER_HUB_USERNAME/commit-api:latest

echo "🔍 Checking current active environment..."
CURRENT=$(readlink -f /opt/app/nginx/default.conf)
if echo "$CURRENT" | grep -q "default-blue.conf"; then
if docker exec nginx-proxy test -f /etc/nginx/conf.d/default.conf; then
CURRENT=$(docker exec nginx-proxy readlink -f /etc/nginx/conf.d/default.conf | xargs basename)
else
echo "❌ Cannot determine current config. Exiting."
exit 1
fi

if echo "$CURRENT" | grep -q "blue"; then
TARGET_COLOR=green
TARGET_PORT=3001
TARGET_CONF=/opt/app/nginx/default-green.conf
TARGET_CONF=/etc/nginx/conf.d/default-green.conf.disabled
else
TARGET_COLOR=blue
TARGET_PORT=3000
TARGET_CONF=/opt/app/nginx/default-blue.conf
TARGET_CONF=/etc/nginx/conf.d/default-blue.conf.disabled
fi

echo "🚀 Deploying to $TARGET_COLOR container on port $TARGET_PORT..."
Expand All @@ -60,27 +67,22 @@ jobs:
-p $TARGET_PORT:3000 \
--network=commit-networks \
-v /opt/app/config/service-account-key.json:/app/config/service-account-key.json:ro \
${{ secrets.DOCKER_HUB_USERNAME }}/commit-api:latest
$DOCKER_HUB_USERNAME/commit-api:latest

echo "⏳ Health check for $TARGET_COLOR..."
for i in {1..10}; do
sleep 2
if curl -s http://localhost:$TARGET_PORT/health | grep "ok" > /dev/null; then
echo "✅ Health check passed. Switching traffic..."

# 심볼릭 링크 변경
ln -sf $TARGET_CONF /opt/app/nginx/default.conf
echo "🔁 Linked $TARGET_CONF to default.conf"

# 현재 심볼릭 링크가 실제로 가리키는 파일 확인
ACTUAL_LINK=$(readlink -f /opt/app/nginx/default.conf)
echo "📌 Current nginx config link points to: $ACTUAL_LINK"
# Switch nginx config inside container
docker exec nginx-proxy cp $TARGET_CONF /etc/nginx/conf.d/default.conf
echo "📋 Switched nginx config to: $TARGET_CONF"

# Nginx reload
docker exec nginx-proxy nginx -s reload
echo "🔄 Nginx config reloaded"
echo "🔄 Nginx reloaded"

# 이전 컨테이너 제거
# Remove previous container
if [ "$TARGET_COLOR" = "blue" ]; then
docker rm -f node-app-green || true
else
Expand All @@ -96,4 +98,5 @@ jobs:
echo "❌ Health check failed. Rolling back..."
docker rm -f node-app-$TARGET_COLOR || true
exit 1

EOF