In this quick start, you use the GitHub Actions for Azure Resource Manager deployment to automate deploying a Bicep file to Azure.
This guide explains how to set up GitHub Actions for automated deployment of Bicep templates to Azure, covering basic setup, advanced workflows, and security best practices.
Prerequisites
An Azure subscription
A GitHub repository
Basic knowledge of Bicep (Azure's Infrastructure as Code language)
Azure CLI installed (for initial setup)
Initial Setup
Create a resource group
First, create a resource group for your deployment:
az group create -n exampleRG -l westus
Create a service principal
Create a service principal with contributor access to your resource group:
az ad sp create-for-rbac --name "GitHubActionsSP" --role contributor \
--scopes /subscriptions/{subscription-id}/resourceGroups/exampleRG \
--sdk-auth
This command outputs JSON credentials that look similar to:
Instead of using the Contributor role at the resource group level, consider using custom roles with just the permissions needed:
# Create a custom role definition file (custom-role.json)
{
"Name": "Bicep Deployer",
"Description": "Can deploy resources from Bicep files",
"Actions": [
"Microsoft.Resources/deployments/*",
"Microsoft.Resources/subscriptions/resourceGroups/read"
],
"AssignableScopes": [
"/subscriptions/{subscription-id}/resourceGroups/exampleRG"
]
}
# Create the custom role
az role definition create --role-definition custom-role.json
# Assign the custom role to your service principal
az role assignment create --assignee {service-principal-id} \
--role "Bicep Deployer" \
--scope "/subscriptions/{subscription-id}/resourceGroups/exampleRG"
2. Use OpenID Connect (OIDC) Instead of Secrets
OIDC provides a more secure way to authenticate GitHub Actions with Azure without storing long-lived credentials:
# Create an app registration first
appId=$(az ad app create --display-name "GitHub-Actions-OIDC" --query appId -o tsv)
objectId=$(az ad app show --id $appId --query id -o tsv)
# Create a service principal
spId=$(az ad sp create --id $appId --query id -o tsv)
# Assign role
az role assignment create \
--role Contributor \
--assignee $spId \
--scope "/subscriptions/{subscription-id}/resourceGroups/exampleRG"
# Create federated credential
az ad app federated-credential create \
--id $objectId \
--parameters "{\"name\":\"github-federated\",\"issuer\":\"https://token.actions.githubusercontent.com\",\"subject\":\"repo:your-org/your-repo:ref:refs/heads/main\",\"audiences\":[\"api://AzureADTokenExchange\"]}"
# Save these values as GitHub secrets
echo "AZURE_CLIENT_ID: $appId"
echo "AZURE_TENANT_ID: $(az account show --query tenantId -o tsv)"
echo "AZURE_SUBSCRIPTION: $(az account show --query id -o tsv)"
3. Secure Parameters Handling
Store sensitive parameters in GitHub secrets and pass them securely:
Using GitHub Actions with Bicep provides a powerful way to automate your infrastructure deployment to Azure. By implementing proper security practices and leveraging advanced workflow configurations, you can create reliable, secure CI/CD pipelines for your infrastructure code.
For more information, refer to the official documentation: