How to set version of the prerelease NuGet according to the latest released NuGet automatically?

强颜欢笑 提交于 2021-02-08 09:51:44

问题


I have two pipelines, release and prerelease.

In release pipeline version set up like this, the counter for the patch and manually set major/minor version:

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'
  majorVersion: '1'  
  minorVersion: '1'
  patchVersion: $[counter(format('{0}.{1}', variables['majorVersion'], variables['minorVersion']), 0)]
  productVersion: $[format('{0}.{1}.{2}', variables['majorVersion'], variables['minorVersion'], variables['patchVersion'])]    

In prerelease pipeline I manual check what is the version for all major, minor and patch from the latest release, I only add Build.BuildNumber in the end to declare it as a prerelease:

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'
  majorVersion: '1'  
  minorVersion: '1'
  patchVersion: '2' <!-- if the last release patch version was 1, in the next prerelease I need here 2-->
  productVersionBeta: $[format('{0}.{1}.{2}-{3}', variables['majorVersion'], variables['minorVersion'], variables['patchVersion'], variables['Build.BuildNumber'])] 

In the end I pack and push NuGet.


What I would like to have in my prerelease pipeline is:

variables:
 solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

  majorVersion: $(SOMEHOW_GET_LATEST_MAJOR_RELEASE_VERSION) 
  minorVersion: $(SOMEHOW_GET_LATEST_MINOR_RELEASE_VERSION) 
  patchVersion: $(SOMEHOW_GET_LATEST_PATCH_RELEASE_VERSION) + 1

  productVersionBeta: $[format('{0}.{1}.{2}-{3}', variables['majorVersion'], variables['minorVersion'], variables['patchVersion'], variables['Build.BuildNumber'])]

Is it possible to get the behavior as described with only yaml config because I didn't find anything about it?

If it's not possible to do just through the config then I found this REST API The Artifact Details - Get Package Version. Is there a clean way to implement it inside the pipeline for the same purposes?


回答1:


I think that this is not possible out of the box and you need to use REST API. Please check this example:

variables:
  orgName: 'thecodemanual'
  packageId: '8e0deb67-89bd-499f-ae8c-1d3814be540a'

steps:
    - pwsh: |
       
        $url = "https://feeds.dev.azure.com/$(orgName)/$(System.TeamProject)/_apis/packaging/Feeds/devops-manual/Packages/$(packageId)?api-version=6.0-preview.1"
        $packageInfo = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}
        Write-Host "Pipeline = $($packageInfo | ConvertTo-Json -Depth 100)"

        $version = $packageInfo.versions[0].version

        Write-Host $version

        $sp = $version.Split(".")

        echo $sp

        $majorVersion = $sp[0]
        $minorVersion = $sp[1]
        $patchVersion = [int]$sp[2] + 1
        $productVersionBeta = “$majorVersion.$minorVersion.$patchVersion-$(Build.BuildNumber)”

        Write-Host "##vso[task.setvariable variable=packageVersion]$version"
        Write-Host "##vso[task.setvariable variable=majorVersion]$majorVersion"
        Write-Host "##vso[task.setvariable variable=minorVersion]$minorVersion"
        Write-Host "##vso[task.setvariable variable=patchVersion]$patchVersion"
        Write-Host "##vso[task.setvariable variable=productVersionBeta]$productVersionBeta"
      name: initial
    - pwsh: |
        Write-Host 'packageVersion: $(packageVersion)'
        Write-Host 'majorVersion: $(majorVersion)'
        Write-Host 'minorVersion: $(minorVersion)'
        Write-Host 'patchVersion: $(patchVersion)'
        Write-Host 'productVersionBeta: $(productVersionBeta)'

Latest version of my package (I got packageId from this endpoint https://feeds.dev.azure.com/{{organization}}/{{project}}/_apis/packaging/Feeds/devops-manual/Packages?api-version=6.0-preview.1) is 2.1.2 and with above pipeline I get these values:

packageVersion: 2.1.2
majorVersion: 2
minorVersion: 1
patchVersion: 3
productVersionBeta: 2.1.3-20200901.6



回答2:


There is an easier way to auto set preview version of nuget.

It is GitVersion.

With the following GitVersion.yml

next-version: 1.1
mode: ContinuousDelivery
increment: Inherit
tag-prefix: '[vV]'
#source-branches: ['master', 'develop', 'hotfix']
ignore:
  sha: []
  commits-before: 2018-01-01T00:00:00
branches:
  master:
    regex: master$
    mode: ContinuousDelivery
    tag: ''
    increment: Patch
    prevent-increment-of-merged-branch-version: true
    track-merge-target: false
    tracks-release-branches: false
    is-release-branch: true
  release:
    regex: r(elease$|(eleases)?[-/])
    mode: ContinuousDelivery
    tag: beta
    increment: Patch
    prevent-increment-of-merged-branch-version: true
    track-merge-target: false
    tracks-release-branches: false
    is-release-branch: true
  feature:
    regex: f(eatures)?[-/]
    mode: ContinuousDeployment
    tag: alpha
    increment: Minor
    prevent-increment-of-merged-branch-version: false
    track-merge-target: false
    tracks-release-branches: false
    is-release-branch: false

GitVersion can auto set version of Nuget. If I tag a commit in master branch with v1.1.0,Then I commit and push an another in Development branch,The 1.1.0-alpha.x will be created in Artifact.

For more information, please refer to the blog:Automatically increase the semantic version using GitVersion



来源:https://stackoverflow.com/questions/63689076/how-to-set-version-of-the-prerelease-nuget-according-to-the-latest-released-nuge

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!