CI/CD for SQL Server automates building, testing, and deploying T-SQL changes from source control to each environment.
Automating database releases removes manual steps, enforces code review, and guarantees every environment—from dev to prod—runs the same scripts. Teams catch errors earlier, shorten release cycles, and create reliable rollback points.
Commit every T-SQL change: DDL scripts, seed data, reference lists, and migration files. Version the .dacpac
or schema.sql
snapshot generated during CI so reviewers can see diffs.
Use sqlcmd
or SqlPackage
inside a container. Spin up a disposable SQL Server 2022 container, execute migrations, then run unit tests with tSQLt or Pester-SQL. Tear down after tests pass.
The YAML below restores NuGet, builds a DACPAC, tests against a container, and deploys to staging when the main branch updates. Secrets store SQL_SA_PASSWORD
and STG_CONN
.
name: ci-cd-sqlserver
.
on:
push:
branches: [main]
jobs:
build-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Build DACPAC
run: dotnet build Database.csproj -c Release
- name: Start SQL container
run: |
docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=${{ secrets.SQL_SA_PASSWORD }}" \
-p 1433:1433 -d mcr.microsoft.com/mssql/server:2022-latest
- name: Deploy DACPAC to container
run: sqlpackage /Action:Publish /SourceFile:bin/Release/Database.dacpac \
/TargetConnectionString:"Server=localhost,1433;User sa;Password=${{ secrets.SQL_SA_PASSWORD }};TrustServerCertificate=True;"
- name: Run unit tests
run: dotnet test Tests/Tests.csproj
deploy-staging:
needs: build-test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Publish to staging
run: sqlpackage /Action:Publish /SourceFile:bin/Release/Database.dacpac \
/TargetConnectionString:"${{ secrets.STG_CONN }}"
Gate the prod job behind a manual approval or pull-request merge. Store the prod connection string in an encrypted secret and call sqlpackage
with /p:BlockOnPossibleDataLoss=true
to abort risky drops.
Write idempotent scripts, name migrations sequentially, seed deterministic test data, and run smoke tests after deployment. Always back up before the first prod run and enable point-in-time recovery.
Yes. Both tools version T-SQL scripts, integrate with GitHub Actions, and work with SQL Server. Replace sqlpackage steps with the Flyway or Liquibase CLI.
Store INSERT scripts in a refdata
folder and apply them after schema migrations. Use MERGE to make the scripts idempotent.