通过Powershell脚本及Azure Pipelines自动发布.Net Core扩展类库

背景

之前为极光的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查询并对比当前扩展类库及引用类库的版本;

版本返回结果.png

2.运行dotnet remove packagedotnet 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 ""
}

其他

详细Powershell代码请点击查看
详细Azure Pipelines配置请点击查看

你可能感兴趣的:(通过Powershell脚本及Azure Pipelines自动发布.Net Core扩展类库)