Skip to content

Commit

Permalink
feat: blue green 무중단 배포 스크립트
Browse files Browse the repository at this point in the history
  • Loading branch information
Arachneee committed Oct 15, 2024
1 parent b17c274 commit 1c01357
Showing 1 changed file with 103 additions and 103 deletions.
206 changes: 103 additions & 103 deletions .github/workflows/backend-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,115 +117,115 @@ jobs:
echo "Health check passed."
port-switch:
needs: deploy
strategy:
matrix:
runner: [ prod-1 , prod-2 ]
runs-on: [ self-hosted, '${{ matrix.runner }}' ]
steps:
- name: Update or create Nginx container to point to new container port
run: |
NGINX_CONTAINER_NAME="nginx-proxy"
echo "Checking if Nginx container exists..."
if sudo docker ps -a --filter "name=$NGINX_CONTAINER_NAME" --format "{{.Names}}" | grep -w $NGINX_CONTAINER_NAME; then
# Check if the Nginx container is running
if sudo docker ps --filter "name=$NGINX_CONTAINER_NAME" --format "{{.Names}}" | grep -w $NGINX_CONTAINER_NAME; then
echo "Nginx container is running."
else
echo "Nginx container exists but is not running. Starting Nginx container..."
sudo docker start $NGINX_CONTAINER_NAME
echo "Nginx container started."
fi
# Ensure nginx-conf directory and default.conf file exist
mkdir -p ./nginx-conf
# Create or update the default.conf file with the new configuration
echo "Creating or updating default.conf..."
cat > ./nginx-conf/default <<EOF
server {
listen 80;
location / {
proxy_pass http://127.0.0.1:${{ steps.check-port.outputs.next_port }};
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
}
}
EOF
port-switch:
needs: deploy
strategy:
matrix:
runner: [ prod-1 , prod-2 ]
runs-on: [ self-hosted, '${{ matrix.runner }}' ]
steps:
- name: Update or create Nginx container to point to new container port
run: |
NGINX_CONTAINER_NAME="nginx-proxy"
echo "Checking if Nginx container exists..."
if sudo docker ps -a --filter "name=$NGINX_CONTAINER_NAME" --format "{{.Names}}" | grep -w $NGINX_CONTAINER_NAME; then
# Check if the Nginx container is running
if sudo docker ps --filter "name=$NGINX_CONTAINER_NAME" --format "{{.Names}}" | grep -w $NGINX_CONTAINER_NAME; then
echo "Nginx container is running."
else
echo "Nginx container exists but is not running. Starting Nginx container..."
sudo docker start $NGINX_CONTAINER_NAME
echo "Nginx container started."
fi
# Ensure nginx-conf directory and default.conf file exist
mkdir -p ./nginx-conf
# Create or update the default.conf file with the new configuration
echo "Creating or updating default.conf..."
# Test Nginx configuration inside a temporary container
echo "Testing Nginx configuration..."
sudo docker run --rm -v $(pwd)/nginx-conf:/etc/nginx/sites-available:ro nginx nginx -t
cat > ./nginx-conf/default <<EOF
server {
listen 80;
if [ $? -eq 0 ]; then
echo "Nginx configuration test passed."
else
echo "Nginx configuration test failed. Please check the configuration file."
exit 1
fi
location / {
proxy_pass http://127.0.0.1:${{ steps.check-port.outputs.next_port }};
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
}
}
EOF
# Restart the Nginx container to apply changes
echo "Restarting Nginx container..."
sudo docker restart $NGINX_CONTAINER_NAME
echo "Nginx container restarted."
# Test Nginx configuration inside a temporary container
echo "Testing Nginx configuration..."
sudo docker run --rm -v $(pwd)/nginx-conf:/etc/nginx/sites-available:ro nginx nginx -t
if [ $? -eq 0 ]; then
echo "Nginx configuration test passed."
else
echo "Nginx container not found. Creating a new Nginx container..."
# Create nginx-conf directory
mkdir -p ./nginx-conf
# Create the default.conf file with the initial configuration
echo "Creating default.conf..."
cat > ./nginx-conf/default <<EOF
server {
listen 80;
location / {
proxy_pass http://127.0.0.1:${{ steps.check-port.outputs.next_port }};
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
}
}
EOF
# Test Nginx configuration inside a temporary container
echo "Testing Nginx configuration..."
sudo docker run --rm -v $(pwd)/nginx-conf:/etc/nginx/sites-available:ro nginx nginx -t
if [ $? -eq 0 ]; then
echo "Nginx configuration test passed."
else
echo "Nginx configuration test failed. Please check the configuration file."
exit 1
fi
# Run a new Nginx container with the updated config
sudo docker run -d --name $NGINX_CONTAINER_NAME -p 80:80 \
-v $(pwd)/nginx-conf:/etc/nginx/conf.d:ro nginx
echo "New Nginx container created and running."
echo "Nginx configuration test failed. Please check the configuration file."
exit 1
fi
- name: Stop and remove the old container
run: |
CURRENT_PORT=${{ steps.check-port.outputs.current_port }}
if [ -n "$CURRENT_PORT" ]; then
echo "Stopping on port $CURRENT_PORT..."
sudo docker ps --filter "publish=$CURRENT_PORT" --format "{{.ID}}" | xargs sudo docker stop
echo "Sleeping on port $CURRENT_PORT..."
sleep 15
echo "Removing on port $CURRENT_PORT..."
sudo docker ps -a --format "{{.Names}}" | grep "haengdong-backend-$CURRENT_PORT" | xargs sudo docker rm
# Restart the Nginx container to apply changes
echo "Restarting Nginx container..."
sudo docker restart $NGINX_CONTAINER_NAME
echo "Nginx container restarted."
else
echo "Nginx container not found. Creating a new Nginx container..."
# Create nginx-conf directory
mkdir -p ./nginx-conf
# Create the default.conf file with the initial configuration
echo "Creating default.conf..."
cat > ./nginx-conf/default <<EOF
server {
listen 80;
location / {
proxy_pass http://127.0.0.1:${{ steps.check-port.outputs.next_port }};
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
}
}
EOF
# Test Nginx configuration inside a temporary container
echo "Testing Nginx configuration..."
sudo docker run --rm -v $(pwd)/nginx-conf:/etc/nginx/sites-available:ro nginx nginx -t
if [ $? -eq 0 ]; then
echo "Nginx configuration test passed."
else
echo "No container to stop and remove."
echo "Nginx configuration test failed. Please check the configuration file."
exit 1
fi
# Run a new Nginx container with the updated config
sudo docker run -d --name $NGINX_CONTAINER_NAME -p 80:80 \
-v $(pwd)/nginx-conf:/etc/nginx/conf.d:ro nginx
echo "New Nginx container created and running."
fi
- name: Stop and remove the old container
run: |
CURRENT_PORT=${{ steps.check-port.outputs.current_port }}
if [ -n "$CURRENT_PORT" ]; then
echo "Stopping on port $CURRENT_PORT..."
sudo docker ps --filter "publish=$CURRENT_PORT" --format "{{.ID}}" | xargs sudo docker stop
echo "Sleeping on port $CURRENT_PORT..."
sleep 15
echo "Removing on port $CURRENT_PORT..."
sudo docker ps -a --format "{{.Names}}" | grep "haengdong-backend-$CURRENT_PORT" | xargs sudo docker rm
else
echo "No container to stop and remove."
fi

0 comments on commit 1c01357

Please sign in to comment.