Created
July 15, 2021 17:42
-
-
Save JustinGrote/97851bbf4cac963ac01d741683520b09 to your computer and use it in GitHub Desktop.
Use Bicep to define a template spec
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#requires -version 7 | |
function Set-AzBicepTemplateSpec { | |
[CmdletBinding()] | |
param( | |
#Template spec Resource ID to set | |
[Alias('Id')][Parameter(ValueFromPipeline,ValueFromPipelineByPropertyName)]$ResourceId, | |
#Path to the bicep file | |
[Parameter(Mandatory)][String]$Path, | |
#Optional Path to the UI Definition | |
[String]$UIDefinitionPath = $($Path -replace '.bicep$','.ui.json'), | |
[Parameter(Mandatory)][String]$Version, | |
[String]$VersionDescription | |
) | |
begin { | |
$ErrorActionPreference = 'stop' | |
$bicepCmd = (Get-Command 'bicep' -ErrorAction SilentlyContinue) ?? (Write-Error 'bicep cli not found, ensure it is in your path') | |
} | |
process { | |
$tempPath = "TEMP:bicep-$(new-GUID).json" | |
$tempFile = New-Item $tempPath | |
try { | |
& $bicepCmd build $Path --outfile $tempFile | |
$templateFile = Get-Item $tempPath | |
$setTemplateSpecParams = @{ | |
TemplateFile = $templateFile | |
ResourceId = $ResourceId | |
Version = $Version | |
} | |
if ($VersionDescription) {$setTemplateSpecParams.VersionDescription = $VersionDescription} | |
if ($UIDefinitionPath) {$setTemplateSpecParams.UIFormDefinitionFile = $UIDefinitionPath} | |
Set-AzTemplateSpec @setTemplateSpecParams | |
} catch {throw} finally { | |
Remove-Item $tempFile | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment