views:

317

answers:

4

Hope someone can assist me with this. Have TeamCity up and running and doing builds on various projects. I'd like to be able to copy/deploy a successful TeamCity ran build to a test server automatically.

I was thinking of using PowerShell to do this but, am open to other ideas. Can some provide me with info on how I can accomplish this.

Thanks.

+3  A: 

I use WGet. Here are the instructions for forming the team city URL. You can do a WGet in powershell, but if you only wanted powershell for this functionality, you can just use a plain wget utility for windows.

EDIT: Here is an example from our QA deployment (names changed to protect the guilty):

"C:\Program Files (x86)\NcFTP\wget.exe" "http://teamcityserver.domain.com:8111/guestAuth/repository/download/bt6/.lastFinished/Artificat.ear"

The location of the wget isn't relevant, that is just where it happens to be. The guestAuth part of the parameter specifies the authentication type (in our case we enabled guest authorization to not have to bother with passwords - it is an internal server only anyway and protected by firewalls). The options are in the documentation I linked to.

The other interesting feature of the parameters is the bt6. That is the unique key of the build, and is different for every project. You can discover what it is by navigating the team city website to the configuration of that build - it will be there. There are also instructions for referencing the configuration by name, but we found that was too verbose to bother with.

Yishai
Can you provide an example... I assume I need to be able to pass credentials for the copy to work as well right..?
Jason
Was wget.exe something you had to actually install/deploy on the server..?
Jason
Yes, it is a small executable. You can get it here: http://pages.interlog.com/~tcharron/wgetwin.html
Yishai
Where does Artifact.ear come into play..? For your project did you configure a artifact dependency..?
Jason
Artifact.ear is just the artifact that team city generates for our project. You will need to change that to match the name of the artifact that comes out of your build process (the file that you deploy). If you have more than one file, you will need more than one line in your cmd file.
Yishai
A: 

I've been implementing this for our applications today. Using msbuild. I have found this very useful as we can add in custom steps such as modifying config files, archiving live builds and notifying people of changes.

Here is a build script you may find useful. It precompiles the application and then copies it into the deploy directory.

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Deploy">

    <PropertyGroup>
        <WebsitePublishDirectory>Artifacts\Website</WebsitePublishDirectory>
        <WebsiteDeployDirectory>\\SERVERNAME\Path\to\web\root</WebsiteDeployDirectory>
        <WebsiteProject>[Project name here]</WebsiteProject>
    </PropertyGroup>

    <Target Name="Deploy">
        <RemoveDir Directories="$(WebsitePublishDirectory)" />

        <AspNetCompiler
            VirtualPath="test"
            PhysicalPath="$(WebsiteProject)"
            TargetPath="$(WebsitePublishDirectory)"
            Force="true"
            Debug="false" />

        <ItemGroup>
            <PublishedFiles Include="$(WebsitePublishDirectory)\**" />
        </ItemGroup>

        <Copy SourceFiles="@(PublishedFiles)" DestinationFolder="$(WebsiteDeployDirectory)\%(RecursiveDir)" />
    </Target>    
</Project>
Jack Ryan
In your "Copy" line is there a way to specify credentials..? Or how did you allow the msbuild script to publish to the other server..?
Jason
I do it by having the TeamCity Build Agents run under an account that have write permissions to the various build directories. You could also use the MSBuild community tasks to upload the files using FTP.
Jack Ryan
JayArr for the "WebsiteProject" I try and specify the .csproj file for our web application but, TeamCity keeps reporting it can't find it...
Jason
A: 

I created a Post Build Script in Visual Studio like this:

c:\TeamCityBuild\pt_build.bat
exit 0

Then on the TC-server I have a .bat that looks like this:

net use r: \192.168.16.85\WebSite password /USER:domain.com\administrator
xcopy "C:\TeamCityBuild\path\WebSite*" "r:\" /R /Y /E
r: \192.168.16.85\WebSite /DELETE
if errorlevel 1 goto buildFAILED
:buildOK
echo Wehej!!!
exit 0
:buildFAILED
echo Oh NOOO!!!
exit 1

'R:' is a mapped drive to the test server.

The error handling is only needed to avoid script errors when someone builds the project on a environment without the correct folder structure.

So far everyting is working great!

Zooking
+1  A: 

You could also install a TeamCity agent on the test server. That's actually how TeamCity was intented to be used.

Philippe Leybaert
In my case that didn't work since my web project had a lot of files that were not included in subversion. When TeamCity builds it erased all of these files and only added the ones from subversion.
Zooking
A TeamCity build task can take all the build artifacts from another build. The files don't have to be in SVN. You simply tell a build task to run on the test server and grab the artifacts of the last successful build.
Philippe Leybaert
Ok, I'll look into how artifacts work and see if I can get it working. Thanks!
Zooking