Problem Statement
Is it possible to have an automated deployment mechanism for the SQL server database through Azure DevOps?
Prerequisites
- Server/Virtual machine able to access the SQL database
- DACPAC file
Solution
- Create a Self Hosted Agent in Azure DevOps mapping to the Server (from which one can access the SQL database).
Steps for Setting up a Self-hosted Agent.
- Once an agent is configured, we need to provide the server with the below level of access to deploy the database.
- Server admin access on the SQL database server
USE [master]
GO
CREATE LOGIN [domain\<<VMName>>] FROM WINDOWS WITH DEFAULT_DATABASE=[master], DEFAULT_LANGUAGE=[us_english]
GO
ALTER SERVER ROLE [serveradmin] ADD MEMBER [domain\ <<VMName>> ]
GO
- Create a Release Pipeline in Azure Pipelines set the Build Agent to the Self Hosted Agent and finally add the SQL Server Database deploy task with the below configurations:
YAML
Steps
- Task: SqlDacpacDeploymentOnMachineGroup@0
- displayName: ‘SQL Server Database Deployment’
inputs
- DacpacFile: ‘$(DACPAC Path)’
- ServerName: ‘$(Server Name)’
- DatabaseName: ‘$(Database Name)’
There is also an SQL Server authentication type supported for SQL Server database deployment but Windows Authentication is a much more secure way for deployment.