Skip to content

Commit

Permalink
ci: Artifactbuilder parses output from nuget pack (#1699)
Browse files Browse the repository at this point in the history
* Modifies Nuget build / validation to capture and parse stdout from the `nuget pack` command, to obtain the actual filename of the NuGet package that was generated and use that in content validation.

* Use regex to parse output from nuget pack
  • Loading branch information
tippmar-nr authored Jun 7, 2023
1 parent 0e86e4b commit 3520f7f
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 18 deletions.
8 changes: 6 additions & 2 deletions build/ArtifactBuilder/Artifacts/AzureSiteExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class AzureSiteExtension : Artifact
private const string NuGetHelperLibraryName = "NewRelic.NuGetHelper.dll";

private string _version;
private string _nuGetPackageName;

public AzureSiteExtension() : base(nameof(AzureSiteExtension))
{
Expand All @@ -26,7 +27,7 @@ protected override void InternalBuild()
package.CopyToContent($@"{RepoRootDirectory}\build\NewRelic.NuGetHelper\bin\{NuGetLibraryName}");
package.CopyToContent($@"{RepoRootDirectory}\build\NewRelic.NuGetHelper\bin\{XmlLibraryName}");
package.SetVersion(_version);
package.Pack();
_nuGetPackageName = package.Pack();
}

private string ReadVersionFromFile()
Expand Down Expand Up @@ -68,8 +69,11 @@ private void ValidateContent()

private string Unpack()
{
if (string.IsNullOrEmpty(_nuGetPackageName))
throw new PackagingException("NuGet package name not found. Did you call InternalBuild()?");

var unpackDir = Path.Join(OutputDirectory, "unpacked");
var nugetFile = Path.Join(OutputDirectory, $"NewRelic.Azure.WebSites.Extension.{_version}.nupkg");
var nugetFile = Path.Join(OutputDirectory, _nuGetPackageName);
NuGetHelpers.Unpack(nugetFile, unpackDir);
return unpackDir;
}
Expand Down
8 changes: 6 additions & 2 deletions build/ArtifactBuilder/Artifacts/NugetAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class NugetAgent : Artifact
private readonly AgentComponents _coreAgentComponents;
private readonly AgentComponents _coreAgentArm64Components;
private readonly AgentComponents _coreAgentX86Components;
private string _nuGetPackageName;

public NugetAgent(string configuration)
: base(nameof(NugetAgent))
Expand Down Expand Up @@ -76,7 +77,7 @@ protected override void InternalBuild()

package.SetVersion(_frameworkAgentComponents.Version);

package.Pack();
_nuGetPackageName = package.Pack();
}

private static void TransformNewRelicConfig(string newRelicConfigPath)
Expand Down Expand Up @@ -120,8 +121,11 @@ private void ValidateContent()

private string Unpack()
{
if (string.IsNullOrEmpty(_nuGetPackageName))
throw new PackagingException("NuGet package name not found. Did you call InternalBuild()?");

var unpackDir = Path.Join(OutputDirectory, "unpacked");
var nugetFile = Path.Join(OutputDirectory, $"NewRelic.Agent.{_frameworkAgentComponents.MaybeSemanticVersion}.nupkg");
var nugetFile = Path.Join(OutputDirectory, _nuGetPackageName);
NuGetHelpers.Unpack(nugetFile, unpackDir);
return unpackDir;
}
Expand Down
8 changes: 6 additions & 2 deletions build/ArtifactBuilder/Artifacts/NugetAgentApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class NugetAgentApi : Artifact
{
private readonly AgentComponents _frameworkAgentComponents;
private readonly AgentComponents _coreAgentComponents;
private string _nuGetPackageName;

public NugetAgentApi(string configuration)
: base(nameof(NugetAgentApi))
Expand All @@ -34,7 +35,7 @@ protected override void InternalBuild()
package.CopyToRoot(_frameworkAgentComponents.NewRelicLicenseFile);
package.CopyToRoot(_frameworkAgentComponents.NewRelicThirdPartyNoticesFile);
package.SetVersion(_frameworkAgentComponents.Version);
package.Pack();
_nuGetPackageName = package.Pack();
}

private void ValidateContent()
Expand All @@ -52,8 +53,11 @@ private void ValidateContent()

private string Unpack()
{
if (string.IsNullOrEmpty(_nuGetPackageName))
throw new PackagingException("NuGet package name not found. Did you call InternalBuild()?");

var unpackDir = Path.Join(OutputDirectory, "unpacked");
var nugetFile = Path.Join(OutputDirectory, $"NewRelic.Agent.Api.{_frameworkAgentComponents.MaybeSemanticVersion}.nupkg");
var nugetFile = Path.Join(OutputDirectory, _nuGetPackageName);
NuGetHelpers.Unpack(nugetFile, unpackDir);
return unpackDir;
}
Expand Down
8 changes: 6 additions & 2 deletions build/ArtifactBuilder/Artifacts/NugetAzureCloudServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace ArtifactBuilder.Artifacts
public class NugetAzureCloudServices : Artifact
{
private readonly AgentComponents _frameworkAgentComponents;
private string _nuGetPackageName;

public NugetAzureCloudServices(string configuration)
: base(nameof(NugetAzureCloudServices))
Expand All @@ -29,7 +30,7 @@ protected override void InternalBuild()
package.CopyToLib(_frameworkAgentComponents.AgentApiDll);
package.CopyToContent($@"{RepoRootDirectory}\src\_build\x64-{Configuration}\Installer\{GetMsiName()}");
package.SetVersion(_frameworkAgentComponents.Version);
package.Pack();
_nuGetPackageName = package.Pack();
}

private void DoInstallerReplacements(string agentInstaller)
Expand Down Expand Up @@ -77,8 +78,11 @@ private void ValidateContent()

private string Unpack()
{
if (string.IsNullOrEmpty(_nuGetPackageName))
throw new PackagingException("NuGet package name not found. Did you call InternalBuild()?");

var unpackDir = Path.Join(OutputDirectory, "unpacked");
var nugetFile = Path.Join(OutputDirectory, $"NewRelicWindowsAzure.{_frameworkAgentComponents.MaybeSemanticVersion}.nupkg");
var nugetFile = Path.Join(OutputDirectory, _nuGetPackageName);
NuGetHelpers.Unpack(nugetFile, unpackDir);
return unpackDir;
}
Expand Down
12 changes: 6 additions & 6 deletions build/ArtifactBuilder/Artifacts/NugetAzureWebSites.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public NugetAzureWebSites(string platform, string configuration)
public string Configuration { get; }
public string Platform { get; }
private readonly AgentComponents _frameworkAgentComponents;
private string _nuGetPackageName;

private string RootDirectory => $@"{StagingDirectory}\content\newrelic";

Expand All @@ -38,7 +39,7 @@ protected override void InternalBuild()

agentInfo.WriteToDisk(RootDirectory);
package.SetVersion(_frameworkAgentComponents.Version);
package.Pack();
_nuGetPackageName = package.Pack();
}

private void TransformNewRelicConfig()
Expand Down Expand Up @@ -85,13 +86,12 @@ private void ValidateContent()

private string Unpack()
{
if (string.IsNullOrEmpty(_nuGetPackageName))
throw new PackagingException("NuGet package name not found. Did you call InternalBuild()?");

var unpackDir = Path.Join(OutputDirectory, "unpacked");

// The two packages have different naming conventions
var fileName = Platform == "x64" ?
$"NewRelic.Azure.WebSites.{Platform}.{_frameworkAgentComponents.MaybeSemanticVersion}.nupkg" :
$"NewRelic.Azure.WebSites.{_frameworkAgentComponents.MaybeSemanticVersion}.nupkg";
var nugetFile = Path.Join(OutputDirectory, fileName);
var nugetFile = Path.Join(OutputDirectory, _nuGetPackageName);

NuGetHelpers.Unpack(nugetFile, unpackDir);
return unpackDir;
Expand Down
39 changes: 37 additions & 2 deletions build/ArtifactBuilder/NuGetHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;

namespace ArtifactBuilder
{
public static class NuGetHelpers
{
private static readonly string _nugetPath = Path.Combine(FileHelpers.GetRepoRootDirectory(), @"Build\Tools\nuget.exe");

public static void Pack(string nuspecFilePath, string outputDirectory)
public static string Pack(string nuspecFilePath, string outputDirectory)
{
var parameters = $@"Pack -NoPackageAnalysis ""{nuspecFilePath}"" -OutputDirectory ""{outputDirectory}""";
var process = Process.Start(_nugetPath, parameters);
var process = new Process();

var startInfo = new ProcessStartInfo
{
FileName = _nugetPath,
Arguments = parameters,
RedirectStandardOutput = true
};
process.StartInfo = startInfo;

process.Start();
process.WaitForExit(30000);
if (!process.HasExited)
{
Expand All @@ -22,6 +33,30 @@ public static void Pack(string nuspecFilePath, string outputDirectory)
{
throw new Exception($"Nuget pack failed with exit code {process.ExitCode}.");
}

var stdOut = process.StandardOutput;
var output = stdOut.ReadToEnd();
Console.WriteLine(output);

// output is expected to look like:
// Attempting to build package from 'NewRelic.Azure.WebSites.x64.nuspec'.
// Successfully created package 'C:\Source\Repos\newrelic-dotnet-agent\Build\BuildArtifacts\NugetAzureWebSites-x64\NewRelic.Azure.WebSites.x64.10.11.0.nupkg'.

// capture the full path
var regex = ".*Successfully created package '(.*)'.*";
var matches = Regex.Match(output, regex);
if (matches.Success && matches.Groups.Count > 1)
{
var packagePath = matches.Groups[1].Value;

// verify the file exists, then return the filename
if (File.Exists(packagePath))
{
return Path.GetFileName(packagePath);
}
}

throw new PackagingException("Failed to parse NuGet package filename.");
}

public static void Unpack(string nupkgFile, string outputDirectory)
Expand Down
4 changes: 2 additions & 2 deletions build/ArtifactBuilder/NugetPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ public void CopyToRoot(IEnumerable<string> filePaths, string subDirectory = null
FileHelpers.CopyFile(filePaths, $@"{StagingDirectory}\{subDirectory}");
}

public void Pack()
public string Pack()
{
NuGetHelpers.Pack(NuspecFilePath, OutputDirectory);
return NuGetHelpers.Pack(NuspecFilePath, OutputDirectory);
}
}
}

0 comments on commit 3520f7f

Please sign in to comment.