diff --git a/.dockerignore b/.dockerignore index dacccf38..2dc1ad1d 100644 --- a/.dockerignore +++ b/.dockerignore @@ -8,6 +8,10 @@ runtime .idea .vscode node_modules +.caiwu-build +public/js +public/css/app.css +public/mix-manifest.json database/database.sqlite storage/debugbar storage/framework/cache/data/* diff --git a/deploy/Dockerfile b/deploy/Dockerfile index eef4ac5e..dbc6f18b 100644 --- a/deploy/Dockerfile +++ b/deploy/Dockerfile @@ -60,9 +60,9 @@ WORKDIR /var/www/html COPY . /var/www/html COPY --from=vendor /build/vendor /var/www/html/vendor -COPY .caiwu-build/public/js /var/www/html/public/js -COPY .caiwu-build/public/css/app.css /var/www/html/public/css/app.css -COPY .caiwu-build/public/mix-manifest.json /var/www/html/public/mix-manifest.json +COPY --from=frontend /js /var/www/html/public/js +COPY --from=frontend /css/app.css /var/www/html/public/css/app.css +COPY --from=frontend /mix-manifest.json /var/www/html/public/mix-manifest.json COPY deploy/apache-vhost.conf /etc/apache2/sites-available/000-default.conf COPY deploy/php.ini /usr/local/etc/php/conf.d/caiwu.ini COPY deploy/entrypoint.sh /usr/local/bin/caiwu-entrypoint diff --git a/docs/开发与部署SOP.md b/docs/开发与部署SOP.md index 70257d25..dc7430b9 100644 --- a/docs/开发与部署SOP.md +++ b/docs/开发与部署SOP.md @@ -80,7 +80,7 @@ git pull --ff-only 1. 检查分支和未提交改动; 2. 使用项目专用 Node 20 和本机 npm 缓存构建前端资源; 3. 使用提交 SHA 构建镜像并复用项目专属缓存; -4. 导出临时镜像包并上传; +4. 导出 gzip 临时镜像包并上传; 5. 服务器加载镜像并启动; 6. 检查真实 HTTP 页面; 7. 失败时恢复上一个镜像; diff --git a/scripts/deploy-from-workstation.ps1 b/scripts/deploy-from-workstation.ps1 index f09c2795..07fb2346 100644 --- a/scripts/deploy-from-workstation.ps1 +++ b/scripts/deploy-from-workstation.ps1 @@ -23,7 +23,8 @@ $keyFullPath = (Resolve-Path $KeyPath).Path $sshTarget = "${SshUser}@${Server}" $imageName = 'caiwu_akaunting' $tempRoot = Join-Path ([IO.Path]::GetTempPath()) ("caiwu-deploy-" + [Guid]::NewGuid().ToString('N')) -$localArchive = Join-Path $tempRoot 'image.tar' +$localTar = Join-Path $tempRoot 'image.tar' +$localArchive = Join-Path $tempRoot 'image.tar.gz' $remoteArchive = $null $bootstrapRemote = $null $buildkitConfig = Join-Path $repoRoot 'deploy/buildkitd.toml' @@ -64,6 +65,33 @@ function Remove-OldProjectImages { } } +function Compress-Gzip { + param( + [Parameter(Mandatory = $true)] + [string]$Source, + [Parameter(Mandatory = $true)] + [string]$Destination + ) + + $inputStream = $null + $outputStream = $null + $gzipStream = $null + try { + $inputStream = [IO.File]::OpenRead($Source) + $outputStream = [IO.File]::Create($Destination) + $gzipStream = [IO.Compression.GZipStream]::new( + $outputStream, + [IO.Compression.CompressionLevel]::Fastest + ) + $inputStream.CopyTo($gzipStream) + } + finally { + if ($gzipStream) { $gzipStream.Dispose() } + if ($outputStream) { $outputStream.Dispose() } + if ($inputStream) { $inputStream.Dispose() } + } +} + Push-Location $repoRoot try { $branch = (git branch --show-current).Trim() @@ -79,7 +107,7 @@ try { $deploySha = (git rev-parse HEAD).Trim() $imageTag = $deploySha.Substring(0, 12) $fullImage = "${imageName}:$imageTag" - $remoteArchive = "$RemotePath/image-$imageTag.tar" + $remoteArchive = "$RemotePath/image-$imageTag.tar.gz" $buildDate = [DateTime]::UtcNow.ToString('yyyy-MM-ddTHH:mm:ssZ') if (-not $AppUrl) { @@ -109,6 +137,7 @@ try { --builder $BuilderName ` --load ` --tag $fullImage ` + --build-context 'frontend=.caiwu-build/public' ` --build-arg "VCS_REF=$deploySha" ` --build-arg "BUILD_DATE=$buildDate" ` --label 'com.caiwu.project=CaiWuXiTong' ` @@ -116,7 +145,8 @@ try { . } '本机镜像构建失败。服务器未发生任何变更。' - $previousSha = (& ssh -i $keyFullPath -o BatchMode=yes -o StrictHostKeyChecking=accept-new $sshTarget "cat '$RemotePath/.deployed-sha' 2>/dev/null || true").Trim() + $previousShaOutput = & ssh -i $keyFullPath -o BatchMode=yes -o StrictHostKeyChecking=accept-new $sshTarget "cat '$RemotePath/.deployed-sha' 2>/dev/null || true" + $previousSha = ([string]$previousShaOutput).Trim() if ($LASTEXITCODE -ne 0) { throw '无法连接服务器。' } @@ -154,7 +184,11 @@ try { ssh -i $keyFullPath -o BatchMode=yes $sshTarget "docker version >/dev/null && docker compose version >/dev/null && install -d -m 700 '$RemotePath'" } '服务器 Docker 未就绪;首次发布请添加 -BootstrapServer。' - Invoke-Native { docker save --output $localArchive $fullImage } '导出镜像失败。' + Invoke-Native { docker save --output $localTar $fullImage } '导出镜像失败。' + Compress-Gzip -Source $localTar -Destination $localArchive + Remove-Item -LiteralPath $localTar -Force + $archiveSizeMB = [math]::Round((Get-Item -LiteralPath $localArchive).Length / 1MB, 1) + Write-Host "压缩传输包:$archiveSizeMB MiB" Invoke-Native { scp -i $keyFullPath -o BatchMode=yes $localArchive "${sshTarget}:$remoteArchive" } '上传镜像失败。' Invoke-Native { scp -i $keyFullPath -o BatchMode=yes compose.jiangling.yml deploy/activate-release.sh "${sshTarget}:$RemotePath/" } '上传运行配置失败。'