Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Bicep CLI directly without installing binaries #1903

Merged
merged 4 commits into from Apr 9, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
102 changes: 90 additions & 12 deletions docs/cicd-with-bicep.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ As your Bicep practice matures, you will want to check-in your Bicep code into s
1. Build your Bicep file into an ARM Template
1. Deploy the generated ARM template

In order to do this, we need to make sure the Bicep CLI is installed on the build agent. For now, Bicep is not preinstalled on any build agents or tasks provided by Microsoft, but installing it manually as part of the pipeline is straightforward.
With the current Azure CLI 2.20 now installed in GitHub and also on Azure DevOps, Bicep CLI can be automatically triggerd by using `az bicep build` command and an explicit task to download Bicep CLI is no more needed.

The following example is designed to be run in GitHub actions workflow and uses Azure CLI, but could be easily adapted to run in a Azure DevOps Pipeline. It assumes the following prerequisite:
The two examples below illustrates this. It assumes the following prerequisite:

* The Bicep file you want to transpile and deploy is called `main.bicep` and exists in the root of the repo
* The parameters file you want to use is called `parameters.json` and exists in the root of the repo
* You are deploying the transpiled ARM Template to a resource group. Deploying to another scope like a subscription requires a different CLI command.

## GitHub Workflow

```yaml

name: bicep build and deploy
Expand All @@ -32,18 +35,10 @@ jobs:
- name: Checkout code
uses: actions/checkout@v2

# Install the latest release of the bicep CLI binaries
- name: Install bicep CLI Binaries
run: |
curl -Lo bicep.bin https://github.com/Azure/bicep/releases/latest/download/bicep-linux-x64
chmod +x ./bicep.bin
sudo mv ./bicep.bin /usr/local/bin/bicep
bicep --help

# Transpile bicep file into ARM template
- name: Build ARM Template from bicep file
run: |
bicep build ./main.bicep
az bicep build --files ./main.bicep

# Stop here if you only want to do "CI" which just generates the
# build artifact (ARM Template JSON)
Expand Down Expand Up @@ -73,5 +68,88 @@ jobs:
az account show
az deployment group create -f ./main.json -g ${{ env.AZURE_RESOURCE_GROUP }}
```
## Azure DevOps Pipeline

```yaml
trigger:
- main
name: 'bicep build and deploy'

variables:
vmImageName: 'ubuntu-latest'
workingDirectory: '$(System.DefaultWorkingDirectory)/'
geoLocation: 'West Europe'

Instead of installing the Bicep CLI manually, you may instead want to use the [community-maintained github action](https://github.com/marketplace/actions/bicep-build) from [@justinyoo](https://github.com/justinyoo) that can run `bicep build` on your behalf.
azureServiceConnection: 'My-Azure-DevOps-ServicePrincipalName'
subscriptionId: 'My-Subscription-Id'
AZURE_RESOURCE_GROUP: 'myResourceGroupName'

stages:
- stage: Build
displayName: Build

jobs:
- job: Build
displayName: Validate and Publish
pool:
vmImage: $(vmImageName)

steps:
- task: AzureCLI@2
displayName: Build ARM Template from bicep file
inputs:
azureSubscription: '$(azureServiceConnection)'
scriptType: bash
scriptLocation: inlineScript
inlineScript: |
az --version
az bicep build --files ./main.bicep
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think you need to first call ‘az bicep install’ first.

Copy link
Collaborator

Choose a reason for hiding this comment

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

you actually don't have to (though you can). If you attempt to create a deployment with a .bicep file and bicep is not installed, we will install it automatically

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ok, right. I just tested and it does not work with az bicep version but it does with build. Apologies for mixup.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct:
using az bicep build will download the CLI if needed, and will then transpile it if needed. (See screenshot) below. So no need for the curl and extension hassle.
Although you still need to transpile to JSON to actually deploy this.

image


- task: AzureResourceManagerTemplateDeployment@3
displayName: 'Validate APIM Templates'
inputs:
azureResourceManagerConnection: '$(azureServiceConnection)'
subscriptionId: '$(subscriptionId)'
resourceGroupName: '$(AZURE_RESOURCE_GROUP)'
location: '$(geoLocation)'
csmFile: main.json
csmParametersFile: parameters.json
deploymentMode: Validation

- task: CopyFiles@2
displayName: 'Copy Templates'
inputs:
SourceFolder: bicep
TargetFolder: '$(build.artifactstagingdirectory)'

- task: PublishBuildArtifacts@1
displayName: 'Publish Artifact: drop'
inputs:
PathtoPublish: '$(build.artifactstagingdirectory)'
ArtifactName: 'drop'

- stage: Development
displayName: Deploy to Development
dependsOn: Build
condition: succeeded()
jobs:
- deployment: Deploy
displayName: 'Deploying APIM Template'
environment: 'Development'
pool:
vmImage: $(vmImageName)
strategy:
runOnce:
deploy:
steps:
- task: AzureResourceManagerTemplateDeployment@3
displayName: 'Deploy/Update APIM (Dev)'
inputs:
azureResourceManagerConnection: '$(azureServiceConnection)'
subscriptionId: '$(subscriptionId)'
resourceGroupName: '$(AZURE_RESOURCE_GROUP)'
location: '$(geoLocation)'
csmFile: '$(Pipeline.Workspace)/drop/main.json'
csmParametersFile: '$(Pipeline.Workspace)/drop/parameters.json'
deploymentMode: 'Incremental'
```
6 changes: 3 additions & 3 deletions docs/installing.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ All of the following methods will install the Bicep CLI and add it to your PATH.
#### Linux
```sh
# Fetch the latest Bicep CLI binary
curl -Lo bicep https://github.com/Azure/bicep/releases/latest/download/bicep-linux-x64
curl -Lo bicep.bin https://github.com/Azure/bicep/releases/latest/download/bicep-linux-x64
# Mark it as executable
chmod +x ./bicep
chmod +x ./bicep.bin
# Add bicep to your PATH (requires admin)
sudo mv ./bicep /usr/local/bin/bicep
sudo mv ./bicep.bin /usr/local/bin/bicep
# Verify you can now access the 'bicep' command
bicep --help
# Done!
Expand Down