Azure DevOps Services | Azure DevOps Server 2022 - Azure DevOps Server 2019
You can automatically deploy your database updates to Azure SQL database after every successful build.
DACPAC
The simplest way to deploy a database is to create data-tier package or DACPAC. DACPACs can be used to package and deploy schema changes and data. You can create a DACPAC using the SQL database project in Visual Studio.
To deploy a DACPAC to an Azure SQL database, add the following snippet to your azure-pipelines.yml file.
YAML
- task:SqlAzureDacpacDeployment@1 displayName:ExecuteAzureSQL:DacpacTask inputs: azureSubscription:'<Azure service connection>' ServerName:'<Database server name>' DatabaseName:'<Database name>' SqlUsername:'<SQL user name>' SqlPassword:'<SQL user password>' DacpacFile:'<Location of Dacpac file in $(Build.SourcesDirectory) after compilation>'
When setting up a build pipeline for your Visual Studio database project, use the .NET desktop template. This template automatically adds the tasks to build the project and publish artifacts, including the DACPAC.
When setting up a release pipeline, choose Start with an empty pipeline, link the artifacts from build, and then add an Azure SQL Database Deployment task.
Instead of using a DACPAC, you can also use SQL scripts to deploy your database. Here’s a simple example of a SQL script that creates an empty database.
SQL
USE [main]
GOIFNOTEXISTS (SELECTnameFROM main.sys.databases WHEREname = N'DatabaseExample')
CREATEDATABASE [DatabaseExample]
GO
To run SQL scripts as part of a pipeline, you’ll need Azure PowerShell scripts to create and remove firewall rules in Azure. Without the firewall rules, the Azure Pipelines agent can’t communicate with Azure SQL Database.
The following PowerShell script creates firewall rules. You can check in this script as SetAzureFirewallRule.ps1 into your repository.
When you set up a build pipeline, make sure that the SQL script to deploy the database and the Azure PowerShell scripts to configure firewall rules are part of the build artifact.
When you set up a release pipeline, choose Start with an Empty process, link the artifacts from build, and then use the following tasks:
Use the Azure PowerShell task to add a firewall rule in Azure to allow the Azure Pipelines agent to connect to Azure SQL Database. The script requires one argument - the name of the SQL server you created.
Use the PowerShell task to invoke SQLCMD and execute your scripts. Add the following inline script to your task:
Use another Azure PowerShell task to remove the firewall rule in Azure.
Azure service connection
The Azure SQL Database Deployment task is the primary mechanism to deploy a database to Azure. This task, as with other built-in Azure tasks, requires an Azure service connection as an input. The Azure service connection stores the credentials to connect from Azure Pipelines to Azure.
The easiest way to get started with this task is to be signed in as a user that owns both the Azure DevOps organization and the Azure subscription.
In this case, you won't have to manually create the service connection.
Otherwise, to learn how to create an Azure service connection, see Create an Azure service connection.
To do this in YAML, you can use one of these techniques:
Isolate the deployment steps into a separate job, and add a condition to that job.
Add a condition to the step.
The following example shows how to use step conditions to deploy only those builds that originate from main branch.
YAML
- task:SqlAzureDacpacDeployment@1 condition:and(succeeded(),eq(variables['Build.SourceBranch'],'refs/heads/main')) inputs: azureSubscription:'<Azure service connection>' ServerName:'<Database server name>' DatabaseName:'<Database name>' SqlUsername:'<SQL user name>' SqlPassword:'<SQL user password>' DacpacFile:'<Location of Dacpac file in $(Build.SourcesDirectory) after compilation>'
SQL Azure Dacpac Deployment may not support all SQL server actions
that you want to perform. In these cases, you can simply use PowerShell or command-line scripts to run the commands you need.
This section shows some of the common use cases for invoking the SqlPackage.exe tool.
As a prerequisite to running this tool, you must use a self-hosted agent and have the tool installed on your agent.
Piezīme
If you execute SQLPackage from the folder where it is installed, you must prefix the path with & and wrap it in double-quotes.
Basic Syntax
<Path of SQLPackage.exe> <Arguments to SQLPackage.exe>
You can use any of the following SQL scripts depending on the action that you want to perform
Extract
Creates a database snapshot (.dacpac) file from a live SQL server or Microsoft Azure SQL Database.
Incrementally updates a database schema to match the schema of a source .dacpac file. If the database doesn’t exist on the server, the publish operation will create it. Otherwise, an existing database will be updated.
Pievienojieties meetup sērijai, lai kopā ar citiem izstrādātājiem un ekspertiem izveidotu mērogojamus AI risinājumus, kuru pamatā ir reālas lietošanas gadījumi.
Learn how to create and configure an Azure SQL Database. You'll use SQL Database Projects in VS Code, including installing the extension, importing, and modifying a schema. Additionally, you'll build and deploy database projects in GitHub Actions and Azure Pipelines, and automate and invoke the publishing of a database.
Administer an SQL Server database infrastructure for cloud, on-premises and hybrid relational databases using the Microsoft PaaS relational database offerings.
A data-tier application (DAC) is a logical database entity that defines all of the SQL Server objects - such as tables, views, and instance objects, including logins - associated with a database.
Learn about actions that you can take with data-tier applications (DACs). Examples include extracting, publishing, and registering snapshot (.dacpac) files.
Manual database deployments can be time-consuming and error-prone, making sure only approved changes are deployed to test, UAT and ultimately production. Azure Pipelines helps development teams and DBAs by providing a consistent process to deploy changes, as changes are checked into source control. This episode will touch on Azure Repos and Azure Data Studio, besides Azure Pipelines. Chapters 00:22 - Introduction 00:57 - Azure Data Pipelines introduction and when to use them 01:42 - Getting started 02:32 -