【问题标题】:Getting date time in Azure DevOps yml and concat a version number with it在 Azure DevOps yml 中获取日期时间并与之连接版本号
【发布时间】:2021-03-17 15:01:22
【问题描述】:

我有以下 yml。我试图做的是根据其他几个变量(version.Majorversion.MinorversionDayBuildNumber)创建一个版本号。但是设置变量 powershell 任务不会覆盖 versionday 的初始值,因此下面我的 echo 脚本中 versionNumber 的输出将类似于:1.0.set below.2021037.6 有什么想法吗?我没有看到语法问题?

trigger:
  branches:
    include:
      - develop, 
      - sprint/*
      - Sprint/*

pool:
  name: 'MyCustomAgent'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  version.Major: '1'
  version.Minor: '0',
  versionDay: 'set below'
  versionNumber: 'set dynamically'

steps:
- script: |
    echo: $(Build.SourceBranch)

- powershell: |
      if ("$(Build.SourceBranch)".Contains('sprint')) {
        Write-Host "##vso[task.setvariable variable=buildConfiguration;]Release"
        [string] $currentMonthDay= (Get-Date -Format 'MMdd')
        Write-Host "##vso[task.setvariable variable=versionDay]$currentMonthDay"
        Write-Host "##vso[task.setvariable variable=versionNumber]$(version.Major).$(version.Minor).$(versionDay)"

      } else {
        Write-Host "##vso[task.setvariable variable=buildConfiguration;]Debug"
        [string] $currentMonthDay= (Get-Date -Format 'MMdd')
        Write-Host "##vso[task.setvariable variable=versionDay]$currentMonthDay"
        Write-Host "##vso[task.setvariable variable=versionNumber]$(version.Major).$(version.Minor).$(versionDay).$(Build.BuildNumber)"
      }      

- script: | 
    echo building configuration $(buildConfiguration)
    echo $(versionDay)
    echo $(versionNumber)

【问题讨论】:

    标签: azure powershell azure-devops yaml


    【解决方案1】:

    您需要将变量设置为输出 - 请注意以下 setvariable 语句的更改:

    pool:
      name: 'MyCustomAgent'
    
    variables:
      solution: '**/*.sln'
      buildPlatform: 'Any CPU'
      version.Major: '1'
      version.Minor: '0',
      versionDay: 'set below'
      versionNumber: 'set dynamically'
    
    steps:
    - script: |
        echo: $(Build.SourceBranch)
    
    - powershell: |
          # note the use of ;isOutput=true in setvariable commands
          if ("$(Build.SourceBranch)".Contains('sprint')) {
            Write-Host "##vso[task.setvariable variable=buildConfiguration;]Release"
            [string] $currentMonthDay= (Get-Date -Format 'MMdd')
            Write-Host "##vso[task.setvariable variable=versionDay]$currentMonthDay"
            # You can't use $(versionDay) here as setvariable is for subsequent steps/jobs/stages
            # use its calculated value instead
            Write-Host "##vso[task.setvariable variable=versionNumber]$(version.Major).$(version.Minor).$($currentMonthDay)"
    
          } else {
            Write-Host "##vso[task.setvariable variable=buildConfiguration;isOutput=true]Debug"
            [string] $currentMonthDay= (Get-Date -Format 'MMdd')
            Write-Host "##vso[task.setvariable variable=versionDay;isOutput=true]$currentMonthDay"
            # You can't use $(versionDay) here as setvariable is for subsequent steps/jobs/stages
            # use its calculated value instead
            Write-Host "##vso[task.setvariable variable=versionNumber;isOutput=true]$(version.Major).$(version.Minor).$($currentMonthDay).$(Build.BuildNumber)"
          }      
    
    - script: | 
        echo building configuration $(buildConfiguration)
        echo $(versionDay)
        echo $(versionNumber)
    

    【讨论】:

      猜你喜欢
      • 2020-11-28
      • 2020-12-25
      • 1970-01-01
      • 2021-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-17
      • 1970-01-01
      相关资源
      最近更新 更多