Skip to content

Commit e350617

Browse files
Experimental bicep deploy, bicep what-if and bicep teardown commands (#18041)
Changes: * New experimental feature flag `deployCommands` - all new functionality requires this to be enabled * New CLI command `bicep deploy <path_to_params_file>` - runs a deployment or deployment stack, and displays progress * New CLI command `bicep what-if <path_to_params_file>` - runs a what-if for a deployment * New CLI command `bicep teardown <path_to_params_file>` - deletes a deployment stack * Optional `with` syntax clause on a `using` statement in a `.bicepparam` file. * Can either be used with deployments: ```bicep using 'main.bicep' with { mode: 'deployment' scope: '/subscriptions/56db6eae-3609-4194-aedc-f919a5ffc162/resourceGroups/test-rg' } ``` * Or with deployment stacks: ```bicep using 'main.bicep' with { mode: 'stack' scope: '/subscriptions/56db6eae-3609-4194-aedc-f919a5ffc162/resourceGroups/test-rg' actionOnUnmanage: { resources: 'delete' } denySettings: { mode: 'denyDelete' } } ``` * Ability to read an environment variable at deploy time using `externalInput('sys.envVar', '<name_of_var>')`. For example: ```sh export MY_ENV=foo bicep deploy main.bicepparam ``` * Ability to read a custom CLI argument at deploy time using `externalInput('sys.cliArg', '<name_of_arg>')`. For example: ```sh bicep deploy main.bicepparam --arg-my-env foo ``` See [Using the Deploy Commands](https://github.com/Azure/bicep/blob/ant/poc_cli/docs/experimental/deploy-commands.md) for additional information and code samples. Related to #17949 - note that I haven't yet gone over the feedback and addressed the points raised there. https://github.com/user-attachments/assets/da64f3a3-3c11-411b-94b6-6324f79af107 ###### Microsoft Reviewers: [Open in CodeFlow](https://microsoft.github.io/open-pr/?codeflow=https://github.com/Azure/bicep/pull/18041)
1 parent da4d176 commit e350617

File tree

118 files changed

+3212
-200
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

118 files changed

+3212
-200
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ jobs:
746746
name: Check secret access
747747
runs-on: ubuntu-latest
748748
outputs:
749-
access_verified: ${{ steps.check-access.outputs.verified && !(github.event_name == 'workflow_dispatch' && github.ref != 'refs/head/main') }}
749+
access_verified: ${{ steps.check-access.outputs.verified && (github.base_ref == 'refs/head/main' || github.ref == 'refs/head/main') }}
750750

751751
steps:
752752
- id: check-access

docs/experimental-features.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ The following features can be optionally enabled through your `bicepconfig.json`
1313

1414
Should be enabled in tandem with `testFramework` experimental feature flag for expected functionality. Allows you to author boolean assertions using the `assert` keyword comparing the actual value of a parameter, variable, or resource name to an expected value. Assert statements can only be written directly within the Bicep file whose resources they reference. For more information, see [Bicep Experimental Test Framework](https://github.com/Azure/bicep/issues/11967).
1515

16+
### `deployCommands`
17+
18+
Enables `deploy`, `what-if` and `teardown` command groups, as well as the `with` syntax in a `.bicepparam` file. For more information, see [Using the Deploy Commands](./experimental/deploy-commands.md).
19+
1620
### `desiredStateConfiguration`
1721

1822
Allows you to author configuration documents for [Microsoft's Desired State Configuration platform](https://github.com/PowerShell/DSC) using `targetScope = 'desiredStateConfiguration'`. If enabled, the file must only contain DSC resource instances. The built file is a valid configuration document to be used with the CLI. For example, `dsc.exe config test --file example.json`. This feature is in early development.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"experimentalFeaturesEnabled": {
3+
"deployCommands": true
4+
}
5+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
param name string
2+
3+
@description('Specify which type of dev environment to deploy')
4+
@allowed(['AzureCLI', 'AzurePowerShell'])
5+
param type string = 'AzureCLI'
6+
7+
@description('Use to override the version to use for Azure CLI or AzurePowerShell')
8+
param toolVersion string = ''
9+
10+
@description('This is the path in the container instance where it\'s mounted to the file share.')
11+
param mountPath string = '/mnt/azscripts/azscriptinput'
12+
13+
@description('Time in second before the container instance is suspended')
14+
param sessionTime string = '1800'
15+
16+
param fileShareName string
17+
param storageName string
18+
param storageId string
19+
param location string = resourceGroup().location
20+
21+
// Specifies which version to use if no specific toolVersion is provided (Azure CLI latest or Azure PowerShell 5.6)
22+
var version = (type == 'AzureCLI' && toolVersion == ''
23+
? 'latest'
24+
: type == 'AzurePowerShell' && toolVersion == '' ? '5.6' : toolVersion)
25+
26+
var azcliImage = 'mcr.microsoft.com/azure-cli:${version}'
27+
var azpwshImage = 'mcr.microsoft.com/azuredeploymentscripts-powershell:az${version}'
28+
29+
var azpwshCommand = ['/bin/sh', '-c', 'pwsh -c \'Start-Sleep -Seconds ${sessionTime}\'']
30+
31+
var azcliCommand = ['/bin/bash', '-c', 'echo hello; sleep ${sessionTime}']
32+
33+
resource containerGroupName 'Microsoft.ContainerInstance/containerGroups@2019-12-01' = {
34+
name: name
35+
location: location
36+
properties: {
37+
containers: [
38+
{
39+
name: '${name}cg'
40+
properties: {
41+
image: type == 'AzureCLI' ? azcliImage : type == 'AzurePowerShell' ? azpwshImage : ''
42+
resources: {
43+
requests: {
44+
cpu: 1
45+
memoryInGB: 2
46+
}
47+
}
48+
ports: [
49+
{
50+
protocol: 'TCP'
51+
port: 80
52+
}
53+
]
54+
volumeMounts: [
55+
{
56+
name: 'filesharevolume'
57+
mountPath: mountPath
58+
}
59+
]
60+
command: type == 'AzureCLI' ? azcliCommand : type == 'AzurePowerShell' ? azpwshCommand : null
61+
}
62+
}
63+
]
64+
osType: 'Linux'
65+
volumes: [
66+
{
67+
name: 'filesharevolume'
68+
azureFile: {
69+
readOnly: false
70+
shareName: fileShareName
71+
storageAccountName: storageName
72+
storageAccountKey: listKeys(storageId, '2019-06-01').keys[0].value
73+
}
74+
}
75+
]
76+
}
77+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var subscriptionId = readEnvVar('AZURE_SUBSCRIPTION_ID')
2+
var resourceGroup = readEnvVar('AZURE_RESOURCE_GROUP')
3+
4+
using 'main.bicep' with {
5+
mode: 'deployment'
6+
scope: '/subscriptions/${subscriptionId}/resourceGroups/${resourceGroup}'
7+
}
8+
9+
param containerName = 'foo'
10+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
param storageName string = toLower('${take('deployscript${uniqueString(resourceGroup().id)}', 22)}st')
2+
param containerName string = toLower('${take('deployscript${uniqueString(resourceGroup().id)}', 22)}ci')
3+
4+
@description('Specify which type of dev environment to deploy')
5+
@allowed(['AzureCLI', 'AzurePowerShell'])
6+
param type string = 'AzureCLI'
7+
8+
@description('Use to specify the version to use for Azure CLI or AzurePowerShell, if no version is specified latest will be used for AzCLI and 5.6 for AzPwsh')
9+
param toolVersion string = ''
10+
11+
param location string = resourceGroup().location
12+
13+
module storage './storage.bicep' = {
14+
params: {
15+
name: storageName
16+
location: location
17+
}
18+
}
19+
20+
module container './containergroups.bicep' = {
21+
params: {
22+
name: containerName
23+
location: location
24+
storageName: storage.outputs.storageName
25+
storageId: storage.outputs.resourceId
26+
fileShareName: storage.outputs.fileShareName
27+
type: type
28+
toolVersion: toolVersion
29+
}
30+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var subscriptionId = readCliArg('subscription-id')
2+
var resourceGroup = readCliArg('resource-group')
3+
4+
using 'main.bicep' with {
5+
mode: 'deployment'
6+
// TODO improve on this
7+
scope: '/subscriptions/${subscriptionId}/resourceGroups/${resourceGroup}'
8+
}
9+
10+
param containerName = 'foo'
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var subscriptionId = readEnvVar('AZURE_SUBSCRIPTION_ID')
2+
var resourceGroup = readEnvVar('AZURE_RESOURCE_GROUP')
3+
4+
using 'main.bicep' with {
5+
mode: 'stack'
6+
scope: '/subscriptions/${subscriptionId}/resourceGroups/${resourceGroup}'
7+
actionOnUnmanage: {
8+
resources: 'delete'
9+
}
10+
denySettings: {
11+
mode: 'denyDelete'
12+
}
13+
}
14+
15+
param containerName = 'foo'
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
param name string
2+
3+
@allowed([
4+
'Standard_LRS'
5+
'Standard_GRS'
6+
'Standard_RAGRS'
7+
'Standard_ZRS'
8+
'Premium_LRS'
9+
'Premium_ZRS'
10+
'Standard_GZRS'
11+
'Standard_RAGZRS'
12+
])
13+
param sku string = 'Standard_LRS'
14+
15+
@allowed(['Storage', 'StorageV2', 'BlobStorage', 'FileStorage', 'BlockBlobStorage'])
16+
param kind string = 'StorageV2'
17+
18+
@allowed(['Hot', 'Cool'])
19+
param accessTier string = 'Hot'
20+
param fileShareName string = 'deployscript'
21+
param location string = resourceGroup().id
22+
23+
resource storage 'Microsoft.Storage/storageAccounts@2019-06-01' = {
24+
name: name
25+
location: location
26+
sku: {
27+
name: sku
28+
}
29+
kind: kind
30+
properties: {
31+
accessTier: accessTier
32+
}
33+
}
34+
35+
resource fileshare 'Microsoft.Storage/storageAccounts/fileServices/shares@2019-06-01' = {
36+
name: '${storage.name}/default/${fileShareName}'
37+
}
38+
39+
output resourceId string = storage.id
40+
output storageName string = storage.name
41+
output fileShareName string = fileShareName
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Using the Deploy Commands
2+
3+
## Goals
4+
5+
1. Allow users to model all properties of the deployment (including scope, stacks configuration) in one place
6+
2. Add support for "teardown" functionality for cleaning up a Stack resource
7+
8+
## Demo
9+
10+
https://github.com/user-attachments/assets/da64f3a3-3c11-411b-94b6-6324f79af107
11+
12+
## Running Samples
13+
14+
### Script
15+
16+
Pre-reqs:
17+
1. Download samples [here](https://download-directory.github.io/?url=https%3A%2F%2Fgithub.com%2FAzure%2Fbicep%2Ftree%2Fmain%2Fdocs%2Fexperimental%2Fdeploy-commands-samples)
18+
1. Unzip
19+
1. Cd to the unzipped directory
20+
21+
#### CLI args
22+
```sh
23+
# what-if
24+
bicep what-if --arg-subscription-id d08e1a72-8180-4ed3-8125-9dff7376b0bd --arg-resource-group ant-test script/main.bicepparam
25+
26+
# deploy
27+
bicep deploy --arg-subscription-id d08e1a72-8180-4ed3-8125-9dff7376b0bd --arg-resource-group ant-test script/main.bicepparam
28+
```
29+
30+
#### Env vars
31+
Linux/Mac
32+
```sh
33+
export AZURE_SUBSCRIPTION_ID=d08e1a72-8180-4ed3-8125-9dff7376b0bd
34+
export AZURE_RESOURCE_GROUP=ant-test
35+
36+
# what-if
37+
bicep what-if script/env_vars.bicepparam
38+
39+
# deploy
40+
bicep deploy script/env_vars.bicepparam
41+
```
42+
43+
Windows
44+
```powershell
45+
$env:AZURE_SUBSCRIPTION_ID = "d08e1a72-8180-4ed3-8125-9dff7376b0bd"
46+
$env:AZURE_RESOURCE_GROUP = "ant-test"
47+
48+
# what-if
49+
bicep what-if script/env_vars.bicepparam
50+
51+
# deploy
52+
bicep deploy script/env_vars.bicepparam
53+
```
54+
55+
#### Stacks
56+
Linux/Mac
57+
```sh
58+
export AZURE_SUBSCRIPTION_ID=d08e1a72-8180-4ed3-8125-9dff7376b0bd
59+
export AZURE_RESOURCE_GROUP=ant-test
60+
61+
# what-if not currently supported
62+
63+
# deploy
64+
bicep deploy script/stack.bicepparam
65+
66+
# teardown
67+
bicep teardown script/stack.bicepparam
68+
```
69+
70+
Windows
71+
```powershell
72+
$env:AZURE_SUBSCRIPTION_ID = "d08e1a72-8180-4ed3-8125-9dff7376b0bd"
73+
$env:AZURE_RESOURCE_GROUP = "ant-test"
74+
75+
# what-if not currently supported
76+
77+
# deploy
78+
bicep deploy script/stack.bicepparam
79+
80+
# teardown
81+
bicep teardown script/stack.bicepparam
82+
```

0 commit comments

Comments
 (0)