예제 #1
0
        public static void OctoVersionDiscoverLocalGitBranch(out string branch, Action <LoggerConfiguration> additionLogs)
        {
            var(_, configuration) = ConfigurationBootstrapper.BootstrapWithoutValidation <AppSettings>();
            LogBootstrapper.Bootstrap(configuration, additionLogs);

            var stdout = new List <string>();
            var stderr = new List <string>();

            var exitCode = ShellExecutor.ExecuteCommand(
                "git",
                "rev-parse --abbrev-ref HEAD",
                Environment.CurrentDirectory,
                log => { },
                log => { stdout.Add(log); },
                log => { stdout.Add(log); }
                );

            if (exitCode != 0)
            {
                throw new Exception("Calling git binary to determine local branch failed.")
                      .WithData(nameof(stdout), stdout)
                      .WithData(nameof(stderr), stderr);
            }

            var bareBranch = stdout.FirstOrDefault() ?? throw new Exception("Failed to determine local branch.")
                                   .WithData(nameof(stdout), stdout)
                                   .WithData(nameof(stderr), stderr);

            branch = $"refs/heads/{bareBranch}";

            var environmentVariableName = $"{ConfigurationBootstrapper.EnvironmentVariablePrefix}{nameof(AppSettings.CurrentBranch)}";

            Environment.SetEnvironmentVariable(environmentVariableName, branch);

            Log.Warning("The current Git branch has been automatically determined to be {branch}.", branch);
            Log.Warning($"It is STRONGLY RECOMMENDED to NOT rely on automatic branch detection on your build agents. It will fail in unexpected ways for tags, pull requests, commit hashes etc. Please set the {environmentVariableName} variable deterministically instead");
        }
예제 #2
0
        public void Run(out OctoVersionInfo versionInfo)
        {
            var outputFormatters = new OutputFormattersProvider().GetFormatters(appSettings.OutputFormats);

            LogBootstrapper.Bootstrap(configuration,
                                      lc =>
            {
                foreach (var outputFormatter in outputFormatters)
                {
                    lc.WriteTo.Sink(outputFormatter.LogSink);
                }

                // Special case: if we're writing to the console then use LiterateConsole
                if (outputFormatters.OfType <ConsoleOutputFormatter>().Any())
                {
                    lc.WriteTo.LiterateConsole();
                }
                additionalLogConfiguration(lc);
            });
            Log.Debug("Running OctoVersion {OctoVersionVersion} with {@AppSettings}", ApplicationVersion, appSettings);

            var currentDirectory = Directory.GetCurrentDirectory();

            Log.Debug("Executing in {Directory}", currentDirectory);

            foreach (var outputFormatter in outputFormatters)
            {
                Log.Debug("Writing build output using {OutputFormatter}", outputFormatter.GetType().Name);
            }

            if (!string.IsNullOrWhiteSpace(appSettings.FullSemVer))
            {
                Log.Information("Adopting previously-provided version information {FullSemVer}. Not calculating a new version number.", appSettings.FullSemVer);
                var semanticVersion = SemanticVersion.TryParse(appSettings.FullSemVer);
                if (semanticVersion == null)
                {
                    throw new Exception("Failed to parse semantic version string");
                }

                versionInfo = new OctoVersionInfo(semanticVersion);
            }
            else
            {
                using (Log.Logger.BeginTimedOperation("Calculating version"))
                {
                    var repositorySearchPath = string.IsNullOrWhiteSpace(appSettings.RepositoryPath)
                        ? currentDirectory
                        : appSettings.RepositoryPath;

                    var versionCalculatorFactory = new VersionCalculatorFactory(repositorySearchPath);
                    var calculator = versionCalculatorFactory.Create();
                    var version    = calculator.GetVersion();
                    var currentSha = calculator.CurrentCommitHash;
                    versionInfo = new StructuredOutputFactory(appSettings.NonPreReleaseTags,
                                                              appSettings.NonPreReleaseTagsRegex,
                                                              appSettings.Major,
                                                              appSettings.Minor,
                                                              appSettings.Patch,
                                                              appSettings.CurrentBranch,
                                                              currentSha,
                                                              appSettings.BuildMetadata)
                                  .Create(version);
                }
            }

            Log.Information("Version is {FullSemVer}", versionInfo.FullSemVer);

            foreach (var outputFormatter in outputFormatters)
            {
                outputFormatter.Write(versionInfo);
            }
        }