TFS - Run Powershell script against package before Deployment - powershell

I currently have a CI Setup in TFS 2013 which does the following
Pulls code down from Git on every commit to a branch
Builds the Solution
Runs N-Unit Tests Against the solution
Runs Jasmine Front-end Tests against the javascript
Deploys on success via WebDeploy to chosen server.
I have now managed to install Grunt and NodeJS on the server to do some manipulation of the Javascript between steps 5-6. Does anyone have any advice on how this might be done?
I've tried post-tests scripts to minify the javascript successfully on both the src and bin/_PublishedWebsites directory but this does not seem to persist over to the deployment server. And infact, the _PublishedWebsites route puts the build folder in an undeletable state due to maxmimum character limits on Windows files (argh).

You should switch over to using Release Management for Visual Studio 2013 (works with 2012 as well). This allows you to parameterize your release and push the same output through multiple environments. Very configurable and even makes sure that the tools you need end up on the server that you are deploying to. Supports Puppet, Chef, DSC, and create your own.
http://nakedalm.com/installing-release-management-server-tfs-2013/
And for an overview: http://nakedalm.com/building-release-pipeline-release-management-visual-studio-2013/

I managed to get this working with the addition of two extra steps to the pubxml file used for the deployment.
First, i added a dependency powershell script which ran NPM install and grunt tasks.
<PipelineDependsOn>
CustomBeforePublish;
$(PipelineDependsOn);
</PipelineDependsOn>
<Target Name="CustomBeforePublish">
<Exec Command="powershell.exe -ExecutionPolicy Unrestricted -file Pre_Deploy_Javascript_Build.ps1 $(ProjectDir)"/>
</Target>
Following this. I had now created additional files which did not exist in the project. I had to now ensure that these were published. To do this, i added another step.
<CopyAllFilesToSingleFolderForMsdeployDependsOn>
CopyMinJSFiles;
$(CopyAllFilesToSingleFolderForMsdeployDependsOn);
</CopyAllFilesToSingleFolderForMsdeployDependsOn >
<Target Name="CopyMinJSFiles">
<ItemGroup>
<_MinJSFiles Include="$(ProjectDir)\App\*.js" />
<FilesForPackagingFromProject Include="%(_MinJSFiles.Identity)">
<DestinationRelativePath>App\%(Filename)%(Extension)</DestinationRelativePath>
</FilesForPackagingFromProject>
</ItemGroup>

Related

Deploy website to drop folder on TFS build server

I am using TFS 2012 on a build server to do continuous integration, and also builds for other environments. I am deploying a .net 4.0 webforms solution containing two websites and a console app to a build server and in the drop folder I get a _PublishedWebsites folder containing the deployment package for the custom configuration I have specified (ie not Debug or Release). I get the correct .Web_Package for each website.
I am using the MSBuildArgumments setting in the build process as follows:
/p:DeployOnBuild=true /p:PublishProfile=Development/p:VisualStudioVersion=10.0
Everything works well, but the team lead wants the actual deployment in the drop-folder and not the deployment package.
I have looked at the MS Documentation but it does not appear to help in my case.
I have tried creating a script, but I dont want to go into a huge series of powershell scripts, and I struggle with powershell anyway. I just cant get it to do what I want.
My question is: can this be done, and what is the best way of doing it? If it does mean creating a script then so be it ( perhaps one line for each object) - I am really having a hard time working out which direction I should be going in.
I thought something a script containing one line for each deployment -3 lines like this:
_PublishedWebsites/<project name>.Web_Package/<project name>.deploy.cmd /T: /M:<site> /U:<user> /P:<password>
But where to put the script and how to call?
I ended up using the solution as described in this blog post:
http://blog.degree.no/2012/03/automatic-config-transformations/
To get what I needed, I needed to change the project files of the projects to be deployed - adding this extra node at the top of each.csproj file
<Target Name="TransformConfigFiles" AfterTargets="AfterBuild" Condition="'$(TransformConfigFiles)'=='true'">
<ItemGroup>
<DeleteAfterBuild Include="$(WebProjectOutputDir)\Web.*.config" />
</ItemGroup>
<TransformXml Source="Web.config" Transform="$(ProjectConfigTransformFileName)" Destination="$(WebProjectOutputDir)\Web.config" />
<Delete Files="#(DeleteAfterBuild)" />
</Target>
and adding this switch to the MSBuildArguments in the build definition:
/p:TransformConfigFiles=true
When I run my builds using the DeployOnBuild switch, my builds output two folders, one with the Package, and one with just all the website files (both under PublishedWebsites). Is that not what you want?
I have:
BuildDrop_PublishedWebsites\Website
BuildDrop_PublishedWebsites\Website_Package

Invalid file names when trying to deploy SSDT project with TeamCity 8

I am trying to deploy Visual Studio 2012 SSDT project to Sql Server using TeamCity 8 and MSBuild Publish task but the deployment fails.
When I look at TeamCity logs and use /v:diag switch in my build configuration I see that for unknown reason MSBuild searches for MyProject.sqlproj.publish.sql and for MyProject.sqlproj.dacpac files.
The exact error:
[SqlPublishTask] C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\SSDT\Microsoft.Data.Tools.Schema.SqlTasks.targets(1233, 5): File "C:\Program Files\TeamCity\buildAgent\work\abf8bc05a2cfe7f\*MyProject*\bin\Debug\*MyProject*.sqlproj.dacpac" does not exist.
The correct .sql and .dacpac files get generated (without the .sqlproj in the middel) in buildAgent/work/identificator/*MySolution*/MyProject/bin/Debug folder.
My TeamCity build step is configured as follows:
Runner type: MSbuild
Build file path: MyProject/*MyProject*.sqlproj
MSBuild version: 4.5
MsBuild ToolsVersion: 4.0
Run platform: 4.0
Targets: Publish
Command line parameters: /p:SqlPublishProfilePath="Debug.publish.xml" /p:Configuration=Debug
If I execute this from commandline I get no errors.
Any ideas on how can I configure TeamCity to search for correct files or configure my project to generate the files that TeamCity is searching for.
Or is my plan to use MSBuild's Publish task futile and I should utilise sqlpackage.exe instead?
UPDATE
After spending almost three days trying to figure this out I gave up and used sqlpackage.exe which works like a charm.
But I would still be interested in an answer though, passing paths to executables in build servers seems a bit crude way to accomplish things.
I had a similar issue and came to the conclusion that the way TeamCity produces "pseudo-project" files with *.teamcity suffixes is confusing something in the MSBuild/SSDT target chain.
I simply replaced the MSBuild runner build step with a pure Command Line step and the problem went away.
We lose the user friendliness of the TeamCity MSBuild runner configuration, but if it works, it's a compromise I'm willing to make.
Note - we are running TeamCity 7 - I am not sure if this has been addressed in later versions.
I found out you can set a System Property named "system.SqlTargetName" on the build configuration to override the default value.
Setting this to your project name without the ".sqlproj" makes the error go away.

Automatic installer deploy to remote server using TeamCity and MSBuild

I'm having a .net WPF project that compiles to a bunch of dlls. I also have another project that compiles all the dlls and creates an installator exe.
I am also using TeamCity to automatically do those tasks for me with a press of a button.
The problem is that I want to have a separate TeamCity build configuration intended to automatically copy the result installator exe to a number of remote machines, each having specific credentials. But, unfortunately, I don't get how do I do this.
I have found some articles on automatic deploying (like this http://www.troyhunt.com/2010/11/you-deploying-it-wrong-teamcity.html), but, obviously, they are very specific for web projects.
So, how should I correctly deploy my installator on build?
Your best bet is to research NAnt and make a simple task that will copy the TeamCity artifact (installer) out to the specified location.
All you would need to do is have TeamCity execute the specified NAnt task after the installer has been built.
http://nant.sourceforge.net/release/0.85/help/tasks/copy.html
Update
Also, check out this question for solutions on specifying the credentials for the copy task.
OK, I've found out that it's pretty easy to do this using the FtpUpload MSBuild community task. There, one can set up the credentials and all other stuff needed for uploading a file (or a set of files) via ftp.

Getting TeamCity to run NUnit tests, with both NCover and TypeMock integrated?

Basically I'd like to use the NUnit plugin for TeamCity (the program, not necessarily the specific build step using it) to run my unit tests, with NCover for code coverage, and since my unit tests uses TypeMock 6, I need that too working.
So far I've tried:
Just basically pointing the TeamCity NUnit build-step to my dll's, but that fails with the following error message:
Typemock Isolator needs to be linked with Coverage Tool to run, to enable do one of the following:
link the Coverage tool through the Typemock Isolator Configuration
run tests via TMockRunner.exe -link
use TypeMockStart tasks for MSBuild or NAnt with Link
Trying to figure out the right command line, I tried this:
C:...\Isolator\6.0\TMockRunner.exe "C:\TeamCity...\JetBrains.BuildServer.NUnitLauncher.exe" v4.0 MSIL NUnit-2.5.9 MyAssembly.dll
This fails with the exact same error.
Setting the environment variables found in the mocking_on.bat file part of TypeMock, this doesn't change the outcome.
Note that the above examples doesn't contain any reference to NCover (yet), that's because I've been hacking around on the command line for a couple of hours with the above examples and still haven't gotten basic unit-tests running. NCover is extra options to the nunit-launcher of TeamCity so I hope this is as simple as just enabling that when I get that far.
Since TypeMock requires you to use their own runner program, TMockRunner, there's no way to just use the GUI options in TeamCity to get everything set up.
Instead, what I ended up doing was to first build a custom msbuild file with this contents:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<TypeMockLocation>C:\Program Files (x86)\TypeMock\Isolator\6.0</TypeMockLocation>
<NUnit>"C:\TeamCity\buildAgent\plugins\dotnetPlugin\bin\JetBrains.BuildServer.NUnitLauncher.exe"</NUnit>
<NCover>C:\Program Files (x86)\NCover\NCover.Console.exe</NCover>
</PropertyGroup>
<Import Project="$(TypeMockLocation)\TypeMock.MSBuild.Tasks"/>
<Target Name="TestWithTypeMock">
<TypeMockStart Link="NCover3.0" ProfilerLaunchedFirst="true" Target="2.0"/>
<Exec ContinueOnError="true" Command="$(NUnit) v2.0 x86 NUnit-2.5.9 SqlDatabases.Core.Tests\bin\Debug\SqlDatabases.Core.Tests.dll SqlDatabases.SqlServer.Tests\bin\Debug\SqlDatabases.SqlServer.Tests.dll /ncover:%22$(NCover)%22 /ncover-arg://ias /ncover-arg:SqlDatabases.Core /ncover-arg://ias /ncover-arg:SqlDatabases.SqlServer /ncover-arg://et /ncover-arg:.*Exception /ncover-arg://x /ncover-arg:c:\temp\coverage.xml"/>
<TypeMockStop/>
</Target>
</Project>
This file I save to a directory on my TeamCity server. Since I didn't want the test script to be part of my repository, I didn't add it to source control (I can build and right-click and run tests from within Visual Studio, if I get something not so tied to my build server I might change that decision later). Also, I only have 1 build-agent for my TeamCity server so this works for me for the time being.
In addition to the above file, I added the following batch-file:
#echo off
setlocal
set CURDIR=%CD%
copy c:\dev\sqldatabases\tests.msbuild .\
msbuild tests.msbuild /target:TestWithTypeMock
rd /s /q c:\dev\sqldatabases\codecoverage
md c:\dev\sqldatabases\codecoverage
"c:\program files\ncover\ncover.reporting.exe" c:\temp\coverage.xml //or FullCoverageReport:Html:c:\dev\sqldatabases\codecoverage
cd \dev\sqldatabases\codecoverage
del %CURDIR%\coverage.zip
7z a -r %CURDIR%\coverage.zip
And then I added the following two build-steps to my TeamCity build configuration:
Visual Studio Solution: Build the debug configuration.
Execute C:\Dev\SqlDatabases\Tests.bat (the above batch file)
I made sure that coverage.zip was listed under artifacts in my build configuration, and now I got code coverage, typemock, and unit tests.
Only thing I haven't figured out how to get so far is that if I click on the build result of a build configuration that uses the normal TeamCity GUI for setting up everything (minus TypeMock), I get a code coverage short summary listed, this is not present in the above setup, but the full report is, and the tab in the TeamCity build results.
Hopefully this can save someone else some trouble.
Lasse,
So long as you're using the TeamCity GUI, running with NCover coverage should just be a simple matter of selecting to run with it.I've never tried throwing Typemock into the mix, so I look forward to hearing how that goes.

Is there a system that I can use to update my projects on my VPS?

Hey guys, So I recently got a VPS, just so I can start gaining experience. But I'm looking for a service/program where I can code on my PC, then when I'm done, I run a script or do a command or something to have it updated to my VPS.
I thought I was looking for Git, but apparently git does not do what I'm looking for.
Any suggestions?
Windows or Linux?
On Windows, there's a host of tools.
First of all you code. Visual Studio is the most common. You get a sln-file and a batch of *.*proj-files.
When talking about deploying to remote servers, often a continuous integration server is used. We are using TeamCity (http://www.jetbrains.com/teamcity/). Download it locally, install and create a new project, selecting the "SLN-runner". Point it to the sln file of yours.
When you want the deployment part working, create a small build file such as "MyProj.build", that contains something along the lines of
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="BuildProject"
InitialTargets="CheckRequiredProperties"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
ToolsVersion="4.0">
<Target Name="BuildProject">
<Message Text="Starting $(Configuration) build. Web site publish location $(OutputWebSite)" />
<MSBuild Projects="$(SolutionPath)"
Targets="Build"
Properties="BuildOutputPath=$(BuildOutputPath);
BuildOutputPathBin=$(BuildOutputPathBin);
Configuration=$(Configuration);
BuildConstants=$(BuildConstants);
MSBuildTargets=$(MSBuildTargets);
TargetFrameworkVersion=$(TargetFrameworkVersion);
TargetFrameworkProfile=$(TargetFrameworkProfile)">
...
Where SolutionPath points to your sln-file.
You will then update the TeamCity config to point to MyProj.build instead, using the MsBuild runner.
Then you need a way of having TeamCity upload everything to your server. Powershell is a nice scripting environment that can run .Net code, but you'd be invoking it through MsBuild...
Something like this
http://community.bartdesmet.net/blogs/bart/archive/2008/02/16/invoking-powershell-scripts-from-msbuild.aspx
And then you can script with MsDeploy accross to your server:
http://blogs.iis.net/jamescoo/archive/2008/08/21/using-msdeploy-in-powershell.aspx
"rsync" or "scp" tools may be useful