October 30, 2017 at 6:36 am
Has anyone managed to initiate a TeamCity (TC) build from an SSIS script task?
This is quite a simple task in a native C# application, using httpClient.PostAsync. But if I try to use async tasks in SSIS, things end badly, with runtime errors and, if I'm being very naughty, minidumps.
So I tried to write synchronous code to do the same thing, so far without success.
If anyone has had success with either of the following, I'd like to hear from them:
1) Including async tasks in a Script Task
2) Making a synchronous POST to TC.
October 30, 2017 at 9:18 am
OK, managed to crack this in the end. Here is some code which may prove useful to others one day. It initiates a TC build and returns the Id of the build which is created ... synchronously. Tested & works in SSIS.
    public string GetBuildId(string username, string password, string buildQueueRoot, string buildTypeId)
   {
    try
    {
      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(buildQueueRoot);
      request.Credentials = new NetworkCredential(username, password);
      byte[] bytes;
      string requestXML = string.Concat("<build> <buildType id=\"", buildTypeId, "\"/> <comment><text>Build triggered through REST API</text></comment> </build>");
      bytes = System.Text.Encoding.ASCII.GetBytes(requestXML);
      request.ContentType = "application/xml";
      request.ContentLength = bytes.Length;
      request.Accept = "application/xml";
      request.Method = "POST";
      Stream requestStream = request.GetRequestStream();
      requestStream.Write(bytes, 0, bytes.Length);
      requestStream.Close();
      HttpWebResponse response;
      response = (HttpWebResponse)request.GetResponse();
      if (response.StatusCode == HttpStatusCode.OK)
      {
       Stream responseStream = response.GetResponseStream();
       string responseStr = new StreamReader(responseStream).ReadToEnd();
       XmlDocument xd = new XmlDocument();
       xd.LoadXml(responseStr);
       XmlNode buildInfo = xd.DocumentElement.SelectSingleNode("/build");
       string id = buildInfo.Attributes["id"]?.InnerText;
       return id;
      }
      return null;
    }
    catch (Exception e)
    {
      throw e;
    }
   }
September 23, 2018 at 6:28 pm
I just tried this, and by "tried" I mean copied and pasted your code into my Visual Studio and it worked on the first try - thank you so much 😉
September 25, 2018 at 6:54 am
kendall 39844 - Sunday, September 23, 2018 6:28 PMI just tried this, and by "tried" I mean copied and pasted your code into my Visual Studio and it worked on the first try - thank you so much 😉
Glad to hear it!
Viewing 4 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply