- Added -SkipExe switch to allow skipping the upload of the executable and its checksum. - Updated paths for the executable and checksum files to reflect new directory structure. - Improved file existence checks with warnings instead of errors for optional artifacts. - Enhanced release body to include checksum information conditionally based on executable upload. - Refined upload process for MSI and executable, including better error handling and logging.
263 lines
8.8 KiB
PowerShell
263 lines
8.8 KiB
PowerShell
# Create Forgejo Release with Binary Assets
|
|
# Usage: .\create_release.ps1 [-Version 1.0.0]
|
|
# If -Version is not provided, it will be read from src/webdrop_bridge/__init__.py
|
|
# Uses your Forgejo credentials (same as git)
|
|
# First run will prompt for credentials and save them to this session
|
|
|
|
param(
|
|
[Parameter(Mandatory=$false)]
|
|
[string]$Version,
|
|
|
|
[Parameter(Mandatory=$false)]
|
|
[string]$ForgejoUser,
|
|
|
|
[Parameter(Mandatory=$false)]
|
|
[string]$ForgejoPW,
|
|
|
|
[switch]$ClearCredentials,
|
|
|
|
[switch]$SkipExe,
|
|
|
|
[string]$ForgejoUrl = "https://git.him-tools.de",
|
|
[string]$Repo = "HIM-public/webdrop-bridge",
|
|
[string]$ExePath = "build\dist\windows\WebDropBridge\WebDropBridge.exe",
|
|
[string]$ChecksumPath = "build\dist\windows\WebDropBridge\WebDropBridge.exe.sha256"
|
|
)
|
|
|
|
$ErrorActionPreference = "Stop"
|
|
|
|
# Get project root (PSScriptRoot is build/scripts, go up to project root with ..\..)
|
|
$projectRoot = Resolve-Path (Join-Path $PSScriptRoot "..\..")
|
|
|
|
# Resolve file paths relative to project root
|
|
$ExePath = Join-Path $projectRoot $ExePath
|
|
$ChecksumPath = Join-Path $projectRoot $ChecksumPath
|
|
$MsiPath = Join-Path $projectRoot $MsiPath
|
|
|
|
# Function to read version from .env or .env.example
|
|
function Get-VersionFromEnv {
|
|
# Use already resolved project root
|
|
|
|
# Try .env first (runtime config), then .env.example (template)
|
|
$envFile = Join-Path $projectRoot ".env"
|
|
$envExampleFile = Join-Path $projectRoot ".env.example"
|
|
|
|
# Check .env first
|
|
if (Test-Path $envFile) {
|
|
$content = Get-Content $envFile -Raw
|
|
if ($content -match 'APP_VERSION=([^\r\n]+)') {
|
|
Write-Host "Version read from .env" -ForegroundColor Gray
|
|
return $matches[1].Trim()
|
|
}
|
|
}
|
|
|
|
# Fall back to .env.example
|
|
if (Test-Path $envExampleFile) {
|
|
$content = Get-Content $envExampleFile -Raw
|
|
if ($content -match 'APP_VERSION=([^\r\n]+)') {
|
|
Write-Host "Version read from .env.example" -ForegroundColor Gray
|
|
return $matches[1].Trim()
|
|
}
|
|
}
|
|
|
|
Write-Host "ERROR: Could not find APP_VERSION in .env or .env.example" -ForegroundColor Red
|
|
exit 1
|
|
}
|
|
|
|
# Handle --ClearCredentials flag
|
|
if ($ClearCredentials) {
|
|
Remove-Item env:FORGEJO_USER -ErrorAction SilentlyContinue
|
|
Remove-Item env:FORGEJO_PASS -ErrorAction SilentlyContinue
|
|
Write-Host "[OK] Credentials cleared from this session" -ForegroundColor Green
|
|
exit 0
|
|
}
|
|
|
|
# Get credentials from sources (in order of priority)
|
|
if (-not $ForgejoUser) {
|
|
$ForgejoUser = $env:FORGEJO_USER
|
|
}
|
|
|
|
if (-not $ForgejoPW) {
|
|
$ForgejoPW = $env:FORGEJO_PASS
|
|
}
|
|
|
|
# If still no credentials, prompt user interactively
|
|
if (-not $ForgejoUser -or -not $ForgejoPW) {
|
|
Write-Host "Forgejo credentials not found. Enter your credentials:" -ForegroundColor Yellow
|
|
|
|
if (-not $ForgejoUser) {
|
|
$ForgejoUser = Read-Host "Username"
|
|
}
|
|
|
|
if (-not $ForgejoPW) {
|
|
$securePass = Read-Host "Password" -AsSecureString
|
|
$ForgejoPW = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToCoTaskMemUnicode($securePass))
|
|
}
|
|
|
|
# Save credentials to environment for this session
|
|
$env:FORGEJO_USER = $ForgejoUser
|
|
$env:FORGEJO_PASS = $ForgejoPW
|
|
Write-Host "[OK] Credentials saved to this PowerShell session" -ForegroundColor Green
|
|
Write-Host "Tip: Credentials will persist until you close PowerShell or run: .\create_release.ps1 -ClearCredentials" -ForegroundColor Gray
|
|
}
|
|
|
|
# Verify Version parameter - if not provided, read from .env.example
|
|
if (-not $Version) {
|
|
Write-Host "Version not provided, reading from .env.example..." -ForegroundColor Cyan
|
|
$Version = Get-VersionFromEnv
|
|
Write-Host "Using version: $Version" -ForegroundColor Green
|
|
}
|
|
|
|
# Define MSI path with resolved version
|
|
$MsiPath = Join-Path $projectRoot "build\dist\windows\WebDropBridge-$Version-Setup.msi"
|
|
|
|
# Verify files exist (exe/checksum optional, MSI required)
|
|
if (-not $SkipExe) {
|
|
if (-not (Test-Path $ExePath)) {
|
|
Write-Host "WARNING: Executable not found at $ExePath" -ForegroundColor Yellow
|
|
Write-Host " Use -SkipExe flag to skip exe upload" -ForegroundColor Gray
|
|
$SkipExe = $true
|
|
}
|
|
|
|
if (-not $SkipExe -and -not (Test-Path $ChecksumPath)) {
|
|
Write-Host "WARNING: Checksum file not found at $ChecksumPath" -ForegroundColor Yellow
|
|
Write-Host " Exe will not be uploaded" -ForegroundColor Gray
|
|
$SkipExe = $true
|
|
}
|
|
}
|
|
|
|
# MSI is the primary release artifact
|
|
if (-not (Test-Path $MsiPath)) {
|
|
Write-Host "ERROR: MSI installer not found at $MsiPath" -ForegroundColor Red
|
|
Write-Host "Please build with MSI support:" -ForegroundColor Yellow
|
|
Write-Host " python build\scripts\build_windows.py --msi" -ForegroundColor Cyan
|
|
exit 1
|
|
}
|
|
|
|
Write-Host "Creating WebDropBridge $Version release on Forgejo..." -ForegroundColor Cyan
|
|
|
|
# Get file info
|
|
$msiSize = (Get-Item $MsiPath).Length / 1MB
|
|
Write-Host "Primary Artifact: WebDropBridge-$Version-Setup.msi ($([math]::Round($msiSize, 2)) MB)"
|
|
|
|
if (-not $SkipExe) {
|
|
$exeSize = (Get-Item $ExePath).Length / 1MB
|
|
$checksum = Get-Content $ChecksumPath -Raw
|
|
Write-Host "Optional Artifact: WebDropBridge.exe ($([math]::Round($exeSize, 2)) MB)"
|
|
Write-Host " Checksum: $($checksum.Substring(0, 16))..."
|
|
}
|
|
|
|
# Create basic auth header
|
|
$auth = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes("${ForgejoUser}:${ForgejoPW}"))
|
|
|
|
$headers = @{
|
|
"Authorization" = "Basic $auth"
|
|
"Content-Type" = "application/json"
|
|
}
|
|
|
|
# Step 1: Create release
|
|
Write-Host "`nCreating release v$Version..." -ForegroundColor Yellow
|
|
$releaseUrl = "$ForgejoUrl/api/v1/repos/$Repo/releases"
|
|
|
|
# Build release body with checksum info if exe is being uploaded
|
|
$releaseBody = "WebDropBridge v$Version`n`n**Release Artifacts:**`n- MSI Installer (Windows Setup)`n"
|
|
if (-not $SkipExe) {
|
|
$checksum = Get-Content $ChecksumPath -Raw
|
|
$releaseBody += "- Portable Executable`n`n**Checksum:**`n$checksum`n"
|
|
}
|
|
|
|
$releaseData = @{
|
|
tag_name = "v$Version"
|
|
name = "WebDropBridge v$Version"
|
|
body = $releaseBody
|
|
draft = $false
|
|
prerelease = $false
|
|
} | ConvertTo-Json
|
|
|
|
try {
|
|
$response = Invoke-WebRequest -Uri $releaseUrl `
|
|
-Method POST `
|
|
-Headers $headers `
|
|
-Body $releaseData `
|
|
-TimeoutSec 30 `
|
|
-UseBasicParsing `
|
|
-ErrorAction Stop
|
|
|
|
$releaseInfo = $response.Content | ConvertFrom-Json
|
|
$releaseId = $releaseInfo.id
|
|
Write-Host "[OK] Release created (ID: $releaseId)" -ForegroundColor Green
|
|
}
|
|
catch {
|
|
Write-Host "ERROR creating release: $_" -ForegroundColor Red
|
|
exit 1
|
|
}
|
|
|
|
# Setup curl authentication
|
|
$curlAuth = "$ForgejoUser`:$ForgejoPW"
|
|
$uploadUrl = "$ForgejoUrl/api/v1/repos/$Repo/releases/$releaseId/assets"
|
|
|
|
# Step 2: Upload MSI installer as primary artifact
|
|
Write-Host "`nUploading MSI installer (primary artifact)..." -ForegroundColor Yellow
|
|
|
|
try {
|
|
$response = curl.exe -s -X POST `
|
|
-u $curlAuth `
|
|
-F "attachment=@$MsiPath" `
|
|
$uploadUrl
|
|
|
|
if ($response -like "*error*" -or $response -like "*404*") {
|
|
Write-Host "ERROR uploading MSI: $response" -ForegroundColor Red
|
|
exit 1
|
|
}
|
|
|
|
Write-Host "[OK] MSI installer uploaded" -ForegroundColor Green
|
|
}
|
|
catch {
|
|
Write-Host "ERROR uploading MSI: $_" -ForegroundColor Red
|
|
exit 1
|
|
}
|
|
|
|
# Step 3: Upload executable as optional artifact (if available)
|
|
if (-not $SkipExe) {
|
|
Write-Host "`nUploading executable (optional portable version)..." -ForegroundColor Yellow
|
|
|
|
try {
|
|
$response = curl.exe -s -X POST `
|
|
-u $curlAuth `
|
|
-F "attachment=@$ExePath" `
|
|
$uploadUrl
|
|
|
|
if ($response -like "*error*" -or $response -like "*404*") {
|
|
Write-Host "WARNING: Could not upload executable: $response" -ForegroundColor Yellow
|
|
}
|
|
else {
|
|
Write-Host "[OK] Executable uploaded" -ForegroundColor Green
|
|
}
|
|
}
|
|
catch {
|
|
Write-Host "WARNING: Could not upload executable: $_" -ForegroundColor Yellow
|
|
}
|
|
|
|
# Step 4: Upload checksum as asset
|
|
Write-Host "Uploading checksum..." -ForegroundColor Yellow
|
|
|
|
try {
|
|
$response = curl.exe -s -X POST `
|
|
-u $curlAuth `
|
|
-F "attachment=@$ChecksumPath" `
|
|
$uploadUrl
|
|
|
|
if ($response -like "*error*" -or $response -like "*404*") {
|
|
Write-Host "WARNING: Could not upload checksum: $response" -ForegroundColor Yellow
|
|
}
|
|
else {
|
|
Write-Host "[OK] Checksum uploaded" -ForegroundColor Green
|
|
}
|
|
}
|
|
catch {
|
|
Write-Host "WARNING: Could not upload checksum: $_" -ForegroundColor Yellow
|
|
}
|
|
}
|
|
|
|
Write-Host "`n[OK] Release complete!" -ForegroundColor Green
|
|
Write-Host "View at: $ForgejoUrl/$Repo/releases/tag/v$Version" -ForegroundColor Cyan
|