Automatically tag the build currently in progress

So, I want to tag a build if it gets to a certain point in the msbuild file. The tag is important for other build configurations to find certain artifacts of those builds so tagged.
I thought that this would be easy via the rest API, so I added a target to my build file to invoke it, eg:

  <Target Name="TeamCityTag"
          Condition="$(TEAMCITY_VERSION) != ''">
    <HttpWebRequest TaskAction="Post"
      <Output TaskParameter="Status"
              PropertyName="PostStatus" />
      <Output TaskParameter="Response"
              PropertyName="PostResponse" />
    <Message Text="Tagged: $(PostStatus) $(PostResponse)" />

Sure enough, when I test this target to tag build numbers of builds that have already finished, it works perfectly. However, when I invoke this target as the last target in an actual build run by TeamCity (in order to effectively have the build "tag itself"), I get eg:

[22:20:00][HttpWebRequest] Executing Post HttpRequest against: https://teamcity.domain.local/httpAuth/app/rest/builds/id:943/tags/
[22:20:00][HttpWebRequest] C:\BuildAgent\work\dc752afaa7933393\TC.targets(548, 5): The remote server returned an error: (404) Not Found.. Status: ProtocolError
[22:20:00][HttpWebRequest] C:\BuildAgent\work\dc752afaa7933393\TC.targets(548, 5): Error has occurred during request processing (Not Found). Error: No build can be found by id '943'. Could not find the entity requested. Check the reference is correct and the user has permissions to access the entity.

I can only assume that since the build with the id $(BUILD_NUMBER)  doesn't actually exist (in a finished state) yet, the rest API can't see  it yet. I guess I could add another configuration that's triggered on completion of the first, that just runs the new target by itself, but I don't necessarily want to always create the tag - only when the build completes (successfully or unsuccessfully) under certain conditions tracked in the original build targets, and that information would be hard to determine from a completely separate build configuration.

Does this make sense? Is there a simple way to achieve the effect of having a build tag itself when certain conditions are met?

1 comment
Comment actions Permalink

Never mind - I realized I need to pass down %teamcity.buildid% instead of using BUILD_NUMBER


Please sign in to leave a comment.