Skip to content

Commit

Permalink
Update the build to support publishing pre-release versions
Browse files Browse the repository at this point in the history
  • Loading branch information
aaubry committed Jan 15, 2021
1 parent e78ed0c commit cf25b75
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
31 changes: 27 additions & 4 deletions tools/build/BuildDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ public static GitVersion ResolveVersion(Options options, PreviousReleases releas
var versionJson = Read("dotnet", $"gitversion /nofetch{(options.Verbose ? " /diag" : "")}", BasePath);
WriteVerbose(versionJson);

if (options.Verbose)
{
// Remove extra output from versionJson
var lines = versionJson
.Split('\n')
.Select(l => l.TrimEnd('\r'))
.SkipWhile(l => !l.StartsWith('{'))
.TakeWhile(l => !l.StartsWith('}'))
.Append("}");

versionJson = string.Join('\n', lines);
}

var jsonOptions = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
Expand Down Expand Up @@ -109,7 +122,7 @@ public static NuGetPackage Pack(Options options, GitVersion version, SuccessfulU
return new NuGetPackage(packagePath);
}

public static void Publish(Options options, NuGetPackage package)
public static void Publish(Options options, GitVersion version, NuGetPackage package)
{
var apiKey = Environment.GetEnvironmentVariable("NUGET_API_KEY");
if (string.IsNullOrEmpty(apiKey))
Expand All @@ -131,6 +144,12 @@ public static void Publish(Options options, NuGetPackage package)
{
Console.WriteLine($"nuget push {package.Path} -ApiKey *** -Source https://api.nuget.org/v3/index.json");
Run("nuget", $"push {package.Path} -ApiKey {apiKey} -Source https://api.nuget.org/v3/index.json", noEcho: true);

if (version.IsPreRelease)
{
Console.WriteLine($"nuget delete YamlDotNet {version.NuGetVersion} -ApiKey *** -Source https://api.nuget.org/v3/index.json");
Run("nuget", $"delete YamlDotNet {version.NuGetVersion} -ApiKey {apiKey} -Source https://api.nuget.org/v3/index.json", noEcho: true);
}
}
}

Expand Down Expand Up @@ -196,7 +215,7 @@ public static ScaffoldedRelease ScaffoldReleaseNotes(GitVersion version, Previou
}
WriteVerbose(releaseNotes);

return new ScaffoldedRelease(releaseNotes, reviewed);
return new ScaffoldedRelease(releaseNotes, releaseNotesPath, reviewed);
}

public static PreviousReleases DiscoverPreviousReleases()
Expand Down Expand Up @@ -252,6 +271,7 @@ public static void Release(GitVersion version, ScaffoldedRelease scaffoldedRelea
File.WriteAllText(releaseNotesPath, releaseNotesFile);

Run("git", $"add \"{releaseNotesPath}\"");
Run("git", $"add \"{scaffoldedRelease.ReleaseNotesPath}\"");
Run("git", $"commit -m \"Prepare release {version.NuGetVersion}\"");
Run("git", $"tag v{version.NuGetVersion}");

Expand Down Expand Up @@ -377,13 +397,14 @@ public class GitVersion : IEquatable<Version>
public int Minor { get; set; }
public int Patch { get; set; }
public string? PreReleaseLabel { get; set; }
public string? CommitsSinceVersionSourcePadded { get; set; }

public string NuGetVersion
{
get
{
return IsPreRelease
? $"{MajorMinorPatch}-{PreReleaseLabel}"
? $"{MajorMinorPatch}-{PreReleaseLabel}-{CommitsSinceVersionSourcePadded}"
: MajorMinorPatch;
}
}
Expand Down Expand Up @@ -411,13 +432,15 @@ public struct SuccessfulUnitTests { }

public class ScaffoldedRelease
{
public ScaffoldedRelease(string releaseNotes, bool reviewed)
public ScaffoldedRelease(string releaseNotes, string releaseNotesPath, bool reviewed)
{
ReleaseNotes = releaseNotes;
ReleaseNotesPath = releaseNotesPath;
Reviewed = reviewed;
}

public string ReleaseNotes { get; set; }
public string ReleaseNotesPath { get; set; }
public bool Reviewed { get; }
}

Expand Down
9 changes: 5 additions & 4 deletions tools/build/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,19 @@ static int Main(string[] args)
switch (options.Host)
{
case Host.Appveyor:

// Default CI targets for AppVeyor
var isBuildingMaster = Environment.GetEnvironmentVariable("APPVEYOR_REPO_BRANCH")?.Equals("master", StringComparison.Ordinal) ?? true;
var isBuildingRelease = Environment.GetEnvironmentVariable("APPVEYOR_REPO_TAG")?.Equals("true", StringComparison.OrdinalIgnoreCase) ?? false;
if (isBuildingRelease)
if (isBuildingRelease || !isBuildingMaster)
{
WriteInformation("Release build detected");
WriteInformation("Publishable build detected");
targets.Add(nameof(BuildDefinition.SetBuildVersion));
targets.Add(nameof(BuildDefinition.Publish));
}
else
{
WriteInformation("Non-release build detected");
WriteInformation("Non-publishable build detected");
targets.Add(nameof(BuildDefinition.SetBuildVersion));
targets.Add(nameof(BuildDefinition.Pack));
}
Expand Down

0 comments on commit cf25b75

Please sign in to comment.