Run TeamCity build with REST

I'm trying to execute a specific build using the REST API. At first, I tried to do some GET requests to TeamCity to see if I had access. As the JavaScript is located on a different server, I had CORS issues that I fixed by adding this in the internal properties file of TC:

rest.cors.optionsRequest.allowUnauthorized=true
rest.cors.origins=http://pdv-spdev22:8081

After that, it started working well. Then, I tried this:

var build = '<build> \
                    <buildType id="Internet_Development"/> \
                    <comment><text>build triggering comment</text></comment> \
                    <properties> \
                        <property name="env.install" value="false"/> \
                    </properties> \
                </build>';

var req = {
    method: 'POST',
    url: 'http://sts-spbld21:8080/httpAuth/app/rest/latest/buildQueue',
    headers: {
        'Content-Type': "application/xml"
    },
    data: build
}

$http(req).then
(
    function(result)
    {
        console.log('ok');
        console.log(result);
    },
    function (result)
    {
        console.log('nok');
        console.log(result);
    }
)

 

Now, when I execute this request, I get "XMLHttpRequest cannot load http://sts-spbld21:8080/httpAuth/app/rest/latest/buildQueue. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://pdv-spdev22:8081' is therefore not allowed access. The response had HTTP status code 401."

I also tried using the "authToken" parameter URL (after I added "rest.use.authToken=true" to the configuration file) and it still does not work (although I'm not sure I'm using it right as there is not a lot of information about it)...

I also tried to call it like this : "http://username:password@sts-spbld21:8080/httpAuth/app/rest/latest/buildQueue"

But still... 401.

I also found "https://youtrack.jetbrains.com/issue/TW-27606" but even when I use "/latest", it does not work... I'm starting to run out of idea :-D

3 comments

Hi Sébastien,

What TeamCity version do you use? There is a known issue, that was fixed in 9.1.1. Could you please upgrade if you use prior versions?

0

Sébastien,

I assume you have restarted the TeamCity server after adding "rest.cors.optionsRequest.allowUnauthorized=true" internal property as that is required.

Also, I think you should force set withCredentials to true in XMLHttpRequest and be logged in to the server in the same browser..

If that still does not help: If you enable debug-rest logging preset and repeat the call, do you get any corresponding entries in the teamcity-rest.log?

0

Please sign in to leave a comment.