Setting Up a Service Principal to Deploy to Azure App Service With GitHub Actions
Posted December 30, 2023
Reading time: 3 minutes
I started moving my personal applications out of Azure DevOps and into GitHub. As part of that, I needed to convert my DevOps Pipelines into GitHub Actions.
Figuring out how to get GitHub Actions to authenticate with Azure App Service and do deployment tasks was not completely straightforward, but after a ton of googling and experimenting, I figured out just enough to make it work. Read on to see the steps.
Create a Service Principal in Microsoft Entra ID
By Service Principal, they mean an App registration
.
- In Azure Portal, go to Microsoft Entra ID and click
App registration
- Click
New registration
- Type in a name for your app
- Under
Supported account types
, choose who can use this application (e.g.,Accounts in this organizational directory only (Default Directory only - Single tenant)
) - Under
Select a platform
, chooseWeb
. It’s okay to leave the URL blank. - Click
Register
This will take you to the App registration Overview screen. You’ll need two pieces of information from this screen:
Application (client) ID
Directory (tenant) ID
Save them for later when you’re creating credentials for GitHub Actions.
Next, you need to create a client secret:
- Click
Certificates & secrets
- Click
New client secret
- Choose an Expiration
- Click
Add
You’ll need to copy the secret now, as it’s only visible immediately after you create it. This is the third part of the credentials.
Grant the App Service’s Contributor Role to your App registration
In Azure Portal, go to your App Service:
- Choose
App Services
from the menu - Click on your App Service
- Click
Access control (IAM)
- Click
Add
, thenAdd role assignment
- Click
Privileged administrator roles
- Select
Contributor
- Click
Next
- Select Assign access to
User, group, or service principal
- Search for and select the service principal you created above
- Click
Next
- Click
Review & assign
2024-04-08 I started getting errors like the following when my Action tried to deploy to Azure App Service:
ERROR: (AuthorizationFailed) The client '{client-id}' with object id '{object-id}' does not have authorization to perform action 'Microsoft.Web/serverfarms/read' over scope '{my-scope}' or the scope is invalid. If access was recently granted, please refresh your credentials. Code: AuthorizationFailed
Adding the App registration’s service principal to the
Reader
roll for the App Service Plan resolved this issue.
Copy the Subscription ID
Still in Azure Portal, you’ll need to copy the ID of the Subscription where your App Service resides:
- Choose
Subscriptions
from the menu - Click on the proper subscription
- Copy the
Subscription ID
from the overview screen
Now you have the four pieces of information you need to create a secret in your GitHub Actions.
Create a secret in your GitHub repository
The Azure/login
action requires credentials as a JSON string. Using the values you recorded in the steps above, fill in the values
using this template:
|
|
In your GitHub repository settings:
- Expand
Secrets and variables
and clickActions
- Create a new secret. I named mine
AZURE_CREDENTIALS
. - Paste in the JSON object from above
That should be all you need to use Azure/login
so that you can perform App Service actions like zip deploy and slot swaps. In one of
your steps, you would reference it like so:
|
|
Recorded to save Future Jon much time and gnashing of teeth. 😬