@@ -0,0 +1,230 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
BASE="/var/www/compras-deploy"
|
||||
RELEASES="$BASE/releases"
|
||||
CURRENT="$BASE/current"
|
||||
SHARED="$BASE/shared"
|
||||
REPO="git@git.tempo.co.mz:nala/compras.git"
|
||||
LOCK="/tmp/compras-tempo-deploy.lock"
|
||||
HOST="compras.tempo.co.mz"
|
||||
|
||||
export APP_ENV=production
|
||||
export APP_DEBUG=false
|
||||
export COMPOSER_NO_INTERACTION=1
|
||||
|
||||
umask 0027
|
||||
|
||||
log() {
|
||||
printf '[DEPLOY] %s %s\n' "$(date -Is)" "$*"
|
||||
}
|
||||
|
||||
fail() {
|
||||
log "ERRO: $*"
|
||||
exit 1
|
||||
}
|
||||
|
||||
[ "$(id -un)" = "mario" ] ||
|
||||
fail "Utilizador de execucao invalido."
|
||||
|
||||
[ -d "$RELEASES" ] ||
|
||||
fail "Directorio de releases inexistente."
|
||||
|
||||
[ -L "$CURRENT" ] ||
|
||||
fail "Current nao e um link simbolico."
|
||||
|
||||
exec 9>"$LOCK"
|
||||
|
||||
flock -n 9 ||
|
||||
fail "Outra publicacao ja esta em execucao."
|
||||
|
||||
OLD_RELEASE="$(readlink -f "$CURRENT")"
|
||||
|
||||
[ -f "$OLD_RELEASE/public/index.php" ] ||
|
||||
fail "Release activa invalida."
|
||||
|
||||
ACTIVE_ENV="$(readlink -f "$OLD_RELEASE/.env")"
|
||||
ACTIVE_STORAGE="$(readlink -f "$OLD_RELEASE/storage")"
|
||||
[ -r "$ACTIVE_ENV" ] && [ -d "$ACTIVE_STORAGE" ] || fail "Configuracao activa indisponivel."
|
||||
[ -f "$OLD_RELEASE/scripts/verify-compras-release.php" ] || fail "Entrega integrada sem verificador."
|
||||
/usr/bin/php8.4 "$OLD_RELEASE/scripts/verify-compras-release.php" "$OLD_RELEASE" --installed || fail "Entrega activa inconsistente."
|
||||
|
||||
log "Inicio da publicacao."
|
||||
log "Release actual: $OLD_RELEASE"
|
||||
|
||||
TARGET_COMMIT="$(
|
||||
git ls-remote "$REPO" refs/heads/main |
|
||||
awk '{print $1}'
|
||||
)"
|
||||
|
||||
[[ "$TARGET_COMMIT" =~ ^[0-9a-f]{40}$ ]] ||
|
||||
fail "Nao foi possivel identificar o commit remoto."
|
||||
|
||||
CURRENT_COMMIT="$(
|
||||
git -C "$OLD_RELEASE" rev-parse HEAD 2>/dev/null || true
|
||||
)"
|
||||
|
||||
log "Commit actual: $CURRENT_COMMIT"
|
||||
log "Commit remoto: $TARGET_COMMIT"
|
||||
|
||||
if [ "$CURRENT_COMMIT" = "$TARGET_COMMIT" ]; then
|
||||
log "Nenhuma actualizacao necessaria."
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Reservar um directorio vazio e unico, sem apagar releases anteriores.
|
||||
NEW_RELEASE="$(mktemp -d "$RELEASES/$(date -u +%Y%m%d-%H%M%S)-${TARGET_COMMIT:0:12}-XXXXXXXX")"
|
||||
chmod 2750 "$NEW_RELEASE"
|
||||
RELEASE_NAME="${NEW_RELEASE##*/}"
|
||||
|
||||
log "Criar release: $RELEASE_NAME"
|
||||
|
||||
git clone \
|
||||
--branch main \
|
||||
--single-branch \
|
||||
"$REPO" \
|
||||
"$NEW_RELEASE"
|
||||
|
||||
ACTUAL_COMMIT="$(
|
||||
git -C "$NEW_RELEASE" rev-parse HEAD
|
||||
)"
|
||||
|
||||
[ "$ACTUAL_COMMIT" = "$TARGET_COMMIT" ] ||
|
||||
fail "O commit clonado difere do commit esperado."
|
||||
|
||||
cd "$NEW_RELEASE"
|
||||
|
||||
log "Preparar armazenamento partilhado."
|
||||
|
||||
[ -d "$NEW_RELEASE/storage" ] ||
|
||||
fail "Storage da nova release inexistente."
|
||||
|
||||
mv "$NEW_RELEASE/storage" "$NEW_RELEASE/storage.original"
|
||||
|
||||
ln -s "$ACTIVE_STORAGE" "$NEW_RELEASE/storage"
|
||||
ln -s "$ACTIVE_ENV" "$NEW_RELEASE/.env"
|
||||
|
||||
[ ! -e "$NEW_RELEASE/public/storage" ] &&
|
||||
[ ! -L "$NEW_RELEASE/public/storage" ] ||
|
||||
fail "public/storage ja existe."
|
||||
|
||||
ln -s "$ACTIVE_STORAGE/app/public" "$NEW_RELEASE/public/storage"
|
||||
|
||||
log "Conferir a mesma entrega integrada de Compras."
|
||||
/usr/bin/php8.4 "$OLD_RELEASE/scripts/verify-compras-release.php" "$NEW_RELEASE" || fail "Fonte incompleta ou sem manifesto de entrega."
|
||||
|
||||
log "Instalar dependencias PHP."
|
||||
|
||||
/usr/bin/php8.4 /usr/local/bin/composer install \
|
||||
--no-dev \
|
||||
--prefer-dist \
|
||||
--optimize-autoloader \
|
||||
--no-interaction
|
||||
|
||||
log "Instalar dependencias frontend."
|
||||
|
||||
/usr/bin/npm ci --no-audit --no-fund
|
||||
|
||||
log "Compilar frontend."
|
||||
|
||||
/usr/bin/npm run build
|
||||
|
||||
[ -f "$NEW_RELEASE/vendor/autoload.php" ] ||
|
||||
fail "Autoload Composer inexistente."
|
||||
|
||||
[ -f "$NEW_RELEASE/public/build/manifest.json" ] ||
|
||||
fail "Manifest frontend inexistente."
|
||||
|
||||
log "Preparar caches Laravel."
|
||||
|
||||
/usr/bin/php8.4 artisan config:cache
|
||||
|
||||
# As vistas compiladas utilizam storage partilhado.
|
||||
# Nao executar view:cache durante a preparacao.
|
||||
|
||||
log "Validar Laravel."
|
||||
|
||||
/usr/bin/php8.4 artisan about --only=environment
|
||||
|
||||
log "Validar ficheiros essenciais."
|
||||
|
||||
[ -f "$NEW_RELEASE/public/index.php" ] ||
|
||||
fail "public/index.php inexistente."
|
||||
|
||||
[ -r "$NEW_RELEASE/.env" ] ||
|
||||
fail ".env inacessivel."
|
||||
|
||||
[ -r "$NEW_RELEASE/public/build/manifest.json" ] ||
|
||||
fail "Manifest inacessivel."
|
||||
|
||||
/usr/bin/php8.4 "$OLD_RELEASE/scripts/verify-compras-release.php" "$NEW_RELEASE" --installed || fail "Compilacao ou componentes diferem da entrega declarada."
|
||||
|
||||
log "Validar release antes da activacao."
|
||||
|
||||
[ "$(readlink -f "$CURRENT")" = "$OLD_RELEASE" ] ||
|
||||
fail "Current foi alterado durante a preparacao."
|
||||
|
||||
[ "$(git -C "$NEW_RELEASE" rev-parse HEAD)" = "$TARGET_COMMIT" ] ||
|
||||
fail "Commit da nova release invalido."
|
||||
|
||||
log "Activar release."
|
||||
|
||||
TEMP_LINK="$BASE/.current-next-$$"
|
||||
|
||||
ln -s "$NEW_RELEASE" "$TEMP_LINK"
|
||||
|
||||
mv -Tf "$TEMP_LINK" "$CURRENT"
|
||||
|
||||
[ "$(readlink -f "$CURRENT")" = "$NEW_RELEASE" ] ||
|
||||
fail "Current nao aponta para a nova release."
|
||||
|
||||
log "Executar health check local."
|
||||
|
||||
[ "$(readlink -f "$CURRENT")" = "$NEW_RELEASE" ] ||
|
||||
fail "Release activa inesperada."
|
||||
|
||||
HEALTH_OK=0
|
||||
|
||||
for attempt in 1 2 3 4 5; do
|
||||
HTTP_CODE="$(
|
||||
/usr/bin/curl \
|
||||
--noproxy '*' \
|
||||
--silent \
|
||||
--show-error \
|
||||
--output /dev/null \
|
||||
--write-out '%{http_code}' \
|
||||
--resolve "$HOST:443:127.0.0.1" \
|
||||
--max-time 15 \
|
||||
"https://$HOST/dashboard" || true
|
||||
)"
|
||||
|
||||
log "Health check $attempt: HTTP $HTTP_CODE"
|
||||
|
||||
if [ "$HTTP_CODE" = "200" ]; then
|
||||
HEALTH_OK=1
|
||||
break
|
||||
fi
|
||||
|
||||
sleep 3
|
||||
done
|
||||
|
||||
if [ "$HEALTH_OK" -ne 1 ]; then
|
||||
log "Health check falhou. Iniciar rollback."
|
||||
|
||||
ROLLBACK_LINK="$BASE/.current-rollback-$$"
|
||||
|
||||
[ "$(readlink -f "$CURRENT")" = "$NEW_RELEASE" ] ||
|
||||
fail "Current foi alterado; rollback automatico interrompido."
|
||||
|
||||
ln -s "$OLD_RELEASE" "$ROLLBACK_LINK"
|
||||
|
||||
mv -Tf "$ROLLBACK_LINK" "$CURRENT"
|
||||
|
||||
log "Rollback efectuado para $OLD_RELEASE"
|
||||
|
||||
exit 1
|
||||
fi
|
||||
|
||||
log "Publicacao concluida."
|
||||
log "Release activa: $(readlink -f "$CURRENT")"
|
||||
log "Commit publicado: $TARGET_COMMIT"
|
||||
Reference in New Issue
Block a user