Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically pin a build in Teamcity

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

like image 242
ThorHalvor Avatar asked Jul 01 '11 08:07

ThorHalvor


3 Answers

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>
like image 34
carlspring Avatar answered Sep 28 '22 23:09

carlspring


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.

like image 44
Tobel Avatar answered Sep 28 '22 23:09

Tobel


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.

like image 116
ThorHalvor Avatar answered Sep 28 '22 22:09

ThorHalvor