diff --git a/source/OctopusTools/Commands/ApiCommand.cs b/source/OctopusTools/Commands/ApiCommand.cs index b5807944a..0d29da7f1 100644 --- a/source/OctopusTools/Commands/ApiCommand.cs +++ b/source/OctopusTools/Commands/ApiCommand.cs @@ -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; } } @@ -52,6 +52,11 @@ protected ILog Log get { return log; } } + protected string ServerBaseUrl + { + get { return serverBaseUrl; } + } + protected IOctopusRepository Repository { get { return repository; } diff --git a/source/OctopusTools/Commands/CreateReleaseCommand.cs b/source/OctopusTools/Commands/CreateReleaseCommand.cs index 069f917d9..7e9b59612 100644 --- a/source/OctopusTools/Commands/CreateReleaseCommand.cs +++ b/source/OctopusTools/Commands/CreateReleaseCommand.cs @@ -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); } diff --git a/source/OctopusTools/Diagnostics/LogExtensions.cs b/source/OctopusTools/Diagnostics/LogExtensions.cs index ded3ec513..a1518144d 100644 --- a/source/OctopusTools/Diagnostics/LogExtensions.cs +++ b/source/OctopusTools/Diagnostics/LogExtensions.cs @@ -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 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 @@ -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) @@ -50,10 +64,13 @@ 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 values) @@ -61,10 +78,13 @@ public static void ServiceMessage(this ILog log, string messageName, IDictionary 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) @@ -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)