diff --git a/docs/开发与部署SOP.md b/docs/开发与部署SOP.md index 7c605a45..f5ac50d7 100644 --- a/docs/开发与部署SOP.md +++ b/docs/开发与部署SOP.md @@ -190,6 +190,14 @@ bash scripts/deploy-from-mac.sh --allow-migrations 应用镜像回滚不能自动撤销数据库结构变更;Codex 必须为本次迁移保留对应的数据备份和恢复路径。 +负责人明确要求**不执行迁移、只发布应用代码**时,可使用显式跳过参数: + +```powershell +.\scripts\deploy-from-workstation.ps1 -SkipMigrations +``` + +macOS 对应 `--skip-migrations`。该参数只跳过数据库迁移,激活时 `RUN_MIGRATIONS` 保持为 `0`;不能与允许迁移参数同时使用。发布前仍需确认目标页面不依赖尚未执行的新表或字段,并如实记录未执行迁移及其影响。 + ## 7. 测试分支发布 正式服务器默认只允许 `main`。确需临时测试时,必须得到明确授权,并添加: diff --git a/scripts/deploy-from-mac.sh b/scripts/deploy-from-mac.sh index d028468b..4e00a2d3 100755 --- a/scripts/deploy-from-mac.sh +++ b/scripts/deploy-from-mac.sh @@ -17,6 +17,7 @@ # --configure-dingtalk-callback # --allow-non-main 允许非 main 分支(测试用) # --allow-migrations 允许执行待办数据库迁移(需先按 AGENTS.md 核对) +# --skip-migrations 明确不执行数据库迁移,只发布应用代码 # --allow-rollback 允许覆盖不在本次版本里的线上提交(仅负责人决定撤回时使用) # --skip-regctl 跳过 regctl,直接用 docker 直传镜像 # --keep-images <数量> 默认 2 @@ -75,6 +76,7 @@ bootstrap_server=0 configure_dingtalk_callback=0 allow_non_main=0 allow_migrations=0 +skip_migrations=0 allow_rollback=0 skip_regctl=0 app_version='3.1.15' @@ -102,6 +104,7 @@ while [ "$#" -gt 0 ]; do --configure-dingtalk-callback) configure_dingtalk_callback=1 ;; --allow-non-main) allow_non_main=1 ;; --allow-migrations) allow_migrations=1 ;; + --skip-migrations) skip_migrations=1 ;; --allow-rollback) allow_rollback=1 ;; --skip-regctl) skip_regctl=1 ;; -h|--help) sed -n '2,20p' "$0"; exit 0 ;; @@ -110,6 +113,11 @@ while [ "$#" -gt 0 ]; do shift done +if [ "$allow_migrations" -eq 1 ] && [ "$skip_migrations" -eq 1 ]; then + echo '--allow-migrations 与 --skip-migrations 不能同时使用。' >&2 + exit 2 +fi + script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" repo_root="$(cd "$script_dir/.." && pwd)" @@ -568,18 +576,22 @@ if [ -n "$previous_sha" ]; then fi fi - if [ "$previous_sha_exists" -eq 0 ] && [ "$allow_migrations" -eq 0 ]; then + if [ "$previous_sha_exists" -eq 0 ] && [ "$allow_migrations" -eq 0 ] && [ "$skip_migrations" -eq 0 ]; then fail "本地缺少服务器当前提交 ${previous_sha},无法安全比较数据库迁移。请先 git fetch 并核对全部待执行迁移,按 AGENTS.md 完成备份后使用 --allow-migrations。" fi if [ "$previous_sha_exists" -eq 1 ]; then migration_changes="$(git diff --diff-filter=ACMRTUXB --name-only "$previous_sha..$deploy_sha" -- database/migrations ':(glob)modules/*/Database/Migrations/*.php')" - if [ -n "$migration_changes" ] && [ "$allow_migrations" -eq 0 ]; then - fail "检测到数据库迁移:$(printf '%s' "$migration_changes" | tr '\n' ' ')。由 Codex 核对全部待执行迁移;本任务镜像迁移完成备份后可直接添加 --allow-migrations,其他迁移按 AGENTS.md 执行。" + if [ -n "$migration_changes" ] && [ "$allow_migrations" -eq 0 ] && [ "$skip_migrations" -eq 0 ]; then + fail "检测到数据库迁移:$(printf '%s' "$migration_changes" | tr '\n' ' ')。由 Codex 核对全部待执行迁移;本任务镜像迁移完成备份后可直接添加 --allow-migrations,其他迁移按 AGENTS.md 执行;明确不执行迁移时使用 --skip-migrations。" fi fi fi +if [ "$skip_migrations" -eq 1 ]; then + log '已明确跳过数据库迁移;本次只切换应用镜像,RUN_MIGRATIONS 保持为 0。' +fi + if [ "$bootstrap_server" -eq 1 ]; then bootstrap_remote="/tmp/caiwu-bootstrap-$image_tag.sh" scp_to_remote "$repo_root/deploy/bootstrap-server.sh" "${ssh_target}:$bootstrap_remote" || fail '上传服务器初始化脚本失败。' diff --git a/scripts/deploy-from-workstation.ps1 b/scripts/deploy-from-workstation.ps1 index a5d4be87..602b7e67 100644 --- a/scripts/deploy-from-workstation.ps1 +++ b/scripts/deploy-from-workstation.ps1 @@ -22,12 +22,16 @@ param( [switch]$ConfigureDingTalkCallback, [switch]$AllowNonMain, [switch]$AllowMigrations, + [switch]$SkipMigrations, # 仅在负责人明确决定撤回线上某次发布时使用;默认禁止覆盖不在本次版本里的线上提交。 [switch]$AllowRollback, [switch]$SkipRegctl ) $ErrorActionPreference = 'Stop' +if ($AllowMigrations -and $SkipMigrations) { + throw '-AllowMigrations 与 -SkipMigrations 不能同时使用。' +} $scriptRoot = $PSScriptRoot $repoRoot = (Resolve-Path (Join-Path $scriptRoot '..')).Path . (Join-Path $scriptRoot 'deploy-auth.ps1') @@ -501,7 +505,7 @@ try { } } - if ($previousSha -and -not $previousShaExists -and -not $AllowMigrations) { + if ($previousSha -and -not $previousShaExists -and -not $AllowMigrations -and -not $SkipMigrations) { throw "本地缺少服务器当前提交 $previousSha,无法安全比较数据库迁移。请先 git fetch 并核对全部待执行迁移,按 AGENTS.md 完成备份后使用 -AllowMigrations。" } @@ -509,11 +513,15 @@ try { $migrationChanges = git diff --diff-filter=ACMRTUXB --name-only "$previousSha..$deploySha" -- ` database/migrations ` ':(glob)modules/*/Database/Migrations/*.php' - if ($migrationChanges -and -not $AllowMigrations) { - throw "检测到数据库迁移:$($migrationChanges -join ', ')。由 Codex 核对全部待执行迁移;本任务镜像迁移完成备份后可直接添加 -AllowMigrations,其他迁移按 AGENTS.md 执行。" + if ($migrationChanges -and -not $AllowMigrations -and -not $SkipMigrations) { + throw "检测到数据库迁移:$($migrationChanges -join ', ')。由 Codex 核对全部待执行迁移;本任务镜像迁移完成备份后可直接添加 -AllowMigrations,其他迁移按 AGENTS.md 执行;明确不执行迁移时使用 -SkipMigrations。" } } + if ($SkipMigrations) { + Write-Warning '已明确跳过数据库迁移;本次只切换应用镜像,RUN_MIGRATIONS 保持为 0。' + } + New-Item -ItemType Directory -Path $tempRoot | Out-Null if ($BootstrapServer) {