Files
solution-erp/.gitea/workflows/deploy.yml
pqhuy1987 305b573f5a
All checks were successful
Deploy SOLUTION_ERP / build-deploy (push) Successful in 10m22s
[CLAUDE] CICD: vá GỐC #92 — npm ci theo lockfile đã commit + guard React-đơn-bản (bỏ xoá lockfile mỗi run)
Root-cause 2 ca trắng màn prod (S189 #486, S191 #600): deploy.yml xoá package-lock.json
rồi npm install => mỗi run resolve lại cây theo semver range => cùng commit ra bundle khác
nhau tuỳ registry lúc build. CI vẫn exit 0 + byte-match CI-prod + smoke 200 vì mọi phép đo
đều đo FILE, không đo CHƯƠNG TRÌNH CHẠY.

- lockfile tái sinh từ cây lành đang chạy prod (thiếu @microsoft/signalr + 9 transitive
  vì lockfile cũ có từ 23/04, trước khi dự án thêm SignalR — nên 'xoá cho lành' từng hợp lý)
- npm install -> npm ci (deterministic theo lockfile) + check exit code từng bước
- GUARD-92: chặn build khi React trùng bản (đúng cơ chế useState null). Fault-inject 4 ca
  bắt được dương-giả lucide-react khớp regex trước khi push; đã neo lookbehind.
- Bundle verify runtime trên prod trước khi commit: eoffice CR538vZx + admin C10ta-wB,
  console 0 lỗi, cùng byte với bản lành (1.700.709 / 1.780.807).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-13 01:13:34 +07:00

227 lines
11 KiB
YAML

# Gitea Actions CI/CD - build + deploy SOLUTION_ERP to IIS on same VPS.
# Trigger: push to main, or manual dispatch.
#
# Self-hosted Windows runner on VPS (shared with VIETREPORT). Runner has:
# - git, .NET 10 SDK, Node 20, IIS
# - Can deploy locally (no WinRM needed)
#
# Secrets required in Gitea repo settings:
# - JWT_SECRET (64+ chars random)
# - DB_CONNECTION (full connection string with vrapp password)
name: Deploy SOLUTION_ERP
# Path filter — skip CI khi commit chỉ docs/MD/skill (~110s saved per docs commit).
# Commit MD-only như "Docs: chốt session" sẽ KHÔNG trigger workflow.
# Lưu ý: nếu cùng 1 commit thay đổi cả MD + code → vẫn trigger (đúng behavior).
on:
push:
branches: [main]
paths-ignore:
- 'docs/**'
- '**/*.md'
- '.claude/skills/**'
- '.claude/agent-memory/**'
- '.claude/workflows/runs/**'
- '.gitignore'
- 'scripts/**.md'
workflow_dispatch:
jobs:
build-deploy:
runs-on: windows-latest
steps:
# Manual checkout thay vì `uses: actions/checkout@v4` — tránh phụ thuộc
# github.com (act_runner mỗi run đều `git fetch` để check update action,
# khi VPS → github.com TCP timeout 21s thì toàn job fail trước khi tới
# test gate). Gitea internal network luôn ổn định, nên clone trực tiếp.
# Token `${{ github.token }}` (Gitea cũng dùng tên này) tự sẵn cho job.
- name: Checkout (manual git, bypass github.com)
shell: powershell
run: |
git config --global --add safe.directory '*'
git init -q
git remote add origin "https://gitea-actions:${{ github.token }}@git.baocaogiaoduc.vn/${{ github.repository }}.git"
# Fetch ref (branch) thay vì SHA — không cần Gitea allow SHA fetch.
# Depth 30 đủ buffer nếu main đã commit thêm sau khi job pickup.
$ref = "${{ github.ref }}"
if ($ref -like "refs/heads/*") { $ref = $ref.Substring(11) }
git fetch --depth=30 origin $ref
git checkout --quiet "${{ github.sha }}"
git log -1 --oneline
- name: Show tool versions
shell: powershell
run: |
& 'C:\Program Files\dotnet\dotnet.exe' --version
& 'C:\Program Files\nodejs\node.exe' --version
& 'C:\Program Files\nodejs\npm.cmd' --version
# ============== TEST GATE ==============
# Run tests TRƯỚC build/publish/deploy. Fail → exit non-zero → no deploy.
# Phase 1: Domain (54 test policy state machine).
# Phase 2: Infrastructure (17 test code generators format/sequence/year scope).
- name: Run unit tests (Domain)
shell: powershell
run: |
& 'C:\Program Files\dotnet\dotnet.exe' test tests/SolutionErp.Domain.Tests/SolutionErp.Domain.Tests.csproj `
--configuration Release `
--logger "trx;LogFileName=domain-tests.trx" `
--results-directory test-results
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
- name: Run integration tests (Infrastructure - SQLite in-memory)
shell: powershell
run: |
& 'C:\Program Files\dotnet\dotnet.exe' test tests/SolutionErp.Infrastructure.Tests/SolutionErp.Infrastructure.Tests.csproj `
--configuration Release `
--logger "trx;LogFileName=infra-tests.trx" `
--results-directory test-results
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
# Upload test results — bỏ vì `actions/upload-artifact@v4` cũng phụ thuộc
# github.com fetch (cùng vấn đề như actions/checkout). TRX file vẫn save
# local trong workspace `test-results/` cho debug khi cần.
- name: List test results (local debug)
if: always()
shell: powershell
run: |
if (Test-Path test-results) {
Get-ChildItem test-results -Recurse | Format-Table FullName, Length
} else {
Write-Host "No test-results directory."
}
# ============== BUILD ==============
- name: Build backend
shell: powershell
run: |
& 'C:\Program Files\dotnet\dotnet.exe' restore SolutionErp.slnx
& 'C:\Program Files\dotnet\dotnet.exe' publish src/Backend/SolutionErp.Api/SolutionErp.Api.csproj `
--configuration Release `
--output out/api `
--runtime win-x64 `
--self-contained false
# FE build — `npm ci` theo package-lock.json ĐÃ COMMIT (gotcha #92, S191).
#
# 🔴 KHÔNG được xoá package-lock.json. Bản trước làm đúng thế:
# Remove-Item node_modules, package-lock.json -Force ; npm install
# ⇒ mỗi run resolve LẠI cả cây theo semver range (react ^19.2.5, vite ^8.0.9…)
# ⇒ cùng một commit build ra bundle KHÁC NHAU tuỳ registry tại thời điểm chạy
# ⇒ đã 2 lần ship bundle crash runtime `useState null` (trắng màn prod, S189+S191)
# trong khi CI vẫn exit 0 + byte-match CI↔prod + smoke 200 (đo file, không đo chạy).
# Lockfile cũ thiếu @microsoft/signalr nên "xoá đi cho lành" từng có vẻ hợp lý —
# thuốc đúng là CẬP NHẬT lockfile (đã làm @S191), không phải bỏ nó.
#
# node_modules vẫn xoá: rolldown native binding phải khớp platform (gotcha #20),
# và `npm ci` bản thân nó luôn cài lại từ đầu.
- name: Build fe-admin
shell: powershell
working-directory: fe-admin
run: |
Remove-Item node_modules -Recurse -Force -ErrorAction SilentlyContinue
& 'C:\Program Files\nodejs\npm.cmd' ci --no-audit --no-fund
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
# GUARD #92 — React trùng bản = đúng nguyên nhân `useState null` (hook đọc null
# vì component render bởi bản React khác bản đang giữ dispatcher). `npm ls react`
# in một dòng "deduped" cho mỗi consumer khi cây LÀNH; có bản thứ 2 thì hiện
# version thật ở nhánh con. Chặn tại đây rẻ hơn phát hiện bằng trắng màn prod.
$tree = & 'C:\Program Files\nodejs\npm.cmd' ls react 2>&1 | Out-String
# Lookbehind BẮT BUỘC: thiếu nó thì `lucide-react@1.8.0` khớp như một bản React
# ⇒ cây LÀNH cũng FLAG ⇒ CI đỏ vĩnh viễn. Fault-inject 4 ca bắt được lỗi này
# trước khi push (cây-lành IM · trùng-bản FLAG · lệch-patch FLAG · cây-hỏng IM).
$versions = [regex]::Matches($tree, '(?<![-\w./@])react@(\d+\.\d+\.\d+)') | ForEach-Object { $_.Groups[1].Value } | Sort-Object -Unique
if ($versions.Count -gt 1) {
Write-Host "GUARD-92 FAIL: co $($versions.Count) ban React trong cay: $($versions -join ', ')"
Write-Host $tree
exit 1
}
Write-Host "GUARD-92 OK: react don ban = $versions"
& 'C:\Program Files\nodejs\npm.cmd' run build
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
- name: Build fe-user
shell: powershell
working-directory: fe-user
run: |
Remove-Item node_modules -Recurse -Force -ErrorAction SilentlyContinue
& 'C:\Program Files\nodejs\npm.cmd' ci --no-audit --no-fund
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
# GUARD #92 — React trùng bản = đúng nguyên nhân `useState null` (hook đọc null
# vì component render bởi bản React khác bản đang giữ dispatcher). `npm ls react`
# in một dòng "deduped" cho mỗi consumer khi cây LÀNH; có bản thứ 2 thì hiện
# version thật ở nhánh con. Chặn tại đây rẻ hơn phát hiện bằng trắng màn prod.
$tree = & 'C:\Program Files\nodejs\npm.cmd' ls react 2>&1 | Out-String
# Lookbehind BẮT BUỘC: thiếu nó thì `lucide-react@1.8.0` khớp như một bản React
# ⇒ cây LÀNH cũng FLAG ⇒ CI đỏ vĩnh viễn. Fault-inject 4 ca bắt được lỗi này
# trước khi push (cây-lành IM · trùng-bản FLAG · lệch-patch FLAG · cây-hỏng IM).
$versions = [regex]::Matches($tree, '(?<![-\w./@])react@(\d+\.\d+\.\d+)') | ForEach-Object { $_.Groups[1].Value } | Sort-Object -Unique
if ($versions.Count -gt 1) {
Write-Host "GUARD-92 FAIL: co $($versions.Count) ban React trong cay: $($versions -join ', ')"
Write-Host $tree
exit 1
}
Write-Host "GUARD-92 OK: react don ban = $versions"
& 'C:\Program Files\nodejs\npm.cmd' run build
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
- name: Deploy to IIS (local)
if: github.ref == 'refs/heads/main'
shell: powershell
env:
JWT_SECRET: ${{ secrets.JWT_SECRET }}
DB_CONNECTION: ${{ secrets.DB_CONNECTION }}
run: |
Import-Module WebAdministration
# Stop app pool (if running) so DLLs are writable
$poolState = (Get-WebAppPoolState -Name SolutionErp-Api -ErrorAction SilentlyContinue).Value
if ($poolState -eq 'Started') {
Stop-WebAppPool -Name SolutionErp-Api
Start-Sleep -Seconds 3
}
# Deploy API
Remove-Item -Path 'C:\inetpub\solution-erp\api\*' -Recurse -Force -Exclude 'appsettings.Production.json','logs','uploads','wwwroot' -ErrorAction SilentlyContinue
Copy-Item -Path 'out\api\*' -Destination 'C:\inetpub\solution-erp\api\' -Recurse -Force
# Write appsettings.Production.json from source template + secrets.
# Template is in source workspace (not in publish output - dotnet publish
# doesn't copy .example files).
$example = 'src\Backend\SolutionErp.Api\appsettings.Production.json.example'
$prod = 'C:\inetpub\solution-erp\api\appsettings.Production.json'
$settings = Get-Content $example -Raw | ConvertFrom-Json
$settings.ConnectionStrings.Default = $env:DB_CONNECTION
$settings.Jwt.Secret = $env:JWT_SECRET
$settings | ConvertTo-Json -Depth 10 | Set-Content -Path $prod -Encoding UTF8
Write-Host "Wrote appsettings.Production.json"
# Restrict ACL
icacls $prod /inheritance:r | Out-Null
icacls $prod /grant:r 'Administrators:(R,W)' 'IIS AppPool\SolutionErp-Api:(R)' | Out-Null
# Deploy fe-admin
Remove-Item -Path 'C:\inetpub\solution-erp\fe-admin\*' -Recurse -Force -Exclude 'web.config' -ErrorAction SilentlyContinue
Copy-Item -Path 'fe-admin\dist\*' -Destination 'C:\inetpub\solution-erp\fe-admin\' -Recurse -Force
# Deploy fe-user
Remove-Item -Path 'C:\inetpub\solution-erp\fe-user\*' -Recurse -Force -Exclude 'web.config' -ErrorAction SilentlyContinue
Copy-Item -Path 'fe-user\dist\*' -Destination 'C:\inetpub\solution-erp\fe-user\' -Recurse -Force
# Restart app pool
Start-WebAppPool -Name SolutionErp-Api
Write-Host "Deploy done. App pool started."
- name: Smoke test
if: github.ref == 'refs/heads/main'
shell: powershell
run: |
Start-Sleep -Seconds 10
try {
$r = Invoke-WebRequest -Uri 'https://api.solutions.com.vn/health/live' -TimeoutSec 30 -UseBasicParsing
Write-Host "API /health/live -> $($r.StatusCode)"
} catch {
Write-Warning "API smoke test: $_"
}