'Publish Multiple Bicep Templates to a Container Registry

We are in the process of transitioning our infrastructure from using ARM templates to Bicep templates. We have a dedicated repository with all of our template files which we wish to publish to a central repository to be used by other repos in our organization.

Previously with ARM templates, we published the folder that contained all of our templates to an Azure Storage account, that could then be referenced by others repo using the template blob url with a SAS token. We are looking to do something with bicep templates so we do not need to publish each one individually. Currently the az cli and powershell command only contain the ability to publish one file at a time using the --file argument:

az bicep publish --file storage.bicep --target br:exampleregistry.azurecr.io/bicep/modules/storage:v1

The only possibility I see is using a foreach statement in powershell that iterates through each file in the folder and publishes individually:

foreach ($file in Get-ChildItem)
{
  az bicep publish --file $file.name --target br:exampleregistry.azurecr.io/bicep/modules/$filename:$version
}

Question:

Has anyone come up with a more optimized way in which to publish multiple bicep templates in a single operation?



Solution 1:[1]

  • AFAIK the way you did seems to be the way to publish multiple bicep templates to acr which iterates through each bicep file.
  • You can also check this Automate Maintaining a Private Bicep Module Registry with Azure Pipelines where already published ones are compared to the bicep files in folder and only non existing ones are published to the registry every time.
  • Here the ACR is used to create a private Bicep registry for sharing modules and a build pipeline is used to publish modules into the ACR when new modules are added or existing ones are modified or changed.

Solution 2:[2]

I had some trouble to make it work with the foreach loop. The following code worked for me:

azure-pipelines.yml:

jobs:
  - job: modules
    displayName: 'Publish Bicep Modules'
  pool:
    name: 'myBuildingPoolName'

steps:
  - task: AzureCLI@2
    displayName: 'Publish/Update Modules to Registry'
    inputs:
      azureSubscription: $(ServiceConnectionName) # Pipeline paramater
      scriptType: 'pscore'
      scriptLocation: inlineScript
      inlineScript: |
        az bicep install
        $registryName = '$(RegistryName)' # Pipeline paramater
        $version = '$(Version)' # Pipeline paramater
        
        # bicep files are in the modules folder
        $modules = Get-ChildItem -Path ./Modules/*.bicep -Recurse -Include *.bicep
        foreach ($module in $modules){
            $moduleName = $module.BaseName.ToLower()
            Write-Host "Adding new module ${moduleName} with version $version"
            az bicep publish --file $module.FullName --target br:${registryName}.azurecr.io/bicep/modules/${moduleName}:${version}
        }

Also make sure you have Azure CLI & Powershell installed in case you use a self hosted docker AgentPool:

Dockerfile:

#Install Azure-CLI
RUN curl -LsS https://aka.ms/InstallAzureCLIDeb | bash \
&& rm -rf /var/lib/apt/lists/*

#Install Powershell
RUN wget -q https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb
RUN dpkg -i packages-microsoft-prod.deb
RUN apt-get update
RUN add-apt-repository universe
RUN apt-get install -y powershell

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kavyasaraboju-MT
Solution 2