Azure SQL Database is a relational database commonly used in the MDW architecture, typically in the serving layer. The following samples demonstrates how you might build CI/CD pipelines to deploy changes to Azure SQL Database.
The following are some sample Azure DevOps pipelines. To deploy these samples, see How to use the samples.
-
Validate Pull Request [azure-pipelines-01-validate-pr]
- This pipeline builds the DACPAC and runs tests (if any). This is triggered only on PRs and is used to validate them before merging into
main
. This pipeline does not produce any artifacts.
- This pipeline builds the DACPAC and runs tests (if any). This is triggered only on PRs and is used to validate them before merging into
-
Build Pipeline [azure-pipelines-02-build]
- This pipeline builds the DACPAC and publishes it as a Build Artifact. Its purpose is to produce the Build Artifact that may be consumed by aRelease Pipeline (classic).
-
Simple Multi-Stage Pipeline [azure-pipelines-03-simple-multi-stage]
- This pipeline demonstrates a simple multi-stage pipeline.
- It has two stages:
- Build - builds the DACPAC and creates a Pipeline Artifact.
- Deploy - deploys the DACPAC to a target AzureSQL instance.
- Required Pipeline Variables:
- AZURESQL_SERVER_NAME - Name of the AzureSQL server (ei. myserver.database.windows.net)
- AZURESQL_DB_NAME - Name of the AzureSQL Database
- AZURESQL_SERVER_USERNAME - Username of AzureSQL login
- AZURESQL_SERVER_PASSWORD - Password of AzureSQL login
-
Multi-Stage Pipeline with pre-deployment test [azure-pipelines-04-multi-stage-predeploy-test]
- This pipeline expands on the simple multi-stage pipeline by introducing a middle pipeline stage which deploys the DACPAC first to a freshly restored copy of the production database prior to deployment to production. It has the ff. stages:
- Build
- Deploy to Test
- Restore Prod Db to Test Db.
- Deploy DACPAC to Test Db.
- Teardown Test Db.
- Deploy to Prod
- This mimics, to a certain extent, a production release, as it is applying the expected changes to a copy of the production database. It also allows for potentially running additional tests run in this environment, along with capturing common schema change errors such as adding a non-nullable column without a default value.
- Important considerations:
- Depending on the size and pricing tier of your Production AzureSQL database, a restore might take several minutes to several hours. Consider batching changes and running this pipeline on a schedule (such as nightly) instead on every commit to
main
. - For simplicity purposes, the Test database is deployed in the same logical server as Production, however, in reality these should be completely separate servers.
- Depending on the size and pricing tier of your Production AzureSQL database, a restore might take several minutes to several hours. Consider batching changes and running this pipeline on a schedule (such as nightly) instead on every commit to
- This pipeline expands on the simple multi-stage pipeline by introducing a middle pipeline stage which deploys the DACPAC first to a freshly restored copy of the production database prior to deployment to production. It has the ff. stages:
- Github account
- Azure Account
- Permissions needed: ability to create and deploy to an azure resource group, a service principal, and grant the collaborator role to the service principal over the resource group.
- Azure DevOps Account
- Permissions needed: ability to create service connections and pipelines.
- For Windows users, Windows Subsystem for Linux
- Azure CLI 2.0.49+
- Azure DevOps CLI
- jq
- For editing AzureSQL objects, Visual Studio 2019. For earlier version of Visual Studio, you may need to install SQL Server Data Tools separately.
To setup the samples, run the following:
- Ensure that:
- You are logged in to the Azure CLI. To login, run
az login
. - Azure CLI is targeting the Azure Subscription you want to deploy the resources to.
- To set target Azure Subscription, run
az account set -s <AZURE_SUBSCRIPTION_ID>
- To set target Azure Subscription, run
- Azure CLI is targeting the Azure DevOps organization and project you want to deploy the pipelines to.
- To set target Azure DevOps project, run
az devops configure --defaults organization=https://dev.azure.com/MY_ORG/ project=MY_PROJECT
- To set target Azure DevOps project, run
- You are logged in to the Azure CLI. To login, run
- Fork* and clone this repository.
cd
in tosingle_tech_samples/azuresql
. - Set the following environment variables:
- GITHUB_REPO_URL - URL of your forked github repo
- GITHUB_PAT_TOKEN - a Github PAT token. Generate them here. This requires "repo" scope.
Optionally, set the following environment variables
- DEPLOYMENT_ID - string appended to all resource names. Default: random five character string.
- BRANCH_NAME** - git branch with Azure DevOps pipelines definitions to deploy. Default: main.
- RESOURCE_GROUP_NAME - target resource group to deploy to
- RESOURCE_GROUP_LOCATION - location of target resource group
- AZURESQL_SERVER_PASSWORD - Password of the admin account for your AzureSQL server instance. Default: mdw-dataops-SqlP@ss-${DEPLOYMENT_ID}
- Username is set to sqlAdmin.
- Run
./deploy.sh
.***
*Your forked repo will serve as the main repository which triggers all pipelines -- allowing you complete control over the sample solution as compared to using the main Azure-Samples repository directly. All pipeline definitions are also pulled from this fork.
**The pipelines are deployed to use the main
branch by default. This can be inconvenient when working on the pipelines in a different branch. You can set the BRANCH_NAME environment variable to override this behaviour. This makes it easier to test changes to your pipeline file. Be sure to push the changes in your yaml file to your repo before running the deployment script. Note that it does not change branch triggers.
***Note that in case of any errors midway through the script, in order to rerun the deployment, you may need to perform some cleanup of any deployed resources. See Cleanup below.
Once you've setup the sample, you should have the following deployed:
-
Azure resource group with a AzureSQL server and database called
salesdb
. -
A service principal with collaborator rights over the deployed resource group.
-
Two Azure DevOps service connections found under
Project Settings > Service Connections
:- mdw-dataops-azure - An AzureRM service connection configured with the Service Principal. This is used to deploy to the AzureSQL database.
- github-mdw-dataops - A Github service connection used to pull from the forked repository. It uses the Github PAT token to authenticate.
-
Three Azure DevOps pipelines found under
Pipelines > Builds
. See Key concepts/Azure DevOps Pipelines below for explanation of each:- azuresql-validate-pr
- azuresql-build
- azuresql-simple-multi-stage
. Each of the pipelines should have run (or is currently running) at least once.
The following shows how to deploy changes to the AzureSQL database using the CI/CD pipelines.
-
In local clone of your forked repo, create a local branch of
main
and call itdev/sample_change
. Checkout this branch. -
Open the Visual Studio solution
src/ddo_samples_azuresql.sln
. -
Add a new CompanyAddress column to the
SaleLT.Customer
table, as seen below. -
Commit and push the change to the remote branch in Github.
-
Raise a Pull Request to merge
dev/sample_change
tomain
. This should trigger the theazuresql-validate-pr
pipeline. -
Typically, a code review would take place. After this, merge changes to
main
by completing the Pull Request. This should trigger theazuresql-build
andazuresql-simple-multi-stage
. -
In Azure DevOps, navigate to the
azuresql-simple-muli-stage
pipeline run. The second stage of this pipeline will deploy the changes to the AzureSQL Database. -
Connect to the AzureSQL Database. Notice that the new column has been deployed.
Congratulations! You've deployed changes via CI/CD process.
- Delete the Azure resource group.
- Delete Azure DevOps Pipelines.
- To delete via Web Portal, navigate to
Pipelines > Builds
. Delete the pipeline, click on the triple dots, and select delete. - To delete via CLI, run
az pipelines list
to list ids. Thenaz pipelines delete --id PIPELINE_ID
.
- To delete via Web Portal, navigate to
- Delete Azure DevOps service connections.
- To delete via Web Portal, navigate to
Project Settings > Service Connections
. Select the connection, click on the triple dots, and select delete. - To delete via CLI, run
az devops service-endpoint list
to list ids. Thenaz devops service-endpoint delete --id SERVICE_ENDPOINT_ID
.
- To delete via Web Portal, navigate to