2015-09-21 102 views
-1

我们正在使用TFS Build 2013,并且正在配置我们的构建策略。我们希望尽可能将其保留为默认值,所以我们希望使用默认的构建过程模板,而无需任何自定义活动。相反,我们有一些Powershell脚本,它们连接到默认模板。其中一个脚本从要构建的项目中提取版本号。如何将参数从powershell脚本传递到TFS 2013版本?

问题:我想在版本号中解析这个版本号,以便我们的版本有一个很好的和有意义的名称(例如Project - 2.1.2.46),我们不必在每个版本中更改构建定义。

我基本上想要实现的是,我的构建脚本(下面)的结果被用作内部编号(格式)。

# Enable -Verbose option 
[CmdletBinding()] 
# Disable parameter 
# Convenience option so you can debug this script or disable it in 
# your build definition without having to remove it from 
# the 'Post-build script path' build process parameter. 
param([switch]$Disable) 
if ($PSBoundParameters.ContainsKey('Disable')) 
{ 
    Write-Verbose "Script disabled; no actions will be taken on the files." 
} 

# Regular expression pattern to find the version in the assemblies 
# and then apply it to the build number 
$AssemblyFileVersionRegex = 'AssemblyFileVersion\("\d+\.\d+\.\d+\.\d+"' 

# Make sure path to source code directory is available 
if (-not $Env:TF_BUILD_SOURCESDIRECTORY) 
{ 
    Write-Error ("TF_BUILD_SOURCESDIRECTORY environment variable is missing.") 
    exit 1 
} 
elseif (-not (Test-Path $Env:TF_BUILD_SOURCESDIRECTORY)) 
{ 
    Write-Error "TF_BUILD_SOURCESDIRECTORY does not exist: $Env:TF_BUILD_SOURCESDIRECTORY" 
    exit 1 
} 
Write-Verbose "TF_BUILD_SOURCESDIRECTORY: $Env:TF_BUILD_SOURCESDIRECTORY" 

# Make sure build definition name is available 
if (-not $Env:TF_BUILD_BUILDDEFINITIONNAME) 
{ 
    Write-Error ("TF_BUILD_BUILDDEFINITIONNAME environment variable is missing.") 
    exit 1 
} 

# Apply the version to the assembly property files 
$files = gci $Env:TF_BUILD_SOURCESDIRECTORY -recurse -include "*Properties*", "Solution Items" | 
    ?{ $_.PSIsContainer } | 
    foreach { gci -Path $_.FullName -Recurse -include CommonAssemblyInfo.*, AssemblyInfo.* } 
if($files) 
{ 
    foreach ($file in $files) { 

     if(-not $Disable) 
     { 
      $fileContent = Get-Content $file 
      $found = $fileContent -match $AssemblyFileVersionRegex 

      if ($found) 
      { 
       Write-Verbose "Found in files: $found" 

       $VersionData = $found -replace [Regex]::Escape('[assembly: AssemblyFileVersion("'), '' 
       $VersionData = $VersionData -replace [Regex]::Escape('")]'), '' 
       Write-Verbose "Will apply $VersionData to build number" 

       $Env:TF_BUILD_BUILDNUMBER = $Env:TF_BUILD_BUILDDEFINITIONNAME + ' - ' + $VersionData 
       Write-Verbose "TF_BUILD_BUILDNUMBER: $Env:TF_BUILD_BUILDNUMBER" 

       exit 0 
      } 
      else 
      { 
       Write-Warning "No version number found in files. Build number will not be changed." 
      }   
     } 
    } 
} 
else 
{ 
    Write-Warning "Found no files." 
} 

有了详细的日志功能,我可以看到我的PowerShell脚本解析在TF_BUILD_BUILDNUMBER变量正确的版本号。所以,这表明我的脚本正在工作。但是,构建完成后,内部版本号将更改回其构建定义中提供的默认值。

任何帮助将不胜感激!

回答

0

我自己找到了答案:无法更新环境变量,例如TF_BUILD_BUILDNUMBER,使用PowerShell并再次使用构建中的更新值。它将始终回退到默认值,这是在构建触发时定义的。

0

您可以修改您的PowerShell脚本并检查最新的文件夹(构建文件夹)名称。假设如果姓氏是1.2.45,那么在您的PowerShell脚本中,您可以将其增加一个次版本号。

+0

我从AssemblyInfo文件中获得版本号。这不是问题。不过,我想使用这个数字作为drop/build文件夹的名称。但是,我不知道如何从Powershell脚本更改此放置位置。 – Sjoerdson

+0

你能分享你的PowerShell脚本吗? –

+0

当然,我更新了我的问题。这是我为了我的需要而改变的标准脚本。我想这不是最好的PowerShell代码,但它确实有魔力;它从项目中获取版本号并将其放入$ Env:TF_BUILD_BUILDNUMBER中。 – Sjoerdson

相关问题