Get the source control git commit ID corresponding to a build ID
Before anyone answer too quickly the Teamcity build change ID is not always the git commit ID. For example if an admin made a configuration change then triggered a build the last change id wont correspond to any git commit. Still in that case I would like to get the git commit ID associated with the build
The GUI can get it there under "build.vcs.number", even if the change was a Teamcity project setting :

b454d000976416331327ed79a2a2071521f1c64c is the last git commit
In the rest api, I found only the lastChanges.change[0].version which sometimes has the git commit id.
fetch({...httpOptions, path:'/app/rest/builds'}).then((response) => {
const json = response.body;
for (const build of json.build) {
fetch({...httpOptions, path:'/app/rest/builds/id:'+build.id}).then((response) => {
console.log('last change', response.body.lastChanges.change[0], options.gitCommit);
if (response.body.lastChanges.change[0].version === options.gitCommit) {
console.log('git commit found', response.body.lastChanges.change[0].version);
console.log('from build id', build.id);
download({...httpOptions, path:'/app/rest/builds/id:'+build.id+'/artifacts/archived'}, "./artifact."+options.gitCommit+".zip");
}
(...)
For that build matching the gui screenshot, the lastChange.change[0].version value is deb426c28d7d8c55c8e7a706c5c07231de8e3e55 which is not the git commit id.
So where do I get the consistent git commit ID associated with a build ID in the Teamcity REST API ?
Thanks!
Please sign in to leave a comment.
To answer my own question, it can be found in
Then extract "build.vcs.number" from the results
Also to be clear, keep in mind that a build ID does not map 1:1 to commit IDs, since a single build can have multiple VCS Roots that have different commit histories, so if you have multiple of them, build.vcs.number will take one of them, but the multiple .1, .2 etc will have different values, same with the revisions element.