Deploy Bicep with GitHub Actions
GitHub Actions is a suite of features in GitHub to automate your software development workflows.
Use the GitHub Action to automatically deploy a Bicep file to Azure.
Workflow file overview
A Action is defined by a YAML (.yml) file in the /.github/workflows/
path in your github repository. This definition holds all the steps and parameters that make up the workflow.
The file YAML file that we will be creating has two sections:
Authentication :
1. Create a service principal on Azure
2. Store it as Secret into Github Secret
Deployment :
- Create a file with Bicep file
- Push the file to your github repository
Create Service Principle for deployment credentials
Create a resource group by using the following command in Azure CLI
az group create -n {AzwayGroup} -l {location}
Replace Your_App_Name
with the name of your new application and replace AzwayGroup and trailing resourceGroups if you want to provide access on the Subscription directly
az ad sp create-for-rbac --name Your_App_Name --role contributor --scopes /subscriptions/xxxx-xxxx-xxxx-xxx/resourceGroups/{AzwayGroup} --sdk-auth
Configure the GitHub secrets
You need to create secrets for your Azure credentials, resource group, and subscriptions.
- Visit your repository on Github.
- Click settings Settings
- Click Secrets
- Click New secret.
- Paste the JSON output from the Azure CLI with { } into the secret’s value field.
- Name the secret
AZURE_Login
. - Create another secret named
AZURE_RG
. - Add the name of your resource group to the secret’s value field
- Create another secret named
AZURE_SUBSCRIPTION
. - Add your subscription ID to the secret’s value field (example:
34fasda9d-4c61-497d-99ba-112esx4rgh6
).
Add a Bicep file
Add a Bicep file to your GitHub repository. The following Bicep file creates a storage account
/ Input parameters must be specified by the module consumer
param publicIpResourceName string ='samplepip'
param publicIpDnsLabel string = '${publicIpResourceName}-${newGuid()}'
param location string = resourceGroup().location
param dynamicAllocation bool = True
resource publicIp 'Microsoft.Network/publicIPAddresses@2020-06-01' = {
name: publicIpResourceName
location: location
properties: {
publicIPAllocationMethod: dynamicAllocation ? 'Dynamic' : 'Static'
dnsSettings: {
domainNameLabel: publicIpDnsLabel
}
}
}
// Set an output which can be accessed by the module consumer
output ipFqdn string = publicIp.properties.dnsSettings.fqdn
You can put the file anywhere in the repository. The workflow sample in the next section assumes the Bicep file is named azuredeploy.bicep, and it’s stored at the root of your repository.
Create workflow
The workflow file must be stored in the .github/workflows folder at the root of your repository. The workflow file extension can be either .yml or .yaml.
- From your GitHub repository, select Actions from the top menu.
- Select New workflow.
- Select set up a workflow yourself.
- Rename the workflow file if you prefer a different name other than main.yml. For example: deploy.yml.
- Replace the content of the yml file with the following
on: [push]
name: Azure ARM
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
# Checkout code
- uses: actions/checkout@main
# Log into Azure
- uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_Login }}
# Deploy Bicep file
- name: deploy
uses: azure/arm-deploy@v1
with:
subscriptionId: ${{ secrets.AZURE_SUBSCRIPTION }}
resourceGroupName: ${{ secrets.AZURE_RG }}
template: ./azuredeploy.bicep
failOnStdErr: false
The workflow starts right after you commit the changes.
Check workflow status
- Select the Actions tab. You’ll see a Create deploy.yml workflow listed. It takes 1-2 minutes to run the workflow.
- Select the workflow to open it.
- Select Run ARM deploy from the menu to verify the deployment.