ZaChickster
ZaChickster

Reputation: 429

Start TeamCity Build via REST Call

A little background.

In my environment we have a large number small .NET solutions each in their own Subversion repositories (500+). We not a TFS shop and are currently evaluating moving our home grown CI process to TeamCity.

Instead of having these 500+ repos polling our Subversion server every 5-10 minutes or so I'd like to kick off a Project build via a post-commit-hook REST http call (as our current solution does). I would then want TeamCity to update from SVN and commence the build process. Is this possible?

I see TeamCity has a REST API, just that the documentation is sparse. I'm not sure how this example ties to anything I've got configured. What is bt7? How does it tie to the projects I've configured?

http://buildserver:8111/httpAuth/action.html?add2Queue=bt7 

Upvotes: 13

Views: 12446

Answers (3)

Winter Soldier
Winter Soldier

Reputation: 2685

If you are in the same boat as mine, this might help you with writing REST calls to Team-City- especially around triggering a build and monitoring it.

  • Here is a sample JS client that I've used in the past to trigger builds. You'll need a Bearer Token from your TeamCity account.
  • That is a pretty standard token, you could generate it like you'd for Git.
  • This doc should help you with different post body options (convert xml to json though)

const TC_CLIENT = {
  async postBuild() {
    const request = {
      buildType: {
        id: "YOUR_BUILD_TYPE_ID"
      },
      properties: {
        property: [{
            name: "balh",
            value: "blah blah"
          },
          {
            name: "balh",
            value: "blah blah"
          }
        ]
      }
    };

    const response = await fetch("http://yourtcserver/app/rest/buildQueue", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        Accept: "application/json",
        Authorization: "Bearer YOUR_TC_TOKEN"
      },
      body: JSON.stringify(request)
    });
    return await response.json();
  },

  async getBuildStatus(buildId) {
    const response = await fetch(
      "http://yourtcserver/app/rest/buildQueue/" + buildId, {
        method: "GET",
        headers: {
          "Content-Type": "application/json",
          Accept: "application/json",
          Authorization: "Bearer YOUR_TC_TOKEN"
        }
      }
    );
    return await response.json();
  }
};

let sampleBuild = TC_CLIENT.postBuild();
console.log("Status", TC_CLIENT.getBuildStatus(sampleBuild.id))

Upvotes: 1

Jesse Webb
Jesse Webb

Reputation: 45243

bt7 is your buildTypeId which is a unique ID for each build configuration (job). Each project can have many build configurations.

If you click into a project and then click into a specific build configuration, your URL will be something like...

http://teacmtiyserver/viewType.html?buildTypeId=bt208

If you want to queue up a build configuration to run, just find out its buildTypeId by looking at the URL and then you can probably try running it by hitting the URL you used.

Upvotes: 2

Mike Two
Mike Two

Reputation: 46173

bt7 is a build type identifier. Each build configuration has one. You can get the full list using the rest api as follows

http://buildserver:8111/httpAuth/app/rest/buildTypes

You can also see the build type in the url if you click any of the build configurations on your team city page. You will see a url parameter such as buildTypeId=bt7

Upvotes: 5

Related Questions