通过Powershell脚本及Azure Pipelines自动
2020-04-12 本文已影响0人
Weidaicheng
背景
之前为极光的JPush写过一个扩展类库来实现在.Net Core中的依赖注入,当时覆盖了JPush的1.0.0,1.1.0,1.2.0三个版本。
前几天收到一个issue让我更新一下版本,检查之后发现JPush当前(2020.04.10)最新版本为1.2.4,由于每次手动更新版本比较繁琐,就想能不能用Powershell脚本+Azure Pipelines每隔一段时间自动生成类库发布到Nuget上。
思路
1.通过Nuget Api查询并对比当前扩展类库及引用类库的版本;
2.运行dotnet remove package
与dotnet add package
命令移除并重新添加新版本包;
3.通过xml修改项目文件中的包metadata;
4.运行dotnet pack
命令打包;
5.通过Azure Pipelines中的Nuget push上传包;
核心Powershell代码
# get version information fro Nuget
$jpushVersions = Invoke-RestMethod -Uri https://api.nuget.org/v3-flatcontainer/Jiguang.JPush/index.json
$jpushExtVersions = Invoke-RestMethod -Uri https://api.nuget.org/v3-flatcontainer/Jiguang.JPush.Extensions/index.json
Foreach($version in $jpushVersions.versions | Where-Object { $jpushExtVersions.versions -notcontains $_ })
{
Write-Output "Jiguang.JPush version $version"
# remove old nuget package
Write-Output " Removing old package..."
dotnet remove package Jiguang.JPush
# add new version of nuget package
Write-Output " Adding new package..."
dotnet add package Jiguang.JPush -v $version
# modifiy package metedata
Write-Output " Modifying package metedata..."
[xml] $projectXmlDoc = Get-Content $projectFile
$projectXmlDoc.Project.PropertyGroup.Version = $version.ToString()
$projectXmlDoc.Project.PropertyGroup.PackageVersion = $version.ToString()
$projectXmlDoc.Project.PropertyGroup.PackageReleaseNotes = $version.ToString() + " release"
$projectXmlDoc.Save($projectFile)
# pack package
Write-Output " Packing package..."
dotnet pack --configuration Release
if ($LASTEXITCODE -ne 0) {
Write-Error "Pack faled for version: $version."
exit
}
Write-Output ""
}