Hands-on Demo – Deploy a Bicep file from GitHub Workflows
In this demo, we will show how to deploy an Azure resource using a Bicep file via GitHub Actions. GitHub Actions allow you to automate your workflows, and in this case, we will set up a CI/CD pipeline to deploy an Azure resource using a Bicep template from a GitHub repository.
Steps Overview
Create the Bicep file in your GitHub repository.
Set up the Azure Service Principal for authentication.
Create a GitHub Actions workflow file (
main.yml) for deploying the Bicep file to Azure.Deploy the Bicep file via the GitHub Actions pipeline.
1. Create the Bicep File in GitHub Repository
First, create a Bicep file in your GitHub repository.
For this example, we’ll create a simple Bicep template that deploys an Azure Virtual Network and Subnet.
Example: `main.bicep
xxxxxxxxxx301// Parameters2param location string = 'East US'3param vnetName string = 'myVNet'4param subnetName string = 'mySubnet'5// Variables6var addressPrefix = '10.0.0.0/16'7var subnetPrefix = '10.0.1.0/24'8// Resource - Virtual Network9resource vnet 'Microsoft.Network/virtualNetworks@2020-05-01' = {10 name: vnetName11 location: location12 properties: {13 addressSpace: {14 addressPrefixes: [15 addressPrefix16 ]17 }18 }19}20// Resource - Subnet21resource subnet 'Microsoft.Network/virtualNetworks/subnets@2020-05-01' = {22 name: '${vnetName}/${subnetName}'23 parent: vnet24 properties: {25 addressPrefix: subnetPrefix26 }27}28// Outputs29output vnetId string = vnet.id30output subnetId string = subnet.idEnsure this Bicep file is committed to your GitHub repository.
Let’s now set up the GitHub Actions pipeline to deploy this template.
2. Set Up the Azure Service Principal for Authentication
To deploy resources to Azure from GitHub Actions, you’ll need to authenticate using an Azure Service Principal (SP). The service principal allows GitHub Actions to interact with Azure resources securely.
Follow below given steps to create an Azure Service Principal.
Log in to Azure CLI:
xxxxxxxxxx11az loginCreate the Service Principal and assign it a role (e.g., Contributor):
xxxxxxxxxx41az ad sp create-for-rbac \2--name "GitHubActionsSP" \3--role Contributor \4--scopes /subscriptions/{subscription-id}/resourceGroups/{resource-group}This will output the following:
appId: Theclient IDfor the service principal.password: Theclient secret(you will need this).tenant: The tenant ID for your Azure Active Directory.
Save the following information (you’ll need it for the GitHub secrets):
AZURE_CLIENT_ID:appIdfrom the output.AZURE_CLIENT_SECRET:passwordfrom the output.AZURE_TENANT_ID:tenantfrom the output.AZURE_SUBSCRIPTION_ID: Your Azure subscription ID.
Set up GitHub Secrets:
Go to your GitHub repository’s Settings → Secrets.
Add the following secrets:
AZURE_CLIENT_IDAZURE_CLIENT_SECRETAZURE_TENANT_IDAZURE_SUBSCRIPTION_ID
3. Create the GitHub Actions Workflow File
Next, create a GitHub Actions workflow file that will automate the deployment process.
Steps to Create the Workflow File:
In your GitHub repository, create a new directory
.github/workflows/.Inside that directory, create a new file
deploy.yml.Here's the
deploy.ymlGitHub Actions workflow file:
xxxxxxxxxx361nameDeploy Bicep Template to Azure2on3 push4 branches5main # Trigger deployment on push to main branch6jobs7 deploy8 runs-onubuntu-latest # Use the latest Ubuntu image for the runner9 steps10 # Step 1: Checkout the repository11nameCheckout repository12 usesactions/checkout@v313 # Step 2: Set up Azure CLI14nameSet up Azure CLI15 usesazure/setup-azurecli@v116 with17 azure-cli-version'2.37.0' # Set the version you want18 # Step 3: Log in to Azure using the service principal19nameAzure Login20 usesazure/login@v121 with22 client-id$ secrets.AZURE_CLIENT_ID 23 client-secret$ secrets.AZURE_CLIENT_SECRET 24 tenant-id$ secrets.AZURE_TENANT_ID 25 # Step 4: Deploy Bicep template26nameDeploy Bicep Template27 run28 az deployment group create \29 --resource-group <your-resource-group> \30 --template-file ./main.bicep \31 --parameters location='East US' vnetName='myVNet' subnetName='mySubnet'32 # Optional Step: Show output of deployed resources33nameShow deployed resources34 run35 az network vnet show --resource-group <your-resource-group> --name myVNet36 az network vnet subnet show --resource-group <your-resource-group> --vnet-name myVNet --name mySubnetExplanation of the Workflow File:
Trigger: The workflow is triggered on a push to the
mainbranch.Steps:
Checkout: This step checks out the repository code.
Set up Azure CLI: Installs Azure CLI on the GitHub runner.
Azure Login: Logs in to Azure using the service principal credentials stored in GitHub secrets.
Deploy Bicep Template: This step runs the Azure CLI command to deploy the
main.bicepfile to the specified Azure resource group. It uses theaz deployment group createcommand.Show Deployed Resources (Optional): This step verifies the deployment by showing the deployed VNet and subnet in the Azure portal.
4. Deploy the Bicep Template via GitHub Actions
Once the workflow file is created, push your changes to the main branch:
xxxxxxxxxx31git add .github/workflows/deploy.yml2git commit -m "Add GitHub Actions workflow for Bicep deployment"3git push origin main5. Monitor the Workflow
After pushing the changes to the main branch, GitHub Actions will automatically start running the workflow.
You can monitor the status of the deployment from the Actions tab of your GitHub repository.
Go to your GitHub repository.
Click on the Actions tab.
You will see the workflow running (it may take a minute or two).
If the workflow runs successfully, your resources will be deployed to Azure.
You can also see logs to troubleshoot if needed.
6. Verify the Deployment
Once the deployment is complete, go to the Azure Portal and verify that the resources (Virtual Network and Subnet) were created successfully.
Alternatively, you can use Azure CLI to check:
xxxxxxxxxx81az network vnet show \2--resource-group <your-resource-group> \3--name myVNet4
5az network vnet subnet show \6--resource-group <your-resource-group> \7--vnet-name myVNet \8--name mySubnet7. Summary
With the above steps, you've automated the process of deploying a Bicep file to Azure using GitHub Actions.
This setup:
Ensures that your Bicep file can be deployed continuously and consistently.
Provides a robust, automated deployment pipeline, reducing manual intervention.






















Leave a Reply