Skip to content

Commit

Permalink
Merge pull request #70 from OctopusDeploy/feature-vstslog
Browse files Browse the repository at this point in the history
Feature vstslog
  • Loading branch information
michaelnoonan committed Feb 18, 2016
2 parents 9299f08 + 29928ef commit 32d29e5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 deletions.
7 changes: 6 additions & 1 deletion source/OctopusTools/Commands/ApiCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected ApiCommand(IOctopusRepositoryFactory repositoryFactory, ILog log, IOct
options.Add("configFile=", "[Optional] Text file of default values, with one 'key = value' per line.", v => ReadAdditionalInputsFromConfigurationFile(v));
options.Add("debug", "[Optional] Enable debug logging", v => enableDebugging = true);
options.Add("ignoreSslErrors", "[Optional] Set this flag if your Octopus server uses HTTPS but the certificate is not trusted on this machine. Any certificate errors will be ignored. WARNING: this option may create a security vulnerability.", v => ignoreSslErrors = true);
options.Add("enableServiceMessages", "[Optional] Enable TeamCity service messages when logging.", v => log.EnableServiceMessages());
options.Add("enableServiceMessages", "[Optional] Enable TeamCity or Team Foundation Build service messages when logging.", v => log.EnableServiceMessages());
}

protected Options Options { get { return optionGroups; } }
Expand All @@ -52,6 +52,11 @@ protected ILog Log
get { return log; }
}

protected string ServerBaseUrl
{
get { return serverBaseUrl; }
}

protected IOctopusRepository Repository
{
get { return repository; }
Expand Down
2 changes: 1 addition & 1 deletion source/OctopusTools/Commands/CreateReleaseCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ protected override void Execute()
SelectedPackages = plan.GetSelections()
}, Force);
Log.Info("Release " + release.Version + " created successfully!");

Log.ServiceMessage("setParameter", new {name = "octo.releaseNumber", value = release.Version});
Log.TfsServiceMessage(ServerBaseUrl, project, release);

DeployRelease(project, release, DeployToEnvironmentNames);
}
Expand Down
51 changes: 43 additions & 8 deletions source/OctopusTools/Diagnostics/LogExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,27 @@
using System.ComponentModel;
using System.Linq;
using log4net;
using Octopus.Client.Model;

namespace OctopusTools.Diagnostics
{
public enum BuildEnvironment
{
NoneOrUnknown,
TeamCity,
TeamFoundationBuild
}

public static class LogExtensions
{
static readonly Dictionary<string, string> Escapes;
static bool serviceMessagesEnabled;
static BuildEnvironment buildEnvironment;

static LogExtensions()
{
serviceMessagesEnabled = false;
buildEnvironment = BuildEnvironment.NoneOrUnknown;

// As per: http://confluence.jetbrains.com/display/TCD65/Build+Script+Interaction+with+TeamCity#BuildScriptInteractionwithTeamCity-ServiceMessages
Escapes = new Dictionary<string, string>
Expand All @@ -33,6 +43,10 @@ static LogExtensions()
public static void EnableServiceMessages(this ILog log)
{
serviceMessagesEnabled = true;
buildEnvironment = string.IsNullOrEmpty(Environment.GetEnvironmentVariable("BUILD_BUILDID"))
? string.IsNullOrEmpty(Environment.GetEnvironmentVariable("TEAMCITY_VERSION")) ? BuildEnvironment.NoneOrUnknown : BuildEnvironment.TeamCity
: BuildEnvironment.TeamFoundationBuild;
log.InfoFormat("Build environment is {0}", buildEnvironment);
}

public static void DisableServiceMessages(this ILog log)
Expand All @@ -50,21 +64,27 @@ public static void ServiceMessage(this ILog log, string messageName, string valu
if (!serviceMessagesEnabled)
return;

log.InfoFormat(
"##teamcity[{0} {1}]",
messageName,
EscapeValue(value));
if (buildEnvironment == BuildEnvironment.TeamCity || buildEnvironment == BuildEnvironment.NoneOrUnknown)
{
log.InfoFormat(
"##teamcity[{0} {1}]",
messageName,
EscapeValue(value));
}
}

public static void ServiceMessage(this ILog log, string messageName, IDictionary<string, string> values)
{
if (!serviceMessagesEnabled)
return;

log.InfoFormat(
"##teamcity[{0} {1}]",
messageName,
string.Join(" ", values.Select(v => v.Key + "='" + EscapeValue(v.Value) + "'")));
if (buildEnvironment == BuildEnvironment.TeamCity || buildEnvironment == BuildEnvironment.NoneOrUnknown)
{
log.InfoFormat(
"##teamcity[{0} {1}]",
messageName,
string.Join(" ", values.Select(v => v.Key + "='" + EscapeValue(v.Value) + "'")));
}
}

public static void ServiceMessage(this ILog log, string messageName, object values)
Expand All @@ -84,6 +104,21 @@ public static void ServiceMessage(this ILog log, string messageName, object valu
}
}

public static void TfsServiceMessage(this ILog log, string serverBaseUrl, ProjectResource project, ReleaseResource release)
{
if (!serviceMessagesEnabled)
return;
if (buildEnvironment == BuildEnvironment.TeamFoundationBuild || buildEnvironment == BuildEnvironment.NoneOrUnknown)
{
var workingDirectory = Environment.GetEnvironmentVariable("SYSTEM_DEFAULTWORKINGDIRECTORY") ?? new System.IO.FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).DirectoryName;
var selflink = new Uri(new Uri(serverBaseUrl), release.Links["Web"].AsString());
var markdown = string.Format("[Release {0} created for '{1}']({2})", release.Version, project.Name, selflink);
var markdownFile = System.IO.Path.Combine(workingDirectory, Guid.NewGuid() + ".md");
System.IO.File.WriteAllText(markdownFile, markdown);
log.InfoFormat("##vso[task.addattachment type=Distributedtask.Core.Summary;name=Octopus Deploy;]{0}", markdownFile);
}
}

static string EscapeValue(string value)
{
if (value == null)
Expand Down

0 comments on commit 32d29e5

Please sign in to comment.