#!/usr/bin/env bash # deploy.yml › frontend — replace the environment's web container with erp-web: # and wait for it to serve /login. On failure the previously deployed image is started again # and the job fails. # shellcheck source=common.sh source "$(dirname "${BASH_SOURCE[0]}")/common.sh" : "${ERP_ENV:?ERP_ENV is required}" "${FRONTEND_HASH:?FRONTEND_HASH is required}" load_env "$ERP_ENV" is_rollback="${IS_ROLLBACK:-false}" docker image inspect "$WEB_IMAGE_REPO:$FRONTEND_HASH" >/dev/null 2>&1 || fail "image $WEB_IMAGE_REPO:$FRONTEND_HASH not found (pruned?)" start_web() { docker rm -f "$WEB_CONTAINER" >/dev/null 2>&1 || true docker run -d --name "$WEB_CONTAINER" \ -p "127.0.0.1:$WEB_PORT:3000" \ --memory=512m --cpus=1 \ --restart unless-stopped \ --label "erp.env=$ERP_ENV" \ "$WEB_IMAGE_REPO:$1" >/dev/null } wait_web() { local deadline=$((SECONDS + 30)) while ((SECONDS < deadline)); do if curl -fsS --max-time 5 -o /dev/null "http://127.0.0.1:$WEB_PORT/login" 2>/dev/null; then return 0; fi if [[ "$(docker inspect -f '{{.State.Running}}' "$WEB_CONTAINER" 2>/dev/null)" != true ]]; then warn "$WEB_CONTAINER is not running" return 1 fi sleep 2 done warn "$WEB_CONTAINER did not serve /login within 30s" return 1 } previous="$(read_state frontend)" log "deploying frontend $FRONTEND_HASH to $ERP_ENV (previous: ${previous:-none})" start_web "$FRONTEND_HASH" if wait_web; then write_state frontend "$FRONTEND_HASH" if [[ "$is_rollback" == true ]]; then result=rollback; else result=deployed; fi record_history frontend "$FRONTEND_HASH" "$result" log "✅ frontend $FRONTEND_HASH is serving on $ERP_ENV" exit 0 fi docker logs --tail 80 "$WEB_CONTAINER" || true record_history frontend "$FRONTEND_HASH" failed if [[ -n "$previous" && "$previous" != "$FRONTEND_HASH" ]] && docker image inspect "$WEB_IMAGE_REPO:$previous" >/dev/null 2>&1; then warn "restoring previous frontend $previous" start_web "$previous" wait_web || warn "the previous frontend is not serving either — investigate now" fi fail "frontend deploy of $FRONTEND_HASH to $ERP_ENV failed"