11

Is it possible to pin a build in Teamcity programmatically/automatically? I want to pin a build if a Deploy-build is successfull.

Cœur
  • 37,241
  • 25
  • 195
  • 267
ThorHalvor
  • 628
  • 5
  • 17

4 Answers4

4

I would like to challenge the accepted answer with an up-to-date answer, which was tested with TeamCity 9 EAP 4 (build 31717) and 8.1.x.

Tagging and pinning could be implemented via a simple plugin that contains just an event adapter such as the following:

package com.foo;

import com.intellij.openapi.diagnostic.Logger;
import jetbrains.buildServer.messages.Status;
import jetbrains.buildServer.serverSide.BuildServerAdapter;
import jetbrains.buildServer.serverSide.BuildServerListener;
import jetbrains.buildServer.serverSide.SRunningBuild;
import jetbrains.buildServer.util.EventDispatcher;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.Map;

public class MyEventAdapter extends BuildServerAdapter
{

    private final static Logger logger = Logger.getInstance(MyEventAdapter.class.getName());


    public MyEventAdapter(@NotNull EventDispatcher<BuildServerListener> serverDispatcher)
    {
        serverDispatcher.addListener(this);
    }

    @Override
    public void buildFinished(@NotNull SRunningBuild build)
    {
        logger.debug("#");
        logger.debug("# Build finished: ");
        logger.debug("# name: " + build.getBuildTypeName() + ";" +
                     " id: " + build.getBuildId() + ";" +
                     " build number: " + build.getBuildNumber() + "; " +
                     " owner: " + build.getTriggeredBy().getUser().getName());
        logger.debug("# status: " + build.getBuildStatus());
        logger.debug("# ---------------------------------------------------");

        super.buildFinished(build);

        if (build.getBuildStatus().equals(Status.NORMAL))
        {
            if (someConditionCheckWhetherToTagAndPinGoesHere())
            {
                final String tag = "dev";

                // Pin the build:
                if (build.getBuildType() != null && build.getBuildType().getLastChangesSuccessfullyFinished() != null)
                {
                    build.getBuildType().getLastChangesSuccessfullyFinished().setPinned(true, build.getOwner(), "This is a " + tag + " build.");
                }

                // Tag the build:
                build.setTags(build.getOwner(), Arrays.asList(tag));
            }
        }
    }

}

You'll also need to have a Spring context under src/main/resources/META-INF/my-plugin.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-autowire="constructor">

    <bean class="com.foo.MyEventAdapter"/>

</beans>
carlspring
  • 31,231
  • 29
  • 115
  • 197
4

Just found out that its possible through the REST API I can f.ex send a PUT command like this http://teamcityserver:81/httpAuth/app/rest/builds/id:688/pin/ and then the build with id 688 (teamcity.build.id) will be pinned.

ThorHalvor
  • 628
  • 5
  • 17
  • @RickardN: You can use guestAuth: `http://YourServer:Port/guestAuth/app/rest/builds/id:688/pin/`. I would be highly cautious of what permissions you allow under guest authentication though. – Mike Bailey Aug 28 '14 at 14:37
3

Inspired by carlspring's answer, I wrote a little teamcity plugin that programmatically adds tags to your build:

https://github.com/echocat/teamcity-buildTagsViaBuildLog-plugin

You could easily modify it to also pin your build. Furthermore, it might be helpful to tag your successful builds instead of pinning them and use the tag as a filter.

Tobel
  • 66
  • 4
  • The plugin requires you putting something in your build-log for it to work, this could have simply been done with a curl call from a build-step. `curl --user ${username}:${password} -X POST --data "${tags}" %teamcity.serverUrl%/httpAuth/app/rest/8.0/builds/id:%teamcity.build.id%/tags --header "Content-Type: text/plain"` In fact, one can make a metarunner from the above curl call and tag running builds from a build step. Jetbrains even has a metarunner power pack that has a similar metarunner using ant. – Amit Feb 24 '15 at 15:35
0

If you are willing to install a plugin, I wrote one that is able to tag and pin builds programmatically based on build features or system messages.

https://github.com/ocroquette/teamcity-autopin

See also: https://youtrack.jetbrains.com/issue/TW-38017

ocroquette
  • 3,049
  • 1
  • 23
  • 26