feat: try fix
This commit is contained in:
parent
328d64ea2f
commit
6a0f80bdb5
|
|
@ -29,8 +29,9 @@ jobs:
|
||||||
DB_CONNECTION: sqlite
|
DB_CONNECTION: sqlite
|
||||||
DB_DATABASE: ":memory:"
|
DB_DATABASE: ":memory:"
|
||||||
with:
|
with:
|
||||||
args: |
|
args: >
|
||||||
bash -c "composer install --no-interaction --ignore-platform-req=ext-intl && php artisan test"
|
bash -c "composer install --no-interaction --ignore-platform-req=ext-intl
|
||||||
|
&& php artisan test"
|
||||||
|
|
||||||
# =========================
|
# =========================
|
||||||
# BUILD & PUSH IMAGE
|
# BUILD & PUSH IMAGE
|
||||||
|
|
@ -41,7 +42,7 @@ jobs:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout code
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Login to Gitea Registry
|
- name: Login to Gitea Registry
|
||||||
|
|
@ -51,16 +52,15 @@ jobs:
|
||||||
-u "${{ github.actor }}" \
|
-u "${{ github.actor }}" \
|
||||||
--password-stdin
|
--password-stdin
|
||||||
|
|
||||||
- name: Build Docker image
|
- name: Build & Push image
|
||||||
run: |
|
run: |
|
||||||
IMAGE=$REGISTRY/$IMAGE_NAME
|
IMAGE=$REGISTRY/$IMAGE_NAME
|
||||||
docker build -t "$IMAGE:${{ github.sha }}" -t "$IMAGE:latest" .
|
docker build -t "$IMAGE:${{ github.sha }}" -t "$IMAGE:latest" .
|
||||||
docker push "$IMAGE:${{ github.sha }}"
|
docker push "$IMAGE:${{ github.sha }}"
|
||||||
docker push "$IMAGE:latest"
|
docker push "$IMAGE:latest"
|
||||||
|
|
||||||
|
|
||||||
# =========================
|
# =========================
|
||||||
# DEPLOY
|
# DEPLOY BLUE / GREEN
|
||||||
# =========================
|
# =========================
|
||||||
deploy:
|
deploy:
|
||||||
name: Deploy Blue/Green
|
name: Deploy Blue/Green
|
||||||
|
|
@ -71,7 +71,7 @@ jobs:
|
||||||
- name: Checkout deploy files
|
- name: Checkout deploy files
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Upload Docker Compose file
|
- name: Upload docker-compose
|
||||||
uses: appleboy/scp-action@v0.1.7
|
uses: appleboy/scp-action@v0.1.7
|
||||||
with:
|
with:
|
||||||
host: ${{ secrets.SSH_HOST }}
|
host: ${{ secrets.SSH_HOST }}
|
||||||
|
|
@ -80,130 +80,96 @@ jobs:
|
||||||
source: docker-compose.prod.yml
|
source: docker-compose.prod.yml
|
||||||
target: ${{ secrets.DEPLOY_PATH || '/opt/bemeal' }}
|
target: ${{ secrets.DEPLOY_PATH || '/opt/bemeal' }}
|
||||||
|
|
||||||
- name: Deploy via SSH (Blue/Green)
|
- name: Deploy via SSH
|
||||||
uses: appleboy/ssh-action@v1.0.3
|
uses: appleboy/ssh-action@v1.0.3
|
||||||
env:
|
env:
|
||||||
IMAGE_TAG: ${{ github.sha }}
|
IMAGE_TAG: ${{ github.sha }}
|
||||||
DEPLOY_PATH: ${{ secrets.DEPLOY_PATH }}
|
DEPLOY_PATH: ${{ secrets.DEPLOY_PATH }}
|
||||||
NGINX_CONTAINER: ${{ secrets.NGINX_CONTAINER }}
|
|
||||||
NGINX_UPSTREAM_FILE: ${{ secrets.NGINX_UPSTREAM_FILE }}
|
|
||||||
with:
|
with:
|
||||||
host: ${{ secrets.SSH_HOST }}
|
host: ${{ secrets.SSH_HOST }}
|
||||||
username: ${{ secrets.SSH_USER }}
|
username: ${{ secrets.SSH_USER }}
|
||||||
key: ${{ secrets.SSH_PRIVATE_KEY }}
|
key: ${{ secrets.SSH_PRIVATE_KEY }}
|
||||||
envs: REGISTRY,IMAGE_NAME,IMAGE_TAG,DEPLOY_PATH,NGINX_CONTAINER,NGINX_UPSTREAM_FILE
|
envs: REGISTRY,IMAGE_NAME,IMAGE_TAG,DEPLOY_PATH
|
||||||
script: |
|
script: |
|
||||||
set -eu
|
set -eu
|
||||||
|
|
||||||
APP_DIR="${DEPLOY_PATH:-/opt/bemeal}"
|
APP_DIR="${DEPLOY_PATH:-/opt/bemeal}"
|
||||||
COMPOSE_FILE="docker-compose.prod.yml"
|
COMPOSE_FILE="docker-compose.prod.yml"
|
||||||
COMPOSE_ENV_FILE=".env.prod"
|
|
||||||
NGINX_CONTAINER="${NGINX_CONTAINER:-nginx-app-1}"
|
|
||||||
NGINX_UPSTREAM_FILE="${NGINX_UPSTREAM_FILE:-/etc/nginx/conf.d/bemeal-upstream.inc}"
|
|
||||||
DRAIN_SECONDS="${DRAIN_SECONDS:-20}"
|
|
||||||
|
|
||||||
if [ ! -d "$APP_DIR" ]; then
|
|
||||||
echo "Missing deploy directory: $APP_DIR"
|
|
||||||
echo "Set the DEPLOY_PATH secret to the directory containing $COMPOSE_FILE."
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
cd "$APP_DIR"
|
cd "$APP_DIR"
|
||||||
|
|
||||||
if [ ! -f "$COMPOSE_FILE" ]; then
|
if [ ! -f "$COMPOSE_FILE" ]; then
|
||||||
echo "Missing $APP_DIR/$COMPOSE_FILE"
|
echo "Missing docker-compose.prod.yml"
|
||||||
echo "Set the DEPLOY_PATH secret to the directory containing $COMPOSE_FILE, or copy the compose file there."
|
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if ! docker inspect "$NGINX_CONTAINER" >/dev/null 2>&1; then
|
COMPOSE="docker compose -f $COMPOSE_FILE"
|
||||||
echo "Missing Nginx container: $NGINX_CONTAINER"
|
|
||||||
echo "Set the NGINX_CONTAINER secret to your reverse proxy container name."
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ -f "$COMPOSE_ENV_FILE" ]; then
|
|
||||||
COMPOSE="docker compose --env-file $COMPOSE_ENV_FILE -f $COMPOSE_FILE"
|
|
||||||
else
|
|
||||||
COMPOSE="docker compose -f $COMPOSE_FILE"
|
|
||||||
fi
|
|
||||||
|
|
||||||
IMAGE="$REGISTRY/$IMAGE_NAME:$IMAGE_TAG"
|
IMAGE="$REGISTRY/$IMAGE_NAME:$IMAGE_TAG"
|
||||||
export IMAGE_TAG
|
|
||||||
|
|
||||||
echo "Pull image $IMAGE"
|
echo "Pull image: $IMAGE"
|
||||||
docker pull "$IMAGE"
|
docker pull "$IMAGE"
|
||||||
|
|
||||||
echo "Ensure shared services are running"
|
echo "Start shared services"
|
||||||
$COMPOSE up -d pgsql redis meilisearch
|
$COMPOSE up -d pgsql redis meilisearch
|
||||||
|
|
||||||
|
# =========================
|
||||||
|
# BLUE / GREEN LOGIC
|
||||||
|
# =========================
|
||||||
|
|
||||||
ACTIVE=""
|
ACTIVE=""
|
||||||
if docker exec "$NGINX_CONTAINER" sh -c "test -f '$NGINX_UPSTREAM_FILE' && grep -q 'bemeal-api-blue:80' '$NGINX_UPSTREAM_FILE'" 2>/dev/null; then
|
|
||||||
ACTIVE="blue"
|
if docker ps --format '{{.Names}}' | grep -q "bemeal-api-blue"; then
|
||||||
elif docker exec "$NGINX_CONTAINER" sh -c "test -f '$NGINX_UPSTREAM_FILE' && grep -q 'bemeal-api-green:80' '$NGINX_UPSTREAM_FILE'" 2>/dev/null; then
|
if [ "$(docker inspect -f '{{.State.Health.Status}}' bemeal-api-blue 2>/dev/null || true)" = "healthy" ]; then
|
||||||
ACTIVE="green"
|
ACTIVE="blue"
|
||||||
|
fi
|
||||||
|
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
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ "$ACTIVE" = "blue" ]; then
|
if [ "$ACTIVE" = "blue" ]; then
|
||||||
TARGET="green"
|
TARGET="green"
|
||||||
TARGET_HOST="bemeal-api-green"
|
|
||||||
else
|
else
|
||||||
TARGET="blue"
|
TARGET="blue"
|
||||||
TARGET_HOST="bemeal-api-blue"
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
TARGET_SERVICE="bemeal-api-$TARGET"
|
TARGET_SERVICE="bemeal-api-$TARGET"
|
||||||
|
|
||||||
echo "Start $TARGET_SERVICE"
|
echo "Deploying to $TARGET_SERVICE"
|
||||||
|
|
||||||
|
IMAGE="$REGISTRY/$IMAGE_NAME:$IMAGE_TAG" \
|
||||||
$COMPOSE up -d --force-recreate --no-deps "$TARGET_SERVICE"
|
$COMPOSE up -d --force-recreate --no-deps "$TARGET_SERVICE"
|
||||||
|
|
||||||
echo "Wait for $TARGET_SERVICE healthcheck"
|
echo "Waiting for healthcheck..."
|
||||||
HEALTH_STATUS=""
|
for i in $(seq 1 60); do
|
||||||
for attempt in $(seq 1 60); do
|
STATUS="$(docker inspect -f '{{.State.Health.Status}}' "$TARGET_SERVICE" 2>/dev/null || true)"
|
||||||
HEALTH_STATUS="$(docker inspect --format='{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}' "$TARGET_SERVICE" 2>/dev/null || true)"
|
|
||||||
|
|
||||||
if [ "$HEALTH_STATUS" = "healthy" ]; then
|
if [ "$STATUS" = "healthy" ]; then
|
||||||
break
|
break
|
||||||
fi
|
fi
|
||||||
|
|
||||||
sleep 2
|
sleep 2
|
||||||
done
|
done
|
||||||
|
|
||||||
if [ "$HEALTH_STATUS" != "healthy" ]; then
|
if [ "$STATUS" != "healthy" ]; then
|
||||||
|
echo "Healthcheck failed"
|
||||||
docker logs --tail=200 "$TARGET_SERVICE" || true
|
docker logs --tail=200 "$TARGET_SERVICE" || true
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
docker exec "$TARGET_SERVICE" wget -qO- http://127.0.0.1/api/health >/dev/null
|
echo "API is healthy on $TARGET_SERVICE"
|
||||||
|
|
||||||
NGINX_UPSTREAM_DIR="$(dirname "$NGINX_UPSTREAM_FILE")"
|
docker exec "$TARGET_SERVICE" wget -qO- http://127.0.0.1/api/health >/dev/null || exit 1
|
||||||
docker exec "$NGINX_CONTAINER" sh -c "mkdir -p '$NGINX_UPSTREAM_DIR'"
|
|
||||||
docker exec "$NGINX_CONTAINER" sh -c "test ! -f '$NGINX_UPSTREAM_FILE' || cp '$NGINX_UPSTREAM_FILE' '$NGINX_UPSTREAM_FILE.bak'"
|
|
||||||
|
|
||||||
printf '%s\n' \
|
echo "Deployment successful"
|
||||||
'upstream bemeal_api {' \
|
|
||||||
" server $TARGET_HOST:80 max_fails=3 fail_timeout=10s;" \
|
|
||||||
' keepalive 32;' \
|
|
||||||
'}' | docker exec -i "$NGINX_CONTAINER" sh -c "cat > '$NGINX_UPSTREAM_FILE'"
|
|
||||||
|
|
||||||
if ! docker exec "$NGINX_CONTAINER" nginx -t; then
|
|
||||||
if docker exec "$NGINX_CONTAINER" sh -c "test -f '$NGINX_UPSTREAM_FILE.bak'" 2>/dev/null; then
|
|
||||||
docker exec "$NGINX_CONTAINER" sh -c "mv '$NGINX_UPSTREAM_FILE.bak' '$NGINX_UPSTREAM_FILE'"
|
|
||||||
fi
|
|
||||||
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "Switch Nginx traffic to $TARGET"
|
|
||||||
docker exec "$NGINX_CONTAINER" nginx -s reload
|
|
||||||
docker exec "$NGINX_CONTAINER" rm -f "$NGINX_UPSTREAM_FILE.bak"
|
|
||||||
|
|
||||||
echo "Restart workers on the new image"
|
|
||||||
$COMPOSE up -d --force-recreate --no-deps horizon scheduler
|
|
||||||
|
|
||||||
|
# optional cleanup old container
|
||||||
if [ -n "$ACTIVE" ] && [ "$ACTIVE" != "$TARGET" ]; then
|
if [ -n "$ACTIVE" ] && [ "$ACTIVE" != "$TARGET" ]; then
|
||||||
echo "Drain old $ACTIVE service for $DRAIN_SECONDS seconds"
|
echo "Stopping old $ACTIVE container"
|
||||||
sleep "$DRAIN_SECONDS"
|
|
||||||
$COMPOSE stop "bemeal-api-$ACTIVE" || true
|
$COMPOSE stop "bemeal-api-$ACTIVE" || true
|
||||||
$COMPOSE rm -f "bemeal-api-$ACTIVE" || true
|
$COMPOSE rm -f "bemeal-api-$ACTIVE" || true
|
||||||
fi
|
fi
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue