Set up continuous integration using PowerShell
Published 29 March 2016
This tutorial shows how to use the DLM Automation PowerShell cmdlets to performs continuous integration tasks on the script folder in your source control repository. This can be done from the PowerShell console or another PowerShell runner (for example your build server). In this tutorial we will use the console but this can easily be applied to your build server or similar.
The cmdlets that we'll use will perform the following tasks:
- Validate: Validate the schema in the source control scripts folder by checking the database can be built from scratch successfully.
- Test: Run tSQLt tests on the database schema.
- Sync: Update a test database to match the database schema.
Before you start
- Install DLM Automation. For more information, see Installing.
- Get a copy of your database scripts folder, you can either:
- checkout a copy from your database source controlled using SQL Source Control
- generate a folder from an existing database using SQL Compare
- download and unzip WidgetShop.zip
- Create a blank database that we can update.
Performing Continuous Integration tasks
In this step, you run a PowerShell script to perform all the continuous integration tasks:
From the Start menu, open Windows PowerShell ISE.
By default, PowerShell doesn't let you run saved scripts. If you need to do this, you may have to change your security policy.
You only need to do this once.
In the top pane of PowerShell ISE, paste this PowerShell script:
Validating the scripts folder
$errorActionPreference = "stop" # Validate the scripts folder $scriptsFolder = "C:\Work\scripts" # The scripts folder to validate, test and sync $validatedScriptsFolder = Invoke-DlmDatabaseSchemaValidation $scriptsFolder
Update $scriptsFolder to the location of the scripts folder you created earlier.
- This will check that your scripts folder is valid and can be used to produce a database.
In top pane of PowerShell ISE add the following to the end of the script:
Test the scripts folder
# Run tSQLt tests $testResults = Invoke-DlmDatabaseTests $validatedScriptsFolder Export-DlmDatabaseTestResults $testResults -OutputFile "C:\Work\TestResults\scripts.junit.xml"
- This will run any tests in your database and store them in the $testResults variable and the file "C:\Work\TestResults\scripts.junit.xml" for review.
Add these lines to the end of your script:
Syncing to a test database
# Sync a test database $testdb = New-DlmDatabaseConnection -ServerInstance ".\sql2012" -Database "TestDatabase" # Update this to use the blank database you created earlier Test-DlmDatabaseConnection $testdb Sync-DlmDatabaseSchema -Source $validatedScriptsFolder -Target $testdb
Update the connection details to the blank database you created earlier. If using SQL Server Authentication, add the desired credentials using the -Username and -Password parameters.
- This will update the test database to match your scripts folder.
- Press F5 to run the script.
What next?
To automate continuous integration, run the same PowerShell script featured in this tutorial from a step in your build server that's triggered every time there's a change your development schema. To learn how to do this using TeamCity, see Set up continuous integration with TeamCity.
Cmdlet reference
For full details about all the DLM Automation cmdlets, see the DLM Automation cmdlet reference.