Use GitHub Actions to connect to Azure SQL Database
Applies to:
Azure SQL Database
Get started with GitHub Actions by using a workflow to deploy database updates to Azure SQL Database.
Prerequisites
You need:
- An Azure account with an active subscription. Create an account for free.
- A GitHub repository with a dacpac package (
Database.dacpac
). If you don't have a GitHub account, sign up for free. - An Azure SQL Database. Quickstart: Create an Azure SQL Database single database.
- A .dacpac file to import into your database.
Workflow file overview
A GitHub Actions workflow is defined by a YAML (.yml) file in the /.github/workflows/
path in your repository. This definition contains the various steps and parameters that make up the workflow.
The file has two sections:
Section | Tasks |
---|---|
Authentication | 1.1. Generate deployment credentials. |
Deploy | 1. Deploy the database. |
Generate deployment credentials
To use Azure Login action with OIDC, you need to configure a federated identity credential on a Microsoft Entra application or a user-assigned managed identity.
Option 1: Microsoft Entra application
- Create a Microsoft Entra application with a service principal by Azure portal, Azure CLI, or Azure PowerShell.
- Copy the values for Client ID, Subscription ID, and Directory (tenant) ID to use later in your GitHub Actions workflow.
- Assign an appropriate role to your service principal by Azure portal, Azure CLI, or Azure PowerShell.
- Configure a federated identity credential on a Microsoft Entra application to trust tokens issued by GitHub Actions to your GitHub repository.
Option 2: User-assigned managed identity
- Create a user-assigned managed identity.
- Copy the values for Client ID, Subscription ID, and Directory (tenant) ID to use later in your GitHub Actions workflow.
- Assign an appropriate role to your user-assigned managed identity.
- Configure a federated identity credential on a user-assigned managed identity to trust tokens issued by GitHub Actions to your GitHub repository.
Copy the SQL connection string
In the Azure portal, go to your Azure SQL Database and open Settings > Connection strings. Copy the ADO.NET connection string. Replace the placeholder values for your_database
and your_password
.
You'll set the connection string as a GitHub secret, AZURE_SQL_CONNECTION_STRING
.
Configure the GitHub secrets
You need to provide your application's Client ID, Directory (tenant) ID, and Subscription ID to the login action. These values can either be provided directly in the workflow or can be stored in GitHub secrets and referenced in your workflow. Saving the values as GitHub secrets is the more secure option.
In GitHub, go to your repository.
Select Security > Secrets and variables > Actions.
Select New repository secret.
Note
To enhance workflow security in public repositories, use environment secrets instead of repository secrets. If the environment requires approval, a job cannot access environment secrets until one of the required reviewers approves it.
Create secrets for
AZURE_CLIENT_ID
,AZURE_TENANT_ID
, andAZURE_SUBSCRIPTION_ID
. Copy these values from your Microsoft Entra application or user-assigned managed identity for your GitHub secrets:GitHub secret Microsoft Entra application or user-assigned managed identity AZURE_CLIENT_ID Client ID AZURE_SUBSCRIPTION_ID Subscription ID AZURE_TENANT_ID Directory (tenant) ID Note
For security reasons, we recommend using GitHub Secrets rather than passing values directly to the workflow.
Add the SQL connection string secret
In GitHub, go to your repository.
Go to Settings in the navigation menu.
Select Security > Secrets and variables > Actions.
Select New repository secret.
Paste your SQL connection string. Give the secret the name
AZURE_SQL_CONNECTION_STRING
.Select Add secret.
Add your workflow
Go to Actions for your GitHub repository.
Select Set up your workflow yourself.
Delete everything after the
on:
section of your workflow file. For example, your remaining workflow may look like this.name: SQL for GitHub Actions on: push: branches: [ main ] pull_request: branches: [ main ]
Rename your workflow
SQL for GitHub Actions
and add the checkout and login actions. These actions check out your site code and authenticate with Azure using theAZURE_CREDENTIALS
GitHub secret you created earlier.name: SQL for GitHub Actions on: push: branches: [ main ] pull_request: branches: [ main ] jobs: build: runs-on: windows-latest steps: - uses: actions/checkout@v1 - uses: azure/login@v2 with: client-id: ${{ secrets.AZURE_CLIENT_ID }} tenant-id: ${{ secrets.AZURE_TENANT_ID }} subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
Use the Azure SQL Deploy action to connect to your SQL instance. You should have a dacpac package (
Database.dacpac
) at the root level of your repository. Use theAZURE_SQL_CONNECTION_STRING
GitHub secret you created earlier.- uses: azure/sql-action@v2 with: connection-string: ${{ secrets.AZURE_SQL_CONNECTION_STRING }} path: './Database.dacpac' action: 'Publish'
Complete your workflow by adding an action to logout of Azure. Here's the completed workflow. The file appears in the
.github/workflows
folder of your repository.name: SQL for GitHub Actions on: push: branches: [ main ] pull_request: branches: [ main ] jobs: build: runs-on: windows-latest steps: - uses: actions/checkout@v1 - uses: azure/login@v2 with: client-id: ${{ secrets.AZURE_CLIENT_ID }} tenant-id: ${{ secrets.AZURE_TENANT_ID }} subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }} # Azure logout - name: logout run: | az logout
Review your deployment
Go to Actions for your GitHub repository.
Open the first result to see detailed logs of your workflow's run.
Clean up resources
When your Azure SQL database and repository are no longer needed, clean up the resources you deployed by deleting the resource group and your GitHub repository.