diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index 844d5e868..fc5d5387a 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -180,10 +180,27 @@ jobs: test -f "$release_tar" test -f "/var/lib/xiaoxia-saas-production/runtime-images-${RELEASE_VERSION}.tar" mkdir -p /var/lib/xiaoxia-saas-production + old_assets_dir="/tmp/xiaoxia-previous-web-assets-${RELEASE_VERSION}" + rm -rf "$old_assets_dir" + if [ -d /var/lib/xiaoxia-saas-production/repo/apps/web/dist/assets ]; then + mkdir -p "$old_assets_dir" + cp -a /var/lib/xiaoxia-saas-production/repo/apps/web/dist/assets/. "$old_assets_dir"/ + fi rm -rf /var/lib/xiaoxia-saas-production/repo mkdir -p /var/lib/xiaoxia-saas-production/repo tar -xzf "$release_tar" -C /var/lib/xiaoxia-saas-production/repo test -f /var/lib/xiaoxia-saas-production/repo/apps/web/dist/index.html + if [ -d "$old_assets_dir" ]; then + mkdir -p /var/lib/xiaoxia-saas-production/repo/apps/web/dist/assets + for asset in "$old_assets_dir"/*; do + [ -e "$asset" ] || continue + name="$(basename "$asset")" + if [ ! -e "/var/lib/xiaoxia-saas-production/repo/apps/web/dist/assets/$name" ]; then + cp -a "$asset" "/var/lib/xiaoxia-saas-production/repo/apps/web/dist/assets/$name" + fi + done + rm -rf "$old_assets_dir" + fi test -f /var/lib/xiaoxia-saas-production/.env cp /var/lib/xiaoxia-saas-production/.env /var/lib/xiaoxia-saas-production/repo/.env HOST_PREFIX= sh /var/lib/xiaoxia-saas-production/repo/infra/docker/deploy-production.sh diff --git a/infra/docker/nginx-production.conf b/infra/docker/nginx-production.conf index 39ffbaf1f..3b07b0be7 100644 --- a/infra/docker/nginx-production.conf +++ b/infra/docker/nginx-production.conf @@ -21,8 +21,13 @@ server { proxy_request_buffering off; } + location = /index.html { + add_header Cache-Control "no-store, no-cache, must-revalidate" always; + } + location / { try_files $uri $uri/ /index.html; + add_header Cache-Control "no-store, no-cache, must-revalidate" always; } location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ { diff --git a/tests/unit/test_release_scripts.py b/tests/unit/test_release_scripts.py index 2c34af360..5b742eb6a 100644 --- a/tests/unit/test_release_scripts.py +++ b/tests/unit/test_release_scripts.py @@ -54,6 +54,15 @@ def test_deploy_production_uses_production_infra_and_project(): assert "EXPECTED_VERSION=$RELEASE_VERSION" in script +def test_production_workflow_preserves_previous_web_assets(): + workflow = Path(".gitea/workflows/deploy.yml").read_text(encoding="utf-8") + production_section = workflow.split("deploy-production:", 1)[1] + + assert "old_assets_dir=\"/tmp/xiaoxia-previous-web-assets-${RELEASE_VERSION}\"" in production_section + assert "cp -a /var/lib/xiaoxia-saas-production/repo/apps/web/dist/assets/. \"$old_assets_dir\"/" in production_section + assert "if [ ! -e \"/var/lib/xiaoxia-saas-production/repo/apps/web/dist/assets/$name\" ]; then" in production_section + + def test_production_deploy_recreates_web_after_api_for_nginx_dns(): script = Path("infra/docker/deploy-production.sh").read_text(encoding="utf-8") @@ -72,6 +81,8 @@ def test_production_nginx_static_upstream_requires_web_recreate(): assert "client_max_body_size 800m;" in config assert "proxy_read_timeout 300s;" in config assert "proxy_request_buffering off;" in config + assert 'location = /index.html' in config + assert 'Cache-Control "no-store, no-cache, must-revalidate" always' in config assert "--force-recreate web" in script