Posts tagged ‘TeamCity’

TeamCity replacing build agent

We have the Professional version of TeamCity at work which allows 3 build agents / machines and 20 users. It’s big enough for our efforts. We just put an old build machine to bed and a new one is coming online. However I could not find how to remove agent from the TeamCity interface. First I uninstalled the agent on the old build machine, but the profile was still on the server. I ended up going into the Oracle TeamCity database and deleting the profile from the AGENTS table. After that I was able to add the new build agent in the TeamCity server console.

NCover error

I ran into a problem when adding NCover to our UnitTests on a new project the error I got when using NCover with Gallio was the following, Profiled process terminated. Profiler connection not established. The FAQ that is in the NCover directory bundled with Gallio suggested – If using the command-line, did you COM register CoverLib.dll ?

Sure enough, just run
>regsvr32 CoverLib.dll
from a command prompt, that fixed the problem on both our build machines.

TeamCity update to version 4.5.5

I Just updated our TeamCity environment to version 4.5.5 today, we were running 3.1 before. I had resisted an update as I always do because things just break when you update software. Since we are having some network issues it was a perfect time to take the plunge. That is after we discovered that TC 4.5+ can use Team Foundation Server repository. As the MsBuild just is not mature enough yet and there are weird quirks all around we are going to use combo TC and TFS setup for a new project we just started. Mainly running the build using nant and nunit of course.
Back to the update, I took a backup of the TC server directory just in case. Then fired up the install, the install recognized the old version and offered to get rid of it for me. The install ran fairly quickly and picked up all the configurations from the last install. After the server was up the build agents got pushed to the new version and came online in a matter of couple of minutes, now that is sweet. No manual installs on the build agent machines, its auto ! Then I just kicked off a build and everything was business as usual, it can’t get any better than this.

Java Authenticated POST

I came across this TeamCity Twitter plugin via StackOverflow the idea is to Tweet the build status as it changes. Pretty neat idea, however it will only run under Java 1.6. I tried to install on our build server at work but the TeamCity version we are running doesn’t run under Java 1.6 There was only one thing to do, write my own that will work under Java 1.5. It has to do authenticated POST request to the Twitter REST API. The build Twitter account can be found here, not that your that interested in our build Twitter feed 🙂

//
import java.io.*;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.net.URLEncoder;
import java.net.URLConnection;
//
import jetbrains.buildServer.serverSide.BuildServerAdapter;
import jetbrains.buildServer.serverSide.SBuildServer;
import jetbrains.buildServer.serverSide.SRunningBuild;
import jetbrains.buildServer.messages.Status;
//
import com.intellij.openapi.diagnostic.Logger;

// Tweets build start / finish to the build account
public class TwitterNotifier extends BuildServerAdapter
{
   private final static Logger LOG = Logger.getInstance(TwitterNotifier.class.getName());
   
   
   // Constructor, register the listener ( this )
   public TwitterNotifier(SBuildServer aBuildServer)
   {
       LOG.info(“*** Notifier adding listener=” + this );

       // Register with TC
       aBuildServer.addListener(this);
   }

   // Build started, set the version number
   public void buildStarted(SRunningBuild build)
   {
       String sTcBuildNumber = build.getBuildNumber();
       sendTweet( “Build “ + sTcBuildNumber + ” started on “ + build.getAgentName() );
   }

   public void buildChangedStatus(SRunningBuild build, Status oldStatus, Status newStatus)
   {
           String sTcBuildNumber = build.getBuildNumber();
       sendTweet( “Build “ + sTcBuildNumber + ” status, “ + newStatus.getText() );
   }
     
   public void buildInterrupted(SRunningBuild build)
   {
           String sTcBuildNumber = build.getBuildNumber();
       sendTweet( “Build “ + sTcBuildNumber + ” interrupted status, “ + build.getBuildStatus().getText() );
   }
   
   public void buildFinished(SRunningBuild build)
   {
           String sTcBuildNumber = build.getBuildNumber();
       sendTweet( “Build “ + sTcBuildNumber + ” finished, “ + build.getBuildStatus().getText() );          
   }
   
   // Updates the twitter account
   public static void sendTweet( String psText )
   {
           try
           {
               LOG.info( “*** TwitterNotifier plugin, “ + psText );
              
               //  Using paramaters known to work
               String urlString = “http://twitter.com/statuses/update.xml”;
               String data = “status=” + URLEncoder.encode( psText, “UTF-8”);
               String username = “twitteruser”;
               String password = “twitterpassword”;
              
               // 
               Authenticator.setDefault(new MyAuthenticator(username, password));
               // Send data
                URL url = new URL(  urlString );
                URLConnection conn = url.openConnection();
                conn.setDoOutput(true);
                OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
                out.write(data);
                out.flush();
                
                // Get the response
                StringBuffer sb = new StringBuffer();
                BufferedReader readIn = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                String line;
                while ((line = readIn.readLine()) != null)
                        sb.append(line + \n );

                //
                //System.out.println( "*** Response=" + sb.toString() );
                
                // Close       
                out.close();
                readIn.close();
           }
           catch( Exception ex )
           {
               LOG.info( “***ERROR=” + exception2string( ex ) );
           }
   }

   public static String exception2string(Exception e)
   {
      try
      {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        return “——\r\n + sw.toString() + “——\r\n;
      }
      catch(Exception ex)
      {
        return “error converting exception to string”;
      }
   }

   
   static class MyAuthenticator extends Authenticator
   {
       private String username, password;

       public MyAuthenticator(String user, String pass)
       {
         username = user;
         password = pass;
       }

       protected PasswordAuthentication getPasswordAuthentication()
       {
         /*
         System.out.println("Requesting Host  : " + getRequestingHost());
         System.out.println("Requesting Port  : " + getRequestingPort());
         System.out.println("Requesting Prompt : " + getRequestingPrompt());
         System.out.println("Requesting Protocol: "
             + getRequestingProtocol());
         System.out.println("Requesting Scheme : " + getRequestingScheme());
         System.out.println("Requesting Site  : " + getRequestingSite());
         */

         return new PasswordAuthentication(username, password.toCharArray());
       }
   }

}  // EOC