1
0
forked from Eeveid/lightOps
Files
lightOps/scripts/publish-gitea-release.ps1

121 lines
3.1 KiB
PowerShell
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
param(
[string]$GiteaUrl = $(if ($env:GITEA_URL) { $env:GITEA_URL } else { "https://gitea.kmux.cn" }),
[string]$Owner = $(if ($env:GITEA_OWNER) { $env:GITEA_OWNER } else { "Eeveid" }),
[string]$Repo = $(if ($env:GITEA_REPO) { $env:GITEA_REPO } else { "lightOps" }),
[Parameter(Mandatory = $true)][string]$Tag,
[string]$Title = "",
[string]$Notes = "",
[string]$Target = "main",
[Parameter(Mandatory = $true)][string[]]$Package,
[switch]$NoCreateTag,
[switch]$NoPushTag,
[switch]$Prerelease,
[switch]$SkipCertificateCheck
)
$ErrorActionPreference = "Stop"
if (-not $env:GITEA_TOKEN) {
throw "必须设置环境变量 GITEA_TOKEN"
}
if (-not $Title) {
$Title = $Tag
}
foreach ($item in $Package) {
if (-not (Test-Path -LiteralPath $item -PathType Leaf)) {
throw "发布包不存在:$item"
}
}
function Write-Step {
param([string]$Message)
Write-Host "[LightOps] $Message" -ForegroundColor Green
}
function Api-Url {
param([string]$Path)
"$($GiteaUrl.TrimEnd('/'))/api/v1/repos/$Owner/$Repo/$Path"
}
function Invoke-Gitea {
param(
[string]$Method,
[string]$Uri,
$Body = $null,
[string]$ContentType = "application/json"
)
$headers = @{ Authorization = "token $env:GITEA_TOKEN" }
$args = @{
Method = $Method
Uri = $Uri
Headers = $headers
}
if ($Body -ne $null) {
$args.Body = $Body
$args.ContentType = $ContentType
}
if ($SkipCertificateCheck) {
$args.SkipCertificateCheck = $true
}
Invoke-RestMethod @args
}
if (-not $NoCreateTag) {
git rev-parse $Tag *> $null
if ($LASTEXITCODE -ne 0) {
Write-Step "创建本地 tag$Tag"
git tag -a $Tag $Target -m $Title
if ($LASTEXITCODE -ne 0) {
throw "创建 tag 失败"
}
}
}
if (-not $NoPushTag) {
Write-Step "推送 tag$Tag"
git push origin $Tag
if ($LASTEXITCODE -ne 0) {
throw "推送 tag 失败"
}
}
$release = $null
try {
$release = Invoke-Gitea -Method Get -Uri (Api-Url "releases/tags/$Tag")
Write-Step "Release 已存在:$Tag"
} catch {
Write-Step "创建 Release$Tag"
$payload = @{
tag_name = $Tag
target_commitish = $Target
name = $Title
body = $Notes
draft = $false
prerelease = [bool]$Prerelease
} | ConvertTo-Json -Depth 5
$release = Invoke-Gitea -Method Post -Uri (Api-Url "releases") -Body $payload
}
foreach ($item in $Package) {
$file = Get-Item -LiteralPath $item
Write-Step "上传附件:$($file.Name)"
$headers = @{ Authorization = "token $env:GITEA_TOKEN" }
$uri = "$(Api-Url "releases/$($release.id)/assets")?name=$([uri]::EscapeDataString($file.Name))"
$form = @{ attachment = $file }
$args = @{
Method = "Post"
Uri = $uri
Headers = $headers
Form = $form
}
if ($SkipCertificateCheck) {
$args.SkipCertificateCheck = $true
}
Invoke-RestMethod @args | Out-Null
}
Write-Host ""
Write-Host "Release 发布完成:$($GiteaUrl.TrimEnd('/'))/$Owner/$Repo/releases/tag/$Tag"