1

I am trying to implement Build automation using TFS (Version 12.0.31101.0) This is the settings I am using, It build properly and publishes to the mentioned drop location:

enter image description here

For PreBuild I am trying to use the following batch script and this is not incrementing:

$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

For 'post build script path' i want to zip the contents and put it into another folder, I am using the following script for this.

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'); }"

On Executon it throws the following error:

Exception calling "CreateFromDirectory" with "2" argument(s): "Could not find a part of the path 'C:\$TF_BUILD_DROPLOCATION\MySolution\_PublishedWebsites\ABC At line:1 char:53 + & { Add-Type -A 'System.IO.Compression.FileSystem'; [IO.Compression.ZipFile]::Cr ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DirectoryNotFoundException

What changes should i make for both prebuild script and post build script to get this working?

Akshay
  • 407
  • 1
  • 4
  • 14

3 Answers3

2

For Prebuild: Create a C# project(i used console project) which updates the assembly information..

  1. Add RunScript to TFS build template before MSBuild.
  2. Call the exe which is generated from c# project from your batch file, and pass the parameter as where your source code location. Eg: (START "C:\UpdateAssemblyTest.exe" "\TFS-Server\TFSMapPath\ABC\")

PostBuild: I found a workaround for getting the TF_BUILD_DROPLOCATION. I modified the TFS Build Template and added a 'RunScript' tool and called my batch file from there and passed the drop location as an argument. enter image description here

enter image description here

Akshay
  • 407
  • 1
  • 4
  • 14
0

For 'Post Build Script' If build drop location is not accessible via the variable you are using; try using COPY command and copy the dropped folders to a known location on TFS Build server and then give that path in the ZIP Powershell command. (Though it is workaround for now. :) )

For 'Pre build Script', I will check and come back to you.

Abhinaw Kaushik
  • 607
  • 6
  • 18
  • As the build deploy folder is auto created after the build is published and as revision number is part of the folder name(eg: TestAkshay_20170613.27) i was not able to get the complete deployed path dynamically. Do we have an alternative workaround for getting the deployed path. – Akshay Jun 14 '17 at 07:38
0

For 'post build script path', according to the Exception message it cannot identify the variable $TF_BUILD_DROPLOCATION.

As it's environment variable, So,please try to access the variables using $env:. The drop location would be $env:TF_BUILD_DROPLOCATION for example.

For PreBuild, if you want to version the assemblies, you can try to put all custom Versioning work into a custom Version.proj MsBuild script and call it in TFS build definition before the .sln. The script injects Version into source code (SharedAssemblyInfo.cs, Wix code, readme.txt), and then solution build builds that source code. Please reference this thread :Versioning .NET builds

You can also reference this article : https://www.stevefenton.co.uk/2012/11/automatically-updating-your-assemblyinfo-with-a-batch-file/

Andy Li-MSFT
  • 28,712
  • 2
  • 33
  • 55
  • Tried with $env:TF_BUILD_DROPLOCATION also, but no luck. Throws the same error. – Akshay Jun 21 '17 at 04:45
  • @Akshay Have you removed the brackets () in \$(TF_BUILD_DROPLOCATION), just try to change it to \$env:TF_BUILD_DROPLOCATION in the script. – Andy Li-MSFT Jun 21 '17 at 13:38
  • I did try possible trial and error options by removing brackets() adding extra brackets, but no luck. So I am passing it as an argument from the TFS build template to runscript control. That worked for me :) – Akshay Jun 27 '17 at 12:54