将Visual Studio Team Services / Visual Studio Online Build的drop文件夹更改为程序集版本

时间:2016-01-20 13:34:19

标签: build msbuild azure-devops

我希望我的Visual Studio Team Services Build能够使用我的可执行文件的程序集版本。有没有简单的方法来实现这个目标?

我知道可以将参数传递给构建过程,但是如何在一个步骤中定义参数并在以后使用它?

2 个答案:

答案 0 :(得分:0)

不确定我是否理解你的问题。您可以通过调用task.servariable Logging Command

将变量从构建任务传递回VSTS
##vso[task.setvariable variable=testvar;]testvalue

因此,您可以编写PowerShell脚本来获取程序集版本并将其传递回VSTS。

答案 1 :(得分:0)

首先,将您的内部版本号格式设置为您想要的内容。我使用: $(BuildDefinitionName)_1.0.0 $(Rev:.r) 。 然后,在构建解决方案之前,在PowerShell脚本构建步骤中调用它:

Param(
  [string]$pathToSearch = $env:BUILD_SOURCESDIRECTORY,
  [string]$buildNumber = $env:BUILD_BUILDNUMBER,
  [string]$searchFilter = "AssemblyInfo.*",
  [regex]$pattern = "\d+\.\d+\.\d+\.\d+",
  [regex]$pattern2 = "AssemblyProduct(\()("".*?"")",
  [regex]$pattern3 = "AssemblyCopyright(\()("".*?"")",
  [Parameter(Mandatory=$true)]
  [string]$assemblyProduct,
  [Parameter(Mandatory=$true)]
  [string]$Copyright
)
# Pattern: "AssemblyProduct(\()("".*?"")"
# Replace: "AssemblyProduct(""$assemblyProduct"""

try
{
    if ($buildNumber -match $pattern -ne $true) {
        Write-Host "Could not extract a version from [$buildNumber] using pattern [$pattern]"
        exit 1
    } else {
        $extractedBuildNumber = $Matches[0]
        Write-Host "Using version $extractedBuildNumber"

        gci -Path $pathToSearch -Filter $searchFilter -Recurse | %{
            Write-Host "  -> Changing $($_.FullName)"

            # remove the read-only bit on the file
            sp $_.FullName IsReadOnly $false

            # Get the copyright string from a local file
            #$Copyright = Get-Content TeamBuildScripts\PSScripts\Copyright.txt


            # run the regex replace
            (gc $_.FullName) | % { $_ -replace $pattern, $extractedBuildNumber } | sc $_.FullName
            (gc $_.FullName) | % { $_ -replace $pattern2, "AssemblyProduct(""$assemblyProduct""" } | sc $_.FullName
            (gc $_.FullName) | % { $_ -replace $pattern3, "AssemblyCopyright(""$Copyright""" } | sc $_.FullName
        }

        Write-Host "Done!"
    }
}
catch {
    Write-Host $_
    exit 1
}