Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 41 additions & 5 deletions source/Calamari.AzureResourceGroup/DeployBicepTemplateBehaviour.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;
using System.Threading.Tasks;
using Azure;
using Azure.ResourceManager;
Expand All @@ -7,8 +8,11 @@
using Calamari.Azure;
using Calamari.CloudAccounts;
using Calamari.Common.Commands;
using Calamari.Common.Features.Packages;
using Calamari.Common.Features.Processes;
using Calamari.Common.Plumbing.Deployment;
using Calamari.Common.Plumbing.Extensions;
using Calamari.Common.Plumbing.FileSystem;
using Calamari.Common.Plumbing.Logging;
using Calamari.Common.Plumbing.Pipeline;
using Calamari.Common.Plumbing.Variables;
Expand All @@ -21,12 +25,16 @@ class DeployBicepTemplateBehaviour : IDeployBehaviour
readonly TemplateService templateService;
readonly AzureResourceGroupOperator resourceGroupOperator;
readonly ILog log;
readonly ICalamariFileSystem fileSystem;
readonly IExtractPackage extractPackage;

public DeployBicepTemplateBehaviour(ICommandLineRunner commandLineRunner, TemplateService templateService, AzureResourceGroupOperator resourceGroupOperator, ILog log)
public DeployBicepTemplateBehaviour(ICommandLineRunner commandLineRunner, TemplateService templateService, AzureResourceGroupOperator resourceGroupOperator, ICalamariFileSystem fileSystem, IExtractPackage extractPackage, ILog log)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a ExtractPackage virtual method on the DeployAzureBicepTemplateCommand base class.
Is it possible to perform the package extraction for the bicep step in an implementation of this method, leaving the deployment behaviour to be agnostic about packages and extraction?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah thanks for this, I didn't realise this infrastructure was there. In the end I figured out a way to make this work without changing any Calamari code at all. Server changes are here: https://github.com/OctopusDeploy/OctopusDeploy/pull/40299

{
this.commandLineRunner = commandLineRunner;
this.templateService = templateService;
this.resourceGroupOperator = resourceGroupOperator;
this.fileSystem = fileSystem;
this.extractPackage = extractPackage;
this.log = log;
}

Expand Down Expand Up @@ -90,21 +98,49 @@ async Task<ResourceGroupResource> GetOrCreateResourceGroup(ArmClient armClient,
var bicepTemplateFile = context.Variables.Get(SpecialVariables.Action.Azure.BicepTemplateFile, "template.bicep");
var templateSource = context.Variables.Get(SpecialVariables.Action.Azure.TemplateSource, string.Empty);

var filesInPackageOrRepository = templateSource is "Package" or "GitRepository";
if (filesInPackageOrRepository)
switch (templateSource)
{
bicepTemplateFile = context.Variables.Get(SpecialVariables.Action.Azure.BicepTemplate);
case "Package":
{
var extractionPath = ExtractPackage(context);

bicepTemplateFile = Path.Combine(extractionPath, context.Variables.Get(SpecialVariables.Action.Azure.BicepTemplate)!);
break;
}
case "GitRepository":
bicepTemplateFile = context.Variables.Get(SpecialVariables.Action.Azure.BicepTemplate);
break;
}

log.Info($"Processing Bicep file: {bicepTemplateFile}");
var armTemplateFile = bicepCli.BuildArmTemplate(bicepTemplateFile!);
log.Info("Bicep file processed");

var template = templateService.GetSubstitutedTemplateContent(armTemplateFile, filesInPackageOrRepository, context.Variables);
var template = templateService.GetSubstitutedTemplateContent(armTemplateFile, templateSource is "GitRepository", context.Variables);

var parameters = templateService.GetSubstitutedTemplateContent("parameters.json", inPackage: false, context.Variables);

return (template, parameters);
}

string ExtractPackage(RunningDeployment context)
{
var packageId = context.Variables.Get(SpecialVariables.Action.Azure.PackageId)!;
var originalFullPath = Path.GetFullPath(context.Variables.Get(PackageVariables.IndexedOriginalPath(packageId))!);
var sanitizedReferenceName = fileSystem.RemoveInvalidFileNameChars(packageId);
var extractionPath = Path.Combine(context.CurrentDirectory, sanitizedReferenceName);
ExtractDependency(originalFullPath, extractionPath);
return extractionPath;
}

void ExtractDependency(string file, string extractionPath)
{
Log.Info($"Extracting dependency '{file}' to '{extractionPath}'");

if (!File.Exists(file))
throw new CommandException("Could not find dependency file: " + file);

extractPackage.ExtractToCustomDirectory(new PathToPackage(file), extractionPath);
}
}
}
1 change: 1 addition & 0 deletions source/Calamari.AzureResourceGroup/SpecialVariables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public static class Azure
public static readonly string ArmDeploymentTimeout = "Octopus.Action.Azure.ArmDeploymentTimeout";
public static readonly string BicepTemplate = "Octopus.Action.Azure.BicepTemplate";
public static readonly string BicepTemplateFile = "Octopus.Action.Azure.BicepTemplateFile";
public static readonly string PackageId = "Octopus.Action.Package.PackageId";
}
}
}
Expand Down