Programmatically pin a build in Teamcity

前端 未结 4 1958
不思量自难忘°
不思量自难忘° 2020-12-31 10:32

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

相关标签:
4条回答
  • 2020-12-31 10:59

    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

    0 讨论(0)
  • 2020-12-31 11:02

    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.

    0 讨论(0)
  • 2020-12-31 11:05

    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>
    
    0 讨论(0)
  • 2020-12-31 11:07

    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.

    0 讨论(0)
提交回复
热议问题