Skip to content

Instantly share code, notes, and snippets.

@dzsquared
Created April 23, 2025 14:55
Show Gist options
  • Save dzsquared/f4ae6a3026f91af551d94a5f6a4a7970 to your computer and use it in GitHub Desktop.
Save dzsquared/f4ae6a3026f91af551d94a5f6a4a7970 to your computer and use it in GitHub Desktop.
Example CI pipeline that integrates database quality and pre-deployment checks
# this workflow will create the DB in a container
# and perform code analysis checks on the T-SQL code
# before getting a script for the anticipated deployment
name: SQL dev feedback
on:
workflow_dispatch:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
env:
DB_FOLDER: ContosoDB
jobs:
code-analysis:
runs-on: ubuntu-latest
env:
ConnectionStrings__SchoolContext: "Server=localhost;Database=ContosoDB;User Id=sa;Password=${{ secrets.CONTAINER_SQL_PASSWORD }};TrustServerCertificate=true;"
# service/sidecar container for sql
services:
mssql:
image: mcr.microsoft.com/mssql/server:2022-latest
env:
ACCEPT_EULA: 1
SA_PASSWORD: ${{ secrets.CONTAINER_SQL_PASSWORD }}
ports:
- 1433:1433
steps:
- uses: actions/checkout@v4
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x
- name: Install dependencies
run: |
dotnet tool install -g microsoft.sqlpackage
dotnet tool install -g dotnet-ef
dotnet new install microsoft.build.sql.templates
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
brew install sqlcmd
- name: Create SQL project and add extra code analysis rule/rules
run: |
dotnet new sqlproj -n ${{ env.DB_FOLDER }}
cd ${{ env.DB_FOLDER }}
dotnet add package DrewSK.AvoidCharMax --version 1.0.0
- name: Create DB
run: |
set +o pipefail +e
for i in {1..60};
do
sqlcmd -S localhost -U sa -P ${{ secrets.CONTAINER_SQL_PASSWORD }} -d master -Q "select getdate()"
if [ $? -eq 0 ]
then
echo "sql server ready"
break
else
echo "not ready yet..."
sleep 1
fi
done
sqlcmd -S localhost -U sa -P ${{ secrets.CONTAINER_SQL_PASSWORD }} -d master -Q "CREATE DATABASE [${{ env.DB_FOLDER }}]"
set -o pipefail -e
- name: Deploy EF migrations
working-directory: ContosoUniversity
run: |
dotnet ef database update
- name: Extract SQL objects as scripts
working-directory: ${{ env.DB_FOLDER }}
run: |
sqlpackage /Action:Extract /SourceConnectionString:"${{ env.ConnectionStrings__SchoolContext }}" /TargetFile:"objects" /p:ExtractTarget=SchemaObjectType
- name: Restore dependencies
working-directory: ${{ env.DB_FOLDER }}
run: dotnet restore
- name: Build
working-directory: ${{ env.DB_FOLDER }}
run: dotnet build --no-restore /p:RunSqlCodeAnalysis=true
- name: Evaluate code analysis output
id: sql_code_analysis
shell: pwsh
run: |
$sqlCodeAnalysisFindings = .github/utilityscripts/CodeAnalysisOutput.ps1 "${{ env.DB_FOLDER }}/bin/Debug/${{ env.DB_FOLDER }}.StaticCodeAnalysis.Results.xml"
$analysisErrors = $sqlCodeAnalysisFindings.ErrorCount
$analysisWarnings = $sqlCodeAnalysisFindings.WarningCount
echo "analysisErrors=$analysisErrors" >> $env:GITHUB_OUTPUT
echo "analysisWarnings=$analysisWarnings" >> $env:GITHUB_OUTPUT
cat CodeAnalysisOutput.md >> $env:GITHUB_STEP_SUMMARY
- name: Archive the database model (.dacpac)
uses: actions/upload-artifact@v4
with:
name: ${{ env.DB_FOLDER }}
path: ${{ env.DB_FOLDER }}/bin/Debug/${{ env.DB_FOLDER }}.dacpac
- name: Generate deploy script
run: |
sqlpackage /Action:Script /SourceFile:"${{ env.DB_FOLDER }}/bin/Debug/${{ env.DB_FOLDER }}.dacpac" /TargetConnectionString:"${{ secrets.SQLDB_CONNECTION_STRING }}" /DeployScriptPath:DeployScript.sql /p:AllowIncompatiblePlatform=true
- name: Archive the deploy script
uses: actions/upload-artifact@v4
with:
name: DeployScript
path: DeployScript.sql
- name: Set job as failed if at a certain number of code analysis errors or warnings
if: ${{ steps.sql_code_analysis.outputs.analysisErrors != 0 || steps.sql_code_analysis.outputs.analysisWarnings > 2 }}
uses: actions/github-script@v7
with:
script: |
core.setFailed('Code analysis results over threshold. Please check the output to improve database quality.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment