Files
healer-man/scripts/publish-gitea-release.ps1
T
2026-08-14 15:56:39 -04:00

82 lines
2.8 KiB
PowerShell

param(
[Parameter(Mandatory = $true)]
[ValidatePattern('^\d+\.\d+\.\d+$')]
[string]$VersionName,
[Parameter(Mandatory = $true)]
[ValidateRange(1, 2147483647)]
[int]$VersionCode,
[string]$ReleaseNotes = 'Healer Man Android update.',
[string]$GiteaBaseUrl = 'https://git.whoagland.com',
[string]$Owner = 'phenom',
[string]$Repository = 'healer-man'
)
$ErrorActionPreference = 'Stop'
$requiredSecrets = @(
'ANDROID_KEYSTORE_FILE',
'ANDROID_KEYSTORE_PASSWORD',
'ANDROID_KEY_ALIAS',
'ANDROID_KEY_PASSWORD',
'GITEA_TOKEN'
)
foreach ($secret in $requiredSecrets) {
if (-not [Environment]::GetEnvironmentVariable($secret)) {
throw "Required environment variable $secret is not set."
}
}
$projectRoot = Split-Path -Parent $PSScriptRoot
$env:ANDROID_VERSION_NAME = $VersionName
$env:ANDROID_VERSION_CODE = [string]$VersionCode
Push-Location $projectRoot
try {
& npm run android:sync
if ($LASTEXITCODE -ne 0) { throw "Android web build and Capacitor sync failed with exit code $LASTEXITCODE" }
& powershell.exe -NoProfile -ExecutionPolicy Bypass -File scripts/runAndroidGradle.ps1 assembleRelease
if ($LASTEXITCODE -ne 0) { throw "Android release build failed with exit code $LASTEXITCODE" }
$apkName = "healer-man-$VersionName-release.apk"
$apkPath = Join-Path $projectRoot "android\app\build\outputs\apk\release\$apkName"
if (-not (Test-Path -LiteralPath $apkPath)) { throw "Expected release APK was not created: $apkPath" }
$checksumPath = "$apkPath.sha256"
$checksum = (Get-FileHash -LiteralPath $apkPath -Algorithm SHA256).Hash.ToLowerInvariant()
[IO.File]::WriteAllText($checksumPath, "$checksum $apkName`n", [Text.UTF8Encoding]::new($false))
$apiRoot = $GiteaBaseUrl.TrimEnd('/') + '/api/v1'
$headers = @{ Authorization = "token $env:GITEA_TOKEN" }
$tag = "v$VersionName"
$releaseBody = @{
tag_name = $tag
target_commitish = 'main'
name = "Healer Man $VersionName"
body = $ReleaseNotes
draft = $false
prerelease = $false
} | ConvertTo-Json
$release = Invoke-RestMethod `
-Method Post `
-Uri "$apiRoot/repos/$Owner/$Repository/releases" `
-Headers $headers `
-ContentType 'application/json' `
-Body $releaseBody
foreach ($attachment in @($apkPath, $checksumPath)) {
$attachmentName = Split-Path -Leaf $attachment
$encodedName = [Uri]::EscapeDataString($attachmentName)
Invoke-RestMethod `
-Method Post `
-Uri "$apiRoot/repos/$Owner/$Repository/releases/$($release.id)/assets?name=$encodedName" `
-Headers $headers `
-ContentType 'application/octet-stream' `
-InFile $attachment | Out-Null
}
Write-Host "Published $tag with $apkName and its SHA-256 checksum."
Write-Host "$GiteaBaseUrl/$Owner/$Repository/releases/tag/$tag"
} finally {
Pop-Location
}