Implement CI/CD for SQL Server with Visual Studio and Azure DevOps

Implementing Continuous Integration and Continuous Deployment (CI/CD) for SQL Server databases ensures that database changes are automatically built, tested, and deployed to various environments. This guide will walk you through setting up a CI/CD pipeline for a SQL Server database project using Visual Studio and Azure DevOps, including how to automatically synchronize changes from a live database to your project.

Prerequisites

Before starting, ensure you have,

  • Visual Studio 2019/2022: With SQL Server Data Tools (SSDT) installed.
  • Azure DevOps Account: For managing your CI/CD pipelines.
  • SQL Server Database Project: Either create a new one or use an existing project.
  • Git Repository: For source control, preferably integrated with Azure DevOps.

Step 1. Setting Up the SQL Server Database Project
 

Creating a New SQL Server Database Project

  1. Open Visual Studio.
  2. Go to File > New > Project.
  3. Select SQL Server Database Project from the list of templates.
  4. Provide a name for your project and click Create.
  5. Import existing database objects if applicable, or start creating new objects such as tables, views, stored procedures, etc.

Importing an Existing Database

  1. Right-click on the project in Solution Explorer.
  2. Choose Import > Database.
  3. Connect to the existing database by providing the server name and credentials.
  4. Select the database objects you want to import and click Finish.

Step 2. Version Control Integration
 

Committing Your Project to Source Control

  1. Initialize a Git repository for your project if it’s not already under version control.
  2. Use the Team Explorer window in Visual Studio to commit and push your changes to the remote repository.

Step 3. Creating the CI Pipeline Using Azure DevOps Classic Editor
 

Setting Up the Build Pipeline

  1. Import the project from the GitHub repository.
  2. Select the Repository from the list.
  3. Now click on the import project.
  4. Navigate to Azure DevOps and open your project.
  5. Go to Pipelines > Builds.
  6. Click New Pipeline and select Use the classic editor to create a pipeline without YAML.
    New Pipeline
  7. Select the GitHub option, and then make the connection with Github.
     GitHub option
  8. Select the repository where your SQL Server Database project is stored.
    SQL Server Database
  9. Choose the Visual Studio Build template.

Configuring the Build Pipeline

  1. Select the .NET Desktop option from the list for SQL Server Database project CICD.
    .NET Desktop
  2. Add a NuGet restore task.
  3. Click + to add a task.
  4. Search for NuGet and add the NuGet restore task.
  5. Configure it to restore packages for your solution.
     NuGet restore
  6. Add a Visual Studio build task.
  7. Click + and add the Visual Studio Build task.
  8. Select your solution file (.sln) and set the configuration to Release.
    Release
  9. Add a Publish Build Artifacts task.
  10. Add the Publish Build Artifacts task.
  11. Set Path to Publish to $(Build.ArtifactStagingDirectory).
  12. Set Artifact Name to drop.
    Artifact Name
  13. Configure the Trigger Option for Continuous Integration.
    Trigger Option
  14. Save and queue the build.
  15. Save the pipeline, give it a name, and click Queue to run the build.
    Queue

Step 4. Setting Up the CD Pipeline Using Azure DevOps Classic Editor
 

Creating the Release Pipeline

  1. Navigate to Pipelines > Releases.
  2. Click New Pipeline and select Start with an Empty Job.
    Navigate to Pipelines
  3. Add an Artifact.
  4. Click Add an Artifact and select the build pipeline you created.
  5. Choose the default version (latest).
    Add an Artifact

Continuous deployment trigger

Continuous deployment

Configuring the Deployment Stage

  1. Click Add a task in the stage.
  2. Search for Azure SQL Database Deployment and add it to the pipeline.
     Azure SQL Database

Configure the Azure SQL Database Deployment task

  • Azure Subscription: Select your Azure subscription.
  • SQL Server Name: Enter your SQL Server name.
  • Authentication Type: Choose SQL Server Authentication and provide credentials.
  • Database Name: Enter the target database name.
  • DACPAC File: Point to the DACPAC file in the artifact ($(System.DefaultWorkingDirectory)/_drop/*.dacpac).
  • Deployment Options: Configure options as needed (e.g., drop objects not in source).
    Deployment Options
    Configure options

Configuring Deployment Triggers

  1. Automatic Deployment: Set the pipeline to trigger automatically after each successful build.
  2. Manual Deployment: Add approval steps if you want to manually control when deployments happen.
  3. Save and Deploy: Save the pipeline and trigger a deployment.
    Save and Deploy

Step 5. Automating Database Changes Import
 

Synchronizing Your Project with Database Changes

Automating Database

Schema Compare

  • In Visual Studio, go to Tools > SQL Server > New Schema Comparison.
  • Set the source as the live database and the target as your project.
  • Click Compare to view the differences between the database and the project.
  • Select the changes you want to import into your project and click Update.
    Schema Compare

Automating Schema Synchronization (Optional)

  1. You can schedule PowerShell scripts or use SQLPackage.exe to automate schema synchronization tasks, though this requires custom scripting.
    Schema Synchronization
  2. Now let's check the source CI Pipeline.
    CI Pipeline
  3. Now you can see that our CI part has been successfully executed.
  4. Now Let's Check the CD Pipeline.
     CD Pipeline
  5. Now Let's Compare the SOURCE $ DESTINATION Databases

Source Database

Source Database 

Destination Database

Destination Database

All database changes from the source database to the destination database have been deployed to the Azure cloud database using the CICD Piple line.

Step 6. Monitoring and Alerts
 

Setting Up Monitoring for CI/CD Pipelines

  1. Enable Azure DevOps Notifications: Set up notifications for build and release failures.
  2. Monitor Pipeline Logs: Regularly review pipeline logs for errors.
  3. Integrate with Azure Monitor: Track the performance and health of your deployed database.

Post-Deployment Verification

  1. Add Post-Deployment Scripts: Include scripts in your pipeline to verify database integrity after deployment.
  2. Automated Testing: Incorporate T-SQL unit tests to validate the deployment.

Conclusion

By following this guide, you can establish a robust CI/CD pipeline for your SQL Server database using Visual Studio and Azure DevOps' Classic Editor. This setup ensures that database changes are automatically built, tested, and deployed, helping you maintain consistency across environments and speeding up your development and deployment cycles.