TFS Build 提供预构建自动递增程序集信息 1 和 post 发布压缩文件

TFS Build providing prebuild auto increment assembly information by 1 and post publish zip the files

我正在尝试使用 TFS(版本 12.0.31101.0)实现构建自动化 这是我正在使用的设置,它正确构建并发布到提到的放置位置:

对于 PreBuild,我正在尝试使用以下批处理脚本并且这不是递增的:

$path = "E:\Dev\ABC\My Project\AssemblyInfo.vb"
$pattern =  '\<Assembly: AssemblyVersion\(.*\)\>'
(Get-Content $path) | ForEach-Object{
    if($_ -match $pattern){
        # We have found the matching line
        # Edit the version number and put back.
        $fileVersion = [version]$matches[1]
        Write-Output "Major is $Matches[0] Minor is $Matches[1] Build is $Matches[2] Revision is [version]$matches[3]"
        $newVersion = "{0}.{1}.{2}.{3}" -f $fileVersion.Major, $fileVersion.Minor, $fileVersion.Build, ($fileVersion.Revision + 1)
        '<Assembly: AssemblyVersion("{0}")>' -f $newVersion
    } else {
        # Output line as is
        $_
    }
} | Set-Content $path

对于 'post build script path' 我想压缩内容并将其放入另一个文件夹,为此我使用以下脚本。

powershell.exe -nologo -noprofile -command "& { Add-Type -A 'System.IO.Compression.FileSystem'; [IO.Compression.ZipFile]::CreateFromDirectory('$(TF_BUILD_DROPLOCATION)\MySolution\_PublishedWebsites\ABC', 'ABC_Deploy.zip'); }"

执行时会抛出以下错误:

使用“2”个参数调用 "CreateFromDirectory" 异常:“找不到 路径的一部分 'C:\$TF_BUILD_DROPLOCATION\MySolution\_PublishedWebsites\ABC 在 line:1 char:53 + & { 添加类型 -A 'System.IO.Compression.FileSystem'; [IO.Compression.ZipFile]::Cr ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DirectoryNotFoundException

我应该对预构建脚本和 post 构建脚本进行哪些更改才能使其正常工作?

对于 'Post Build Script' 如果构建放置位置无法通过您正在使用的变量访问;尝试使用 COPY 命令并将放置的文件夹复制到 TFS 构建服务器上的已知位置,然后在 ZIP Powershell 命令中提供该路径。 (虽然它现在是解决方法。:))

对于'Pre build Script',我会查看并回复您。

对于'post build script path',根据异常消息它无法识别变量$TF_BUILD_DROPLOCATION

因为它是环境变量,所以,请尝试使用 $env: 访问变量。下降位置将是 $env:TF_BUILD_DROPLOCATION 例如。

对于 PreBuild,如果要对程序集进行版本控制,可以尝试将所有自定义版本控制工作放入自定义 Version.proj MsBuild 脚本中,并在 .sln 之前的 TFS 构建定义中调用它。该脚本将 Version 注入源代码(SharedAssemblyInfo.cs、Wix 代码、readme.txt),然后解决方案构建构建该源代码。请参考此线程:Versioning .NET builds

您也可以参考这篇文章: https://www.stevefenton.co.uk/2012/11/automatically-updating-your-assemblyinfo-with-a-batch-file/

对于预构建: 创建一个更新程序集信息的 C# 项目(我使用控制台项目)..

  1. 在 MSBuild 之前将 RunScript 添加到 TFS 构建模板。
  2. 从您的批处理文件中调用从c# 项目生成的exe,并将参数作为您的源代码位置传递。 例如:(START "C:\UpdateAssemblyTest.exe" "\TFS-Server\TFSMapPath\ABC\")

PostBuild:我找到了获取 TF_BUILD_DROPLOCATION 的解决方法。我修改了 TFS 构建模板并添加了一个 'RunScript' 工具并从那里调用我的批处理文件并将放置位置作为参数传递。