Starting a TeamCity build from a PowerShell script with cURL

by Klaus Graefensteiner 1. July 2011 05:28

Introduction

One of the nice TeamCity features is to start builds via http REST API.  The TeamCity REST API uses basic authentication and you can provide the Build Type ID and additional data via parameter name value pairs. The perfect command line tool for sending http requests is cURL.  cURL wrapped in PowerShell make them an unbeatable duo. This blog post demonstrates how.

Step by Step

Step 1 – Find the build type id

Find the build type id of the build configuration that you would like to start form PowerShell. You just need to open the configuration in the TeamCity portal and look for the bt[number] part of the URL in the browser address bar. In my example it is bt2.

BuildArtifact

Figure 1: Trigger a build from a PowerShell script

Step 2 – Download cURL

cURL can be downloaded here: cURL

Step 3 – Run the following PowerShell script

Of course you could just run the PowerShell script from the PowerShell ISE or console directly. But double clicking a batch file would be much more convenient.

The first batch file passes an external parameter and the build type id to the second batch file. The second batch file forwards these parameters to the PowerShell script which then calls cURL to trigger a new build.

rerun.bat

run.bat bt3 7734

run.bat

@echo %1
@echo %2

PowerShell -NoExit -file "C:\TeamCityScripts\StartTeamCityBuildWithCurl.ps1" %1 %2

PAUSE

StartTeamCityBuildFromCurl.ps1

Param([string] $BuildTypeId="bt3", [string] $BuildNumber="50")

Set-StrictMode -Version "Latest"
$DebugPreference = "Continue"


$URL = "`"http://localhost:8887/httpAuth/action.html?add2Queue=$BuildTypeId&env.name=ExternalBuildID&env.value=$BuildNumber`""
Write-Debug $URL
Write-Debug "Starting teamcity via cURL and teamcity REST API"
$result = start-process -filePath "C:\TeamCityScripts\curl" `
                        -ArgumentList "--user", "autoadmin:LuckyYou", $URL `
                        -NoNewWindow `
                        -RedirectStandardError "error.txt" `
                        -RedirectStandardOutput "output.txt" `
                        -wait `
                        -passthru
$result.HasExited
$result.ExitCode

Download

The example script can be downloaded here: StartTeamCityBuildFromPowerShell.zip

Ausblick

Automating builds with TeamCity and PowerShell is fun square.

Tags: , , , ,

Continuous Integration | PowerShell

About Klaus Graefensteiner

I like the programming of machines.

Add to Google Reader or Homepage

LinkedIn FacebookTwitter View Klaus Graefensteiner's profile on Technorati
Klaus Graefensteiner

Klaus Graefensteiner
works as Developer In Test and is founder of the PowerShell Unit Testing Framework PSUnit. More...

Open Source Projects

PSUnit is a Unit Testing framwork for PowerShell. It is designed for simplicity and hosted by Codeplex.
BlogShell is The tool for lazy developers who like to automate the composition of blog content during the writing of a blog post. It is hosted by CodePlex.

Administration

About

Powered by:
BlogEngine.Net
Version: 1.6.1.0

License:
Creative Commons License

Copyright:
© Copyright 2013, Klaus Graefensteiner.

Disclaimer:
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Theme design:
This blog theme was designed and is copyrighted 2013 by Klaus Graefensteiner

Rendertime:
Page rendered at 5/20/2013 12:01:13 PM (PST Pacific Standard Time UTC DST -7)