feat(deploy): smart update script with skip-if-no-changes and silent mode
Only pulls and restarts the service when new commits exist. Supports -Silent flag for Task Scheduler (logs to update.log). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
63
update.ps1
63
update.ps1
@@ -1,35 +1,72 @@
|
|||||||
# GoMag Vending - Update Script
|
# GoMag Vending - Update Script
|
||||||
# Ruleaza ca Administrator: .\update.ps1
|
# Ruleaza interactiv: .\update.ps1
|
||||||
|
# Ruleaza din scheduler: .\update.ps1 -Silent
|
||||||
|
|
||||||
|
param(
|
||||||
|
[switch]$Silent
|
||||||
|
)
|
||||||
|
|
||||||
$RepoPath = "C:\gomag-vending"
|
$RepoPath = "C:\gomag-vending"
|
||||||
$TokenFile = Join-Path $RepoPath ".gittoken"
|
$TokenFile = Join-Path $RepoPath ".gittoken"
|
||||||
|
$LogFile = Join-Path $RepoPath "update.log"
|
||||||
|
|
||||||
|
function Log($msg, $color = "White") {
|
||||||
|
$ts = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
|
||||||
|
if ($Silent) {
|
||||||
|
Add-Content -Path $LogFile -Value "$ts $msg"
|
||||||
|
} else {
|
||||||
|
Write-Host $msg -ForegroundColor $color
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
# Citire token
|
# Citire token
|
||||||
if (-not (Test-Path $TokenFile)) {
|
if (-not (Test-Path $TokenFile)) {
|
||||||
Write-Host "EROARE: $TokenFile nu exista!" -ForegroundColor Red
|
Log "EROARE: $TokenFile nu exista!" "Red"
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
$token = (Get-Content $TokenFile -Raw).Trim()
|
$token = (Get-Content $TokenFile -Raw).Trim()
|
||||||
|
|
||||||
# Git pull
|
# Fetch remote
|
||||||
Write-Host "==> Git pull..." -ForegroundColor Cyan
|
|
||||||
Set-Location $RepoPath
|
Set-Location $RepoPath
|
||||||
git -c credential.helper="" pull "https://gomag-vending:$token@gitea.romfast.ro/romfast/gomag-vending.git"
|
git -c credential.helper="" fetch "https://gomag-vending:$token@gitea.romfast.ro/romfast/gomag-vending.git" main 2>&1 | Out-Null
|
||||||
if ($LASTEXITCODE -ne 0) {
|
if ($LASTEXITCODE -ne 0) {
|
||||||
Write-Host "EROARE: git pull esuat!" -ForegroundColor Red
|
Log "EROARE: git fetch esuat!" "Red"
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
# Compara local vs remote
|
||||||
|
$local = git rev-parse HEAD
|
||||||
|
$remote = git rev-parse FETCH_HEAD
|
||||||
|
|
||||||
|
if ($local -eq $remote) {
|
||||||
|
Log "Nicio actualizare disponibila." "Gray"
|
||||||
|
exit 0
|
||||||
|
}
|
||||||
|
|
||||||
|
# Exista update-uri
|
||||||
|
$commits = git log --oneline "$local..$remote"
|
||||||
|
Log "==> Update disponibil ($($commits.Count) commit-uri noi)" "Cyan"
|
||||||
|
if (-not $Silent) {
|
||||||
|
$commits | ForEach-Object { Write-Host " $_" -ForegroundColor DarkGray }
|
||||||
|
}
|
||||||
|
|
||||||
|
# Git pull
|
||||||
|
Log "==> Git pull..." "Cyan"
|
||||||
|
git -c credential.helper="" pull "https://gomag-vending:$token@gitea.romfast.ro/romfast/gomag-vending.git" 2>&1 | Out-Null
|
||||||
|
if ($LASTEXITCODE -ne 0) {
|
||||||
|
Log "EROARE: git pull esuat!" "Red"
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
# Pip install (daca s-au schimbat dependintele)
|
# Pip install (daca s-au schimbat dependintele)
|
||||||
Write-Host "==> Verificare dependinte..." -ForegroundColor Cyan
|
Log "==> Verificare dependinte..." "Cyan"
|
||||||
& "$RepoPath\venv\Scripts\pip.exe" install -r "$RepoPath\api\requirements.txt" --quiet
|
& "$RepoPath\venv\Scripts\pip.exe" install -r "$RepoPath\api\requirements.txt" --quiet 2>&1 | Out-Null
|
||||||
|
|
||||||
# Restart serviciu
|
# Restart serviciu
|
||||||
Write-Host "==> Restart GoMagVending..." -ForegroundColor Cyan
|
Log "==> Restart GoMagVending..." "Cyan"
|
||||||
nssm restart GoMagVending
|
nssm restart GoMagVending 2>&1 | Out-Null
|
||||||
|
|
||||||
Start-Sleep -Seconds 3
|
Start-Sleep -Seconds 3
|
||||||
$status = nssm status GoMagVending 2>&1
|
$status = nssm status GoMagVending 2>&1
|
||||||
Write-Host "Serviciu: $status" -ForegroundColor Green
|
Log "Serviciu: $status" "Green"
|
||||||
Write-Host ""
|
Log "Update complet!" "Green"
|
||||||
Write-Host "Update complet! Acces: http://localhost/gomag/" -ForegroundColor Green
|
|
||||||
|
|||||||
Reference in New Issue
Block a user