-
Notifications
You must be signed in to change notification settings - Fork 453
/
Copy pathPublish-ModuleToPrivateBicepRegistry.ps1
106 lines (83 loc) · 4.2 KB
/
Publish-ModuleToPrivateBicepRegistry.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<#
.SYNOPSIS
Publish a new version of a given module to a private bicep registry
.DESCRIPTION
Publish a new version of a given module to a private bicep registry
.PARAMETER TemplateFilePath
Mandatory. Path to the module deployment file from root.
Example: 'C:\modules\key-vault\vault\main.bicep'
.PARAMETER ModuleVersion
Mandatory. Version of the module to publish, following SemVer convention.
Example: '1.0.0', '2.1.5-alpha.1', '0.0.5-beta.1'
.PARAMETER BicepRegistryName
Mandatory. Name of the private bicep registry to publish to.
Example: 'adpsxxazacrx001'
.PARAMETER BicepRegistryRgName
Mandatory. ResourceGroup of the private bicep registry to publish to.
Example: 'artifacts-rg'
.PARAMETER BicepRegistryRgLocation
Optional. The location of the resourceGroup the private bicep registry is deployed to. Required if the resource group is not yet existing.
Example: 'West Europe'
.PARAMETER UseApiSpecsAlignedName
Optional. If set to true, the module will be published with a name that is aligned with the Azure API naming. If not, one aligned with the module's folder path. See the following examples:
- True: bicep/modules/microsoft.keyvault.vaults.secrets
- False: bicep/modules/key-vault.vault.secret
.EXAMPLE
Publish-ModuleToPrivateBicepRegistry -TemplateFilePath 'C:\modules\key-vault\vault\main.bicep' -ModuleVersion '3.0.0-alpha' -BicepRegistryName 'adpsxxazacrx001' -BicepRegistryRgName 'artifacts-rg'
Try to publish the KeyVault module with version 3.0.0-alpha to a private bicep registry called 'adpsxxazacrx001' in resource group 'artifacts-rg'.
#>
function Publish-ModuleToPrivateBicepRegistry {
[CmdletBinding(SupportsShouldProcess)]
param (
[Parameter(Mandatory)]
[string] $TemplateFilePath,
[Parameter(Mandatory)]
[string] $ModuleVersion,
[Parameter(Mandatory)]
[string] $BicepRegistryName,
[Parameter(Mandatory)]
[string] $BicepRegistryRgName,
[Parameter(Mandatory = $false)]
[string] $BicepRegistryRgLocation,
[Parameter(Mandatory = $false)]
[bool] $UseApiSpecsAlignedName = $false
)
begin {
Write-Debug ('{0} entered' -f $MyInvocation.MyCommand)
# Load used functions
. (Join-Path $PSScriptRoot 'Get-PrivateRegistryRepositoryName.ps1')
}
process {
#############################
## EVALUATE RESOURCES ##
#############################
if ((Split-Path $TemplateFilePath -Extension) -ne '.bicep') {
throw "The template in path [$TemplateFilePath] is no bicep template."
}
# Resource Group
if (-not (Get-AzResourceGroup -Name $BicepRegistryRgName -ErrorAction 'SilentlyContinue')) {
if ($PSCmdlet.ShouldProcess("Resource group [$BicepRegistryRgName] to location [$BicepRegistryRgLocation]", 'Deploy')) {
New-AzResourceGroup -Name $BicepRegistryRgName -Location $BicepRegistryRgLocation
}
}
# Registry
if (-not (Get-AzContainerRegistry -ResourceGroupName $BicepRegistryRgName -Name $BicepRegistryName -ErrorAction 'SilentlyContinue')) {
if ($PSCmdlet.ShouldProcess("Container Registry [$BicepRegistryName] to resource group [$BicepRegistryRgName]", 'Deploy')) {
New-AzContainerRegistry -ResourceGroupName $BicepRegistryRgName -Name $BicepRegistryName -Sku 'Basic'
}
}
# Get a valid Container Registry name
$moduleRegistryIdentifier = Get-PrivateRegistryRepositoryName -TemplateFilePath $TemplateFilePath -UseApiSpecsAlignedName $UseApiSpecsAlignedName
#############################################
## Publish to private bicep registry ##
#############################################
$publishingTarget = 'br:{0}.azurecr.io/{1}:{2}' -f $BicepRegistryName, $moduleRegistryIdentifier, $ModuleVersion
if ($PSCmdlet.ShouldProcess("Private bicep registry entry [$moduleRegistryIdentifier] version [$ModuleVersion] to registry [$BicepRegistryName]", 'Publish')) {
bicep publish $TemplateFilePath --target $publishingTarget --force
}
Write-Verbose 'Publish complete'
}
end {
Write-Debug ('{0} exited' -f $MyInvocation.MyCommand)
}
}