title | description | author | ms.devlang | ms.topic | ms.date | ms.author | ms.custom |
---|---|---|---|---|---|---|---|
Tutorial: Use GitHub Actions to deploy to App Service and connect to a database |
Deploy a database-backed ASP.NET core app to Azure with GitHub Actions |
cephalin |
csharp |
tutorial |
01/09/2023 |
jukullam |
github-actions-azure |
Learn how to set up a GitHub Actions workflow to deploy a ASP.NET Core application with an Azure SQL Database backend. When you're finished, you have an ASP.NET app running in Azure and connected to SQL Database. You'll first use an ARM template to create resources.
This tutorial does not use containers. If you want to deploy to a containerized ASP.NET Core application, see Use GitHub Actions to deploy to App Service for Containers and connect to a database.
In this tutorial, you learn how to:
[!div class="checklist"]
- Use a GitHub Actions workflow to add resources to Azure with a Azure Resource Manager template (ARM template)
- Use a GitHub Actions workflow to build an ASP.NET Core application
[!INCLUDE quickstarts-free-trial-note]
To complete this tutorial, you'll need:
- An Azure account with an active subscription. Create an account for free.
- A GitHub account. If you don't have one, sign up for free.
- A GitHub repository to store your Resource Manager templates and your workflow files. To create one, see Creating a new repository.
Fork the sample project in the Azure Samples repo.
https://github.com/Azure-Samples/dotnetcore-sqldb-ghactions
Open the Azure Cloud Shell at https://shell.azure.com. You can alternately use the Azure CLI if you've installed it locally. (For more information on Cloud Shell, see the Cloud Shell Overview.)
az group create --name {resource-group-name} --location {resource-group-location}
[!INCLUDE include]
[!INCLUDE include]
-
Create two new secrets in your GitHub repository for
SQLADMIN_PASS
andSQLADMIN_LOGIN
. Make sure you choose a complex password, otherwise the create step for the SQL database server will fail. You won't be able to access this password again so save it separately. -
Create an
AZURE_SUBSCRIPTION_ID
secret for your Azure subscription ID. If you do not know your subscription ID, use this command in the Azure Shell to find it. Copy the value in theSubscriptionId
column.az account list -o table
The create Azure resources workflow runs an ARM template to deploy resources to Azure. The workflow:
- Checks out source code with the Checkout action.
- Logs into Azure with the Azure Login action and gathers environment and Azure resource information.
- Deploys resources with the Azure Resource Manager Deploy action.
To run the create Azure resources workflow:
-
Open the
infraworkflow.yml
file in.github/workflows
within your repository. -
Update the value of
AZURE_RESOURCE_GROUP
to your resource group name. -
Set the input for
region
in your ARM Deploy actions to your region.- Open
templates/azuredeploy.resourcegroup.parameters.json
and update thergLocation
property to your region.
- Open
-
Go to Actions and select Run workflow.
:::image type="content" source="media/github-actions-workflows/github-actions-run-workflow.png" alt-text="Run the GitHub Actions workflow to add resources.":::
-
Verify that your action ran successfully by checking for a green checkmark on the Actions page.
:::image type="content" source="media/github-actions-workflows/create-resources-success.png" alt-text="Successful run of create resources. ":::
-
After you've created your resources, go to Actions, select Create Azure Resources, disable the workflow.
:::image type="content" source="media/github-actions-workflows/disable-workflow-github-actions.png" alt-text="Disable the Create Azure Resources workflow.":::
-
In the Azure portal, open your new staging App Service (Slot) created with the
Create Azure Resources
workflow. -
Select Get Publish Profile.
-
Open the publish profile file in a text editor and copy its contents.
-
Create a new GitHub secret for
AZURE_WEBAPP_PUBLISH_PROFILE
.
To run the build and deploy workflow:
-
Open your
workflow.yaml
file in.github/workflows
within your repository. -
Verify that the environment variables for
AZURE_RESOURCE_GROUP
,AZURE_WEBAPP_NAME
,SQLSERVER_NAME
, andDATABASE_NAME
match the ones ininfraworkflow.yml
. -
Verify that your app deployed by visiting the URL in the Swap to production slot output. You should see a sample app, My TodoList App.
If you no longer need your sample project, delete your resource group in the Azure portal and delete your repository on GitHub.
[!div class="nextstepaction"] Learn about Azure and GitHub integration