Search code examples
batch-filevisual-studio-2013tfstfsbuild

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


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?


Solution

  • 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