azure-devopsazure-pipelinesazure-sql-databasedacpac

Build and Deployment of DACPAC via Azure DevOps Pipeline without .sln (or any artifact)


I need to build, publish and deploy a .dacpac file in order the database to adhere with the changes that take place in the code.

I read several articles but all of them make the assumption of having Visual studio to generate a .sln file and from there they start building up

      - task: VSBuild@1
        displayName: 'Build Solution: bla.sln'
        inputs:
          solution: database/bla.sln
          platform: 'Any CPU'
          configuration: Release

In my case the bla.sln file is not there and I don't want to create one (if that's possible)

Tooling I'm using is: PyCharm, Azure DevOps pipeline, Azure SQL server, Azure SQL Databases

Ultimate goal: to have database as code (schemas, roles, ...) in my repo and a pipeline which when triggered will build, publish and deploy the .dacpac to some of the SQL server's databases.

Is this possible? You have an example?

After some investigation and stackoverflow discussion I realized that it's possible to have a .sqlproj file in your codebase that kinda "describes" how the database look like (tables, stored procedures and etc). If I apply some changes to the schema of these databases how these changes can be reflected in the .sqlproj file, since in the above step you only build the file?


Solution

  • Based on the discussions, the .sln or .sqlproj files from your SQL database project should NOT be considered as artifacts but ought to be pushed as source code into one of your cloud Azure Repos, so that they could be built by the VSBuild@1 pipeline task running in an agent job.

    Taking the SQL database project for example, the pipeline can either build WideWorldImportersDW.sln or WideWorldImportersDW.sqlproj to generate the .dacpac package and then deploy it to Azure SQL database via the SqlAzureDacpacDeployment@1 pipeline task. Here is a sample YAML pipeline for your reference.

    variables:
      ARMSvcCnnName: YourAzureResourceManagerServiceConnectionName
      AzureSQLServerName: YourAzureSQLSeverName
      AzureSQLDBName: YourAzureSQLDBName
      buildPlatform: 'Any CPU'
      buildConfiguration: 'Release'
    
    pool:
      vmImage: 'windows-latest'
    
    steps:
    - task: VSBuild@1
      displayName: 'Build DACPAC from SQL Database Project'
      inputs:
        solution: '**\*.sqlproj'
        platform: '$(buildPlatform)'
        configuration: '$(buildConfiguration)'
    - task: SqlAzureDacpacDeployment@1
      displayName: 'Deploy Azure SQL DB'
      inputs:
        azureSubscription: '$(ARMSvcCnnName)'
        AuthenticationType: 'servicePrincipal'
        ServerName: '$(AzureSQLServerName).database.windows.net'
        DatabaseName: '$(AzureSQLDBName)'
        deployType: 'DacpacTask'
        DeploymentAction: 'Publish'
        DacpacFile: '**\*.dacpac'
        AdditionalArguments: '/v:ETLUserPassword="$(sql-password)" /v:AppUserPassword="$(sql-password)"'
        IpDetectionMethod: 'AutoDetect'
        DeleteFirewallRule: false
    
    

    Image Image Image