Get Build status by Commit SHA1

江枫思渺然 提交于 2019-12-10 11:40:55

问题


Our team is using TeamCity for continuous integration and Git-TFS (TFS2015 RC) for versioning.

I want to check if a specific Commit has been built (successfully) by TeamCity and thus can be reintegrated.

It is possible to query TeamCity builds by using their REST API, e.g.:

GET http://teamcity:8000/guestAuth/app/rest/builds/18

with the result containing some xml that shows the git commit SHA:

<revisions count="1">
    <revision version="96b1db05b64ecc895da070137e93cde3d2cadfa1">
        <vcs-root-instance [...]/>
    </revision>
</revisions>

The fact, that this information is in theory available, makes me hope that it is possible to query TeamCity builds by this particular information, like:

GET http://teamcity:8000/guestAuth/app/rest/builds/revision:96b1db05b64ecc895da[...]

but this yields a #400 BAD REQUEST response. I was not able to find out in the TeamCity 9 Documentation, whether this is possible or not. I would rather not iterate all builds to check if they contain this specific commit.

NOTE: This feature has now been implemented by JetBrains and is available in TeamCity 9.1 EAP4 now.


回答1:


I don't believe this can be done without iteration, which is a little annoying

You can view changes by hash

/httpAuth/app/rest/changes?version:SHA_HASH

and you can find changes by build locator

/httpAuth/app/rest/changes?locator=build:(id:BUILD_ID)

but you can't go the other way, otherwise this could be done simply.

The buildLocator doesn't allow you to query using a revision dimension so I can't see any way around this

The following script may be of use to you if you haven't already written it yourself - save this to a file called get-build-status-by-git-commit.ps1 so it works with the sample at the end

# -----------------------------------------------
# Get Build Status By Git Commit
# -----------------------------------------------
#
# Ver   Who         When      What
# 1.0   DevOpsGuys  01-07-15  Initial Version

# Script Input Parameters
param (
    [ValidateNotNullOrEmpty()]
    [string] $TeamCityServer = $(throw "-TeamCityServer is mandatory, please provide a value."),
    [ValidateNotNullOrEmpty()]
    [string] $ApiUsername = $(throw "-ApiUsername is mandatory, please provide a value."),
    [ValidateNotNullOrEmpty()]
    [string] $ApiPassword = $(throw "-ApiPassword is mandatory, please provide a value."),
    [ValidateNotNullOrEmpty()]
    [string] $GitSha = $(throw "-GitSha is mandatory, please provide a value.")
)

function Main() 
{
    $CurrentScriptVersion = "1.0"
    $ApiCredentials = New-Object System.Management.Automation.PSCredential($ApiUsername, (ConvertTo-SecureString $ApiPassword -AsPlainText -Force))

    Write-Host "================== Get Build Status By Git Commit - Version"$CurrentScriptVersion": START =================="

    # Log input variables passed in
    Log-Variables
    Write-Host

    # Set initial query url
    $queryBuilds = "/httpAuth/app/rest/builds?fields=nextHref,build(id,status,revisions)"

    while($queryBuilds)
    {
        $buildsToCheck = Api-Get "$TeamCityServer$queryBuilds"
        $queryBuilds = $buildsToCheck.builds.nextHref;
        foreach($build in $buildsToCheck.builds.build)
        {
            if ($build.revisions.revision.version -eq $GitSha) {
                Write-Host "STATUS: "$build.status
                Exit 0
            }
        }
    }

    Write-Host "================== Get Build Status By Git Commit - Version"$CurrentScriptVersion": END =================="
}

function Log-Variables
{
    Write-Host "TeamCityServer: " $TeamCityServer
    Write-Host "GitSha: " $GitSha
    Write-Host "Computername:" (gc env:computername)
}

function Api-Get($Url)
{
    Write-Host $Url
    return Invoke-RestMethod -Credential $ApiCredentials -Uri $Url -Method Get -TimeoutSec 20;
}

Main

You can use this in the following way

.\get-build-status-by-git-commit.ps1 "http://teamcity:8000/" username password 96b1db05b64ecc895da070137e93cde3d2cadfa1

This is using httpAuth, but you could easily tailor the script to be using guest. I've used httpAuth in case it's of use to anyone else.

Hope this helps




回答2:


I was looking for the same thing and stumbled upon this question. I don't know if you still need this but however I found the API in TeamCity v10. But you have to know your BuildTypeID for this

    https://${teamcity.domain}/app/rest/buildTypes/id:${BuildTypeID}/builds/revision:${COMMIT_SHA}

You can find your build type id from TeamCity UI by going to your specific build and then

    Edit configuration settings >> General Settings

And then the value in the Build configuration ID field.

Hope this helps.




回答3:


The related feature request in TeamCity issue tracker: https://youtrack.jetbrains.com/issue/TW-40540. Vote for it. The current workaround is to request builds with their revisions included into the response and then find the necessary builds on the client side. The request can look like:

.../app/rest/builds?locator=buildType(id:XXX)&fields=build(id,href,revisions(revision))




回答4:


Recent versions of teamcity allow the 'version' locator such that the following will return builds where the version SHA matches the query.

http://teamcity:8111/app/rest/changes?locator=version:SHA_HASH



来源:https://stackoverflow.com/questions/31164543/get-build-status-by-commit-sha1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!