如何从 GitHub REST API 获取 nuget 包的最新版本号?
我需要将其作为构建脚本的一部分,因此我不需要在构建文件 (PowerShell) 中手动增加版本。
curl -H "Accept: application/vnd.github.v3+json" -u myusername:<mykey> https://api.github.com/orgs/<mycompany>/packages/nuget/<mypackagename>
我在响应中看不到包的实际最新版本号。我期待看到类似 latest_version: 1.0.24
我可以看到有 version_count: 24
但这并不能帮助我构建 1.2.28 等的完整 SEMVER 版本。
答案 0 :(得分:0)
我为此创建了一个 Powershell 辅助函数。
您将需要编辑您的 Powershell 配置文件以使用您自己的 GitHub 个人访问令牌填充 $env:GitHubAPIKey(或者如果您已经拥有该令牌,则只需更改脚本中的变量名称)。
# Source: https://stackoverflow.com/questions/66264349
write-output "Build helpers included"
<#
.SYNOPSIS
This is a helper function that gets the next available NuGet package version from our NuGet package repository on GitHub
.EXAMPLE
GetPackageVersion PackageName
#>
function GetNewPackageVersion
{
[CmdletBinding()]
param(
[Parameter(Position=0,Mandatory=1)][string]$packageName
)
if (!$env:GitHubAPIKey) {
Write-Host "The environment variable `$env:GitHubAPIKey` is empty. Please ensure this is populated in your Powershell profile file at $home\Powershell\profile.ps1" -BackgroundColor red -ForegroundColor Black
exit -1
}
# TODO: If using PowerShell 7 rather than Windows Powershell 5.1, the next couple of lines can be improved by using the built in -authorization command
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "token",$env:GitHubAPIKey )))
$results = Invoke-RestMethod -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Uri https://api.github.com/orgs/<your-organisation>/packages/nuget/$packageName/versions
$v = [version]($results | Select-Object -first 1).name
$newVersion = "$($v.Major).$($v.Minor).$($v.Build+1)"
write-host ("New package version: " + $newVersion) -BackgroundColor green -ForegroundColor Black
return $newVersion
}
用法: ."./BuildHelpers.ps1" # 包含辅助函数
$version = GetNewPackageVersion MyCompany.MyPackageName
echo $version
输出: