【问题标题】:Azure Pipelines Conditional and Persistent VariablesAzure Pipelines 条件变量和持久变量
【发布时间】:2020-06-02 21:03:20
【问题描述】:

我正在尝试设置一个 Azure Pipeline,它会根据正在构建的分支自动更新版本号。给定一个master 分支、一个develop 分支和一个{major.minor.patch.build} 形式的版本,我想实现以下目标:

每当管道被master触发时:

  • patch 加 1
  • build 重置为 0

每当管道被develop触发时:

  • build 加 1

假设起始版本为 1.0.0.0,我希望在多次运行后版本号看起来像这样的示例:

  • develop 上运行 1:1.0.0.1
  • develop 上运行 2:1.0.0.2
  • master 上运行 3:1.0.1.0
  • master 上运行 4:1.0.2.0
  • develop 上运行 5:1.0.2.1
  • develop 上运行 6:1.0.2.2
  • master 上运行 7:1.0.3.0

我的第一个想法是为 patchbuild 值使用计数器,但似乎没有办法有条件地增加它们或重置计数器。我的下一个想法是使用几个基于分支有条件执行的脚本任务,例如:

    #Update build
    - powershell: echo "##vso[task.setvariable variable=version.build;isOutput=true]$(version.build + 1)"
      condition: eq(variables['Build.SourceBranch'], 'refs/heads/develop')

    #Reset build number
    - powershell: echo "##vso[task.setvariable variable=version.build;isOutput=true]0"
      condition: eq(variables['Build.SourceBranch'], 'refs/heads/master')

    #Update patch
    - powershell: echo "##vso[task.setvariable variable=version.patch;isOutput=true]$(version.patch + 1)"
      condition: eq(variables['Build.SourceBranch'], 'refs/heads/master')

但是,这种方法在运行中并不持久(更不用说$(version.patch + 1) 不起作用。我找不到有关如何以这种方式实际增加变量的任何信息)。我的最后一个想法是在存储库中有一个版本文件并使用它,但我真的不想走这条路。

有没有办法使用 Pipelines 来实现我正在寻找的东西,或者我是否在试图以这种方式进行尝试?

编辑 我应该在问题中提到,此时我有一个管道设置来构建我的应用程序,但对版本控制没有任何作用。我现在正在寻找的是一种动态更新一些持久变量/值的方法(特别是patchbuild)。

【问题讨论】:

    标签: azure-devops azure-pipelines


    【解决方案1】:

    所以有一个可用的计数器变量可以处理一些what you want

    使用它,我能够部分地让它与你所追求的东西一起工作。

    trigger:
    - master
    
    pool:
      vmImage: 'ubuntu-latest'
    
    variables:
      major: 1
      minor: 0
      # define b as a counter with the prefix as variable a, and seed as 100.
      #patch: $[counter(variables['major'], 1)]
    
      ${{ if eq( variables['Build.SourceBranchName'], 'master') }}:
        patch: $[counter(variables['major'], 1)]
        build: 0
    
      ${{ if eq( variables['Build.SourceBranchName'], 'develop') }}:
        build: $[counter(variables['major'], 1)]
        #patch: ?
    
    
    name: $(major).$(minor).$(patch).$(build)
    
    steps:
        - bash: echo $(major).$(minor).$(patch).$(build)
    

    但据我所知,当构建在开发时运行时,没有一种很好的方法来获取当前的 $(patch) 计数器变量。旁注我很想知道如何联系custom counter values。您可能会使用 API 并从上次运行中获取 Buildnumber 并提取补丁号并重用它,但这会导致您提出这个问题。

    有没有办法使用管道实现我正在寻找的东西,或者我是 试图以这种方式去吠叫错误的树?

    我会争辩改变你的方法。如果您正在使用 powershell 或 yaml 制作自定义版本控制方案,那么我会说版本控制对于您使用 GitVersion 或类似的东西来说已经足够重要了。入门有一个学习曲线,但默认的开箱即用 GitVersion 模式之一可能会满足您的目的。

    有一个管道task here 将为您处理计算版本,如果您只是将它添加到您的管道中。

    - task: GitVersion@5
      inputs:
        runtime: 'core'
        configFilePath: 'GitVersion.yml'
    

    最好提供一个 GitVersion.yml,它是如何进行版本控制的方案或指令集。

    有一个chocolatey package,您可以安装它以在本地进行试验。如果您在 repo 的根目录中 run GitVersion init ,它将引导您完成一些版本控制选项,并为您创建 GitVersion.yml。

    mode: Mainline
    branches:
      master:
        tag: ''
        regex: master
        increment: minor
      develop:
        tag: ''
        regex: develop
        increment: patch
    ignore:
      sha: []
    merge-message-formats: {}
    

    作为一个例子,上面的股票主线模式给了我:

    【讨论】:

      【解决方案2】:

      我有一个解决方法可以使用 restful api 来实现这一点。

      例如,您想将上述版本标记添加到构建中。您可以先使用get tag api 获取上一个构建的标签。然后您可以使用脚本更新版本并使用更新的标签进行更新。您可以在 powershell 任务中参考以下脚本。

      # first get previous build id
      
      $url = "$(System.TeamFoundationCollectionUri)$(System.TeamProject)/_apis/build/builds?definitions={pipeline Id}&`$top=2&api-version=5.1"
      
      $result = Invoke-RestMethod -Uri $url -Headers @{authorization = "Bearer $(System.AccessToken)"} -Method get
      
      $builds = $result.value
      $bid = $builds[1].id
      
      # get the build tag with get build tag api
      
      $urltag ="$(System.TeamFoundationCollectionUri)$(System.TeamProject)/_apis/build/builds/$bid/tags?api-version=5.1"
      
      $tagResult = Invoke-RestMethod -Uri $urltag -Headers @{authorization = "Bearer $(System.AccessToken)"} -Method get
      
      # get the tag and update it
      
      if($tagResult.value)
      {
         $tagarr = [regex]::split($result.value, '\.')
         $patch = $tagarr[2]
         $build = $tagarr[3]
         $tag=""
         if("$(Build.SourceBranch)" -eq "refs/heads/master")
         {
            $patch = [int]$patch + 1
            # update the variables version.patch and version.build with below scripts, so that it can be referenced in the following tasks.
            #echo "##vso[task.setvariable variable=version.patch;isOutput=true]$patch"
            #echo "##vso[task.setvariable variable=version.build;isOutput=true]0"
      
           $tag = "$(version.major).$(version.minor).$patch.0"    
         }  
         if("$(Build.SourceBranch)" -eq "refs/heads/dev")
         {
            $build= [int]$build+ 1
            #echo "##vso[task.setvariable variable=version.patch;isOutput=true]$patch"
            #echo "##vso[task.setvariable variable=version.build;isOutput=true]$build"
            $tag = "$(version.major).$(version.minor).$patch.$build"  
         }
      
       #add $tag to current build.
        $urltagupdate = "$(System.TeamFoundationCollectionUri)$(System.TeamProject)/_apis/build/builds/$(Build.BuildId)/tags/$($tag)?api-version=5.1"
      
        Invoke-RestMethod -Uri $urltagupdate -Headers @{authorization = "Bearer $(System.AccessToken)"} -Method put
      
      }else 
      {
        #if the build is run for the first time and the tag is not exist.
        #you can omit this part by manually creating a initial tag for the build.
      
        $tag = ""
        if("$(Build.SourceBranchName)" -eq "refs/heads/master")
        {
          $tag = "1.0.1.0"
        }
        if("$(Build.SourceBranchName)" -eq "refs/heads/dev") 
        {
          $tag="1.0.0.1"
        }
      
       #add $tag to current build.
      
        $urltagupdate = "$(System.TeamFoundationCollectionUri)$(System.TeamProject)/_apis/build/builds/$(Build.BuildId)/tags/$($tag)?api-version=5.1"
      
        Invoke-RestMethod -Uri $urltagupdate -Headers @{authorization = "Bearer $(System.AccessToken)"} -Method put
      
      }
      

      以上是构建标签的示例。如果要标记 repo 分支,可以使用refs tag api

      主要思路是通过api获取之前的标签,根据powershell任务中的build.sourcebranch更新version.patchversion.build

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-06-09
        • 2021-09-12
        • 2021-02-10
        • 1970-01-01
        • 2021-05-19
        • 2019-12-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多