DEV Community

Sean Killeen
Sean Killeen

Posted on • Originally published at seankilleen.com on

3

How to: Call a TeamCity build from OctopusDeploy

We have an OctopusDeploy deployment that deploys our code to an automated acceptance test environment. And we have a TeamCity build that executes our automated acceptance tests.

The challenge is: the TeamCity build kicked off after our code was merged, not after the deployment was finished. We wanted the tests to kick off only after the environment was ready.

Solution

I created an OctopusDeploy step template to kick off a TeamCity build, and then utilized that step template in the Octopus deployment.

It works via crafting an HTTP POST request against the TeamCity API.

Step Template Contents

I created a PowerShell script that utilizes some parameters

$parsedUrlString = "$TeamCityServerURL" + "/app/rest/buildQueue" $parsedXmlBody = '<build><buildType id="' + $TeamCityBuildConfigID + '"/></build>'

$HeaderFields = @{} $HeaderFields.add("Authorization", "Bearer $TeamCityAccessToken")
$HeaderFields.add("Origin", "$TeamCityServerURL")

Write-Host "Attempting to run the $TeamCityBuildConfigID build" Write-Host "Connecting to $parsedUrlString"

Invoke-WebRequest -UseBasicParsing $parsedUrlString -ContentType "application/xml" -Method POST -Body $parsedXmlBody -Headers $HeaderFields

Enter fullscreen mode Exit fullscreen mode

The Parameters

From the variables in the script, Octopus will generate step templates:

Script parameters shown visually

Generating the TeamCity Access Token

  • Open TeamCity and head to the settings menu:

The settings menu

  • Click to create the access token:

the menu for creating an ccess token

  • Give the access token a name:

Name entry for access token

  • Copy the access token

Access token text for copying

Adding the build step

  • In OctopusDeploy, open the deployment in question
  • Add a deployment step, and select the step template
  • Add the applicable parameters

The Result

Now, when we commit code:

  • The build finishes
  • The Octopus deployment kicks off
  • After the deployment, the last step calls the other TeamCity build
  • The TeamCity build kicks off to run our automated acceptance tests

Happy deploying!

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay