A quick post, but something that I’ve found useful.
There have been a few times when we’ve needed to trigger a build from our team city server – but I’ve left the office and forgot to trigger it. I could login remotely…. but being a bit of a geek I wanted a quicker/easier solution.
This post describes a simple approach to add a bit of automtion/email support. We are using Outlook and TeamCity at work, however I think this approach could be adapted to a wide range of problems.
The solution requires three pieces to work – the first is a general purpose script that will send each line in the email as a parameter to a powershell script:
Sub RunTeamCityBuild(item As Outlook.MailItem) Dim lines() As String lines = Split(item.Body) Dim script As String script = "X:\Scripts\RunTeamCityBuild.ps1" For Each Line In lines Line = Trim(Replace(Line, vbNewLine, "")) If (Len(Line) > 0 And Len(Line) < 10) Then Call Shell("powershell -noexit -file " + script + " " + Line, vbMaximizedFocus) End If Next End Sub
The second is a simple powershell script that will call off to our teamcity server, first logging in (as me) and then secondly triggering the build (the build parameter is the build id of the target in teamcity):
Param( [string]$buildId ) $buildId = $buildId.ToUpper(); $login = "http://teamcity:81/ntlmLogin.html"; $url = "http://teamcity:81/httpAuth/app/rest/buildQueue"; $body = "<build><buildType id='$buildId'/></build>"; Invoke-WebRequest -Uri $login -sessionVariable session -UseDefaultCredentials Invoke-WebRequest -uri $url -body $body -method "POST" -UseDefaultCredentials -webSession $session -contentType "application/xml"
Finally, I setup a simple rule in outlook that runs the above script task when an email is received that has a specific subject (e.g. ##TEAMCITY), and that is from myself and to myself.
… And that’s it! Of course, we need to be a little careful with this – there is a risk of an email spoof/injection attack. I’ve added some basic precautions in the VBA script to prevent this, and will be tightening my installed version up now that I’ve proved the concept.
Image Credit: SomeDriftwood