feat: docker docmpose
This commit is contained in:
+26
-58
@@ -63,7 +63,7 @@ jobs:
|
||||
# DEPLOY BLUE / GREEN (CADDY GÈRE LE ROUTING)
|
||||
# =========================
|
||||
deploy:
|
||||
name: Deploy Blue/Green
|
||||
name: Deploy Blue/Green (Caddy routing)
|
||||
needs: build
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
@@ -78,98 +78,66 @@ jobs:
|
||||
username: ${{ secrets.SSH_USER }}
|
||||
key: ${{ secrets.SSH_PRIVATE_KEY }}
|
||||
source: docker-compose.prod.yml
|
||||
target: ${{ secrets.DEPLOY_PATH || '/opt/bemeal' }}
|
||||
target: /opt/bemeal
|
||||
|
||||
- name: Deploy via SSH
|
||||
uses: appleboy/ssh-action@v1.0.3
|
||||
env:
|
||||
IMAGE_TAG: ${{ github.sha }}
|
||||
REGISTRY: ${{ env.REGISTRY }}
|
||||
IMAGE_NAME: ${{ env.IMAGE_NAME }}
|
||||
DEPLOY_PATH: ${{ secrets.DEPLOY_PATH }}
|
||||
REGISTRY: gitea.leonmorival.com
|
||||
IMAGE_NAME: daily_meal/bemeal-api
|
||||
with:
|
||||
host: ${{ secrets.SSH_HOST }}
|
||||
username: ${{ secrets.SSH_USER }}
|
||||
key: ${{ secrets.SSH_PRIVATE_KEY }}
|
||||
envs: REGISTRY,IMAGE_NAME,IMAGE_TAG,DEPLOY_PATH
|
||||
envs: IMAGE_TAG,REGISTRY,IMAGE_NAME
|
||||
script: |
|
||||
set -eu
|
||||
|
||||
APP_DIR="${DEPLOY_PATH:-/opt/bemeal}"
|
||||
COMPOSE_FILE="docker-compose.prod.yml"
|
||||
|
||||
cd "$APP_DIR"
|
||||
|
||||
COMPOSE="docker compose --env-file .env.prod -f $COMPOSE_FILE"
|
||||
cd /opt/bemeal
|
||||
|
||||
COMPOSE="docker compose --env-file .env.prod -f docker-compose.prod.yml"
|
||||
IMAGE="$REGISTRY/$IMAGE_NAME:$IMAGE_TAG"
|
||||
|
||||
echo "Pull image: $IMAGE"
|
||||
docker pull "$IMAGE"
|
||||
|
||||
echo "Start shared services"
|
||||
echo "Deploy shared infra"
|
||||
$COMPOSE up -d pgsql redis meilisearch
|
||||
|
||||
# =========================
|
||||
# BLUE / GREEN DETECTION
|
||||
# =========================
|
||||
# detect active
|
||||
ACTIVE=""
|
||||
|
||||
if docker ps --format '{{.Names}}' | grep -q "bemeal-api-blue"; then
|
||||
if [ "$(docker inspect -f '{{.State.Health.Status}}' bemeal-api-blue 2>/dev/null || true)" = "healthy" ]; then
|
||||
ACTIVE="blue"
|
||||
fi
|
||||
if docker ps | grep -q bemeal-api-blue && \
|
||||
[ "$(docker inspect -f '{{.State.Health.Status}}' bemeal-api-blue 2>/dev/null || true)" = "healthy" ]; then
|
||||
ACTIVE=blue
|
||||
fi
|
||||
|
||||
if docker ps --format '{{.Names}}' | grep -q "bemeal-api-green"; then
|
||||
if [ "$(docker inspect -f '{{.State.Health.Status}}' bemeal-api-green 2>/dev/null || true)" = "healthy" ]; then
|
||||
ACTIVE="green"
|
||||
fi
|
||||
if docker ps | grep -q bemeal-api-green && \
|
||||
[ "$(docker inspect -f '{{.State.Health.Status}}' bemeal-api-green 2>/dev/null || true)" = "healthy" ]; then
|
||||
ACTIVE=green
|
||||
fi
|
||||
|
||||
if [ "$ACTIVE" = "blue" ]; then
|
||||
TARGET="green"
|
||||
TARGET=green
|
||||
else
|
||||
TARGET="blue"
|
||||
TARGET=blue
|
||||
fi
|
||||
|
||||
TARGET_SERVICE="bemeal-api-$TARGET"
|
||||
|
||||
echo "Deploying to $TARGET_SERVICE"
|
||||
echo "Deploy $TARGET_SERVICE"
|
||||
|
||||
IMAGE="$REGISTRY/$IMAGE_NAME:$IMAGE_TAG" \
|
||||
$COMPOSE up -d --force-recreate --no-deps "$TARGET_SERVICE"
|
||||
|
||||
# =========================
|
||||
# HEALTHCHECK
|
||||
# =========================
|
||||
echo "Waiting for healthcheck..."
|
||||
IMAGE="$IMAGE" $COMPOSE up -d --no-deps --force-recreate "$TARGET_SERVICE"
|
||||
|
||||
# wait health
|
||||
for i in $(seq 1 60); do
|
||||
STATUS="$(docker inspect -f '{{.State.Health.Status}}' "$TARGET_SERVICE" 2>/dev/null || true)"
|
||||
|
||||
if [ "$STATUS" = "healthy" ]; then
|
||||
break
|
||||
fi
|
||||
|
||||
STATUS=$(docker inspect -f '{{.State.Health.Status}}' "$TARGET_SERVICE" || true)
|
||||
[ "$STATUS" = "healthy" ] && break
|
||||
sleep 2
|
||||
done
|
||||
|
||||
if [ "$STATUS" != "healthy" ]; then
|
||||
echo "Healthcheck failed"
|
||||
docker logs --tail=200 "$TARGET_SERVICE" || true
|
||||
exit 1
|
||||
fi
|
||||
[ "$STATUS" != "healthy" ] && exit 1
|
||||
|
||||
docker exec "$TARGET_SERVICE" wget -qO- http://127.0.0.1/api/health >/dev/null || exit 1
|
||||
# 👉 SWITCH ULTRA SIMPLE
|
||||
echo "bemeal-api-$TARGET" > /opt/bemeal/caddy/active
|
||||
|
||||
echo "Deployment successful"
|
||||
|
||||
# =========================
|
||||
# CLEAN OLD CONTAINER
|
||||
# =========================
|
||||
if [ -n "$ACTIVE" ] && [ "$ACTIVE" != "$TARGET" ]; then
|
||||
echo "Stopping old $ACTIVE"
|
||||
$COMPOSE stop "bemeal-api-$ACTIVE" || true
|
||||
$COMPOSE rm -f "bemeal-api-$ACTIVE" || true
|
||||
fi
|
||||
docker exec caddy caddy reload
|
||||
|
||||
Reference in New Issue
Block a user