Files
myoffice_public/proxmox/sync-public.ps1
T

157 lines
4.6 KiB
PowerShell

# Sync full source from the private repo to the public Gitea repo (manual build + Proxmox scripts).
# Usage:
# $env:GITEA_TOKEN = '<token>'
# .\proxmox\sync-public.ps1 -GiteaUrl https://gitea.example.com -Owner org -Repo myoffice-public -Push
#
# Copies tracked git files (plus generates public README.md). Skips private CI, secrets, build junk.
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$GiteaUrl,
[Parameter(Mandatory = $true)]
[string]$Owner,
[string]$Repo = 'myoffice-public',
[string]$Branch = 'master',
[string]$Token = $env:GITEA_TOKEN,
[string]$WorkDir = '',
[switch]$Push
)
$ErrorActionPreference = 'Stop'
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$repoRoot = Resolve-Path (Join-Path $scriptDir '..')
Set-Location $repoRoot
$GiteaUrl = $GiteaUrl.TrimEnd('/')
if (-not $WorkDir) {
$WorkDir = Join-Path $env:TEMP 'myoffice-public-sync'
}
function Test-SkipPath([string]$RelPath) {
$n = $RelPath -replace '\\', '/'
if ($n -eq '.gitea' -or $n.StartsWith('.gitea/')) { return $true }
if ($n -eq '_Published' -or $n.StartsWith('_Published/')) { return $true }
if ($n -eq 'myoffice-publish.tar.gz') { return $true }
if ($n -eq 'Docker/data' -or $n.StartsWith('Docker/data/')) { return $true }
if ($n -eq '.env' -or $n.StartsWith('.env.') -or $n -eq 'env.local') { return $true }
if ($n -eq 'proxmox/README.public.md') { return $true }
if ($n.EndsWith('.user') -or $n.EndsWith('.suo')) { return $true }
return $false
}
if ($Token) {
$hostPart = $GiteaUrl -replace '^https://', '' -replace '^http://', ''
$scheme = if ($GiteaUrl -match '^http://') { 'http' } else { 'https' }
$remote = "${scheme}://oauth2:${Token}@${hostPart}/${Owner}/${Repo}.git"
}
else {
$remote = "${GiteaUrl}/${Owner}/${Repo}.git"
Write-Host "No token — using remote as-is (SSH URL recommended): $remote"
}
Write-Host "-----------------------------------------------"
Write-Host "Sync full source -> $Owner/$Repo ($Branch)"
Write-Host "WorkDir: $WorkDir"
Write-Host "-----------------------------------------------"
if (Test-Path $WorkDir) {
Remove-Item -Recurse -Force $WorkDir
}
New-Item -ItemType Directory -Path $WorkDir | Out-Null
$cloned = $false
git clone --depth 1 --branch $Branch $remote $WorkDir 2>$null
if ($LASTEXITCODE -eq 0) {
$cloned = $true
}
if (-not $cloned) {
Write-Host "Clone failed or empty repo — initializing new git repo..."
git -C $WorkDir init -b $Branch
if ($LASTEXITCODE -ne 0) {
git -C $WorkDir init
git -C $WorkDir checkout -b $Branch 2>$null
}
git -C $WorkDir remote remove origin 2>$null
git -C $WorkDir remote add origin $remote
}
Get-ChildItem -Path $WorkDir -Force | Where-Object { $_.Name -ne '.git' } | ForEach-Object {
Remove-Item $_.FullName -Recurse -Force
}
$tracked = git -C $repoRoot ls-files
$copied = 0
$skipped = 0
foreach ($f in $tracked) {
if (Test-SkipPath $f) {
$skipped++
continue
}
$src = Join-Path $repoRoot $f
if (-not (Test-Path -LiteralPath $src)) {
continue
}
$dest = Join-Path $WorkDir $f
$destDir = Split-Path -Parent $dest
if (-not (Test-Path $destDir)) {
New-Item -ItemType Directory -Path $destDir -Force | Out-Null
}
Copy-Item -LiteralPath $src -Destination $dest -Force
$copied++
}
Write-Host "Copied $copied tracked files (skipped $skipped)."
$repoRawBase = "$GiteaUrl/$Owner/$Repo/raw/branch/$Branch"
$template = Join-Path $scriptDir 'README.public.md'
if (-not (Test-Path $template)) {
throw "Missing $template"
}
$readme = Get-Content -LiteralPath $template -Raw -Encoding utf8
$readme = $readme.
Replace('__GITEA_URL__', $GiteaUrl).
Replace('__GITEA_OWNER__', $Owner).
Replace('__GITEA_REPO__', $Repo).
Replace('__GITEA_BRANCH__', $Branch).
Replace('__REPO_RAW_BASE__', $repoRawBase)
Set-Content -LiteralPath (Join-Path $WorkDir 'README.md') -Value $readme -Encoding utf8 -NoNewline
Write-Host "Wrote public README.md"
git -C $WorkDir add -A
$status = git -C $WorkDir status --porcelain
if (-not $status) {
Write-Host "No changes to commit."
}
else {
git -C $WorkDir -c user.email='myoffice-sync@local' -c user.name='myoffice-sync' `
commit -m "sync full source from private myoffice"
Write-Host "Committed public snapshot."
}
if ($Push) {
Write-Host "Pushing to origin $Branch..."
git -C $WorkDir push -u origin "HEAD:$Branch"
if ($LASTEXITCODE -ne 0) {
throw "git push failed with exit code $LASTEXITCODE"
}
Write-Host "Pushed."
}
else {
Write-Host "Dry run (no push). Re-run with -Push to publish."
Write-Host "Work tree left at: $WorkDir"
}
Write-Host ""
Write-Host "Proxmox one-liner:"
Write-Host (' bash -c "$(curl -fsSL {0}/proxmox/myoffice.sh)"' -f $repoRawBase)
Write-Host ""