Configure database connection on Azure Pipelines
Published 17 October 2019
This document will guide you through creating a SmartAssembly.settings file as part of the Azure build pipeline to establish a database connection for SmartAssembly.
Connecting SmartAssembly to a database will allow you to access information about each build, even after the build agent is discarded. It will also make it possible to store and access error and feature reports from multiple computers.
Prerequisites
Before following this instruction you should have SmartAssembly already installed in your Azure Pipeline.
Make sure to first read Using SmartAssembly with Azure Pipelines.
Prepare Azure SQL database
If you don't have it already, you need to configure an SQL Server on Azure.
You can also use any other SQL Server instance, as long as it allows for connections both from your local computer (for viewing error reports), and Azure Pipelines (to store details about each build).
If the database with the name declared in the connection string doesn't exist, SmartAssembly will attempt to create it. This may significantly increase the time required to execute SmartAssembly for the first time. Subsequent executions will not be affected.
Configuring Azure Pipelines
Option 1: Configure using the azure-pipelines.yml file
Securely storing database password
- Sign-in to Azure DevOps: https://dev.azure.com/.
- From the main screen choose a project you’re working on.
- From the left-side menu choose Pipelines, then choose an existing pipeline and click Edit in the top-right corner.
- Click Variables in the top-right corner, then a button to add a new variable.
- In the Name field type SA_DB_PASSWORD. This name will later be used to set the connection string without ever disclosing the password.
- In the Value field provide your database password.
- Check Keep this value secret to prevent the password to be shown in Azure GUI and build logs (it will be replaced with ***).
- Click OK.
Set the connection string for SmartAssembly
Add the following task after previously added SmartAssembly installation task.
The task will create a SmartAssembly.settings file with your connection string.
- task: PowerShell@2 displayName: 'SmartAssembly.settings' inputs: targetType: 'inline' script: | ######################## $saDbHost = "YOUR_DATABASE_HOST"; # replace with host of your database server $saDbUser = "YOUR_DATABASE_USER"; # replace with your database username $saDbName = "SmartAssembly"; # database will be created if it doesn't exist ######################## $saDbPass = "$(SA_DB_PASSWORD)"; "<SmartAssembly.Settings><Options><Database ConnectionString='Server=$saDbHost;Initial Catalog=$saDbName;User ID=$saDbUser;Password=$saDbPass' Server='$saDbHost' /></Options></SmartAssembly.Settings>" | Out-File -FilePath "$($env:ProgramData)\Red Gate\SmartAssembly 7\SmartAssembly.settings" -Verbose
Option 2: Configure using the classic editor (without YAML)
Securely storing database password
- Sign-in to Azure DevOps: https://dev.azure.com/.
- From the main screen choose a project you’re working on.
- From the left-side menu choose Pipelines, then choose an existing pipeline and click Edit in the top-right corner.
- Go to Variables tab and make sure Pipeline variables tab is selected on the left-side menu.
- Click Add button below the variables table.
- In the Name column type SA_DB_PASSWORD. This name will later be used to set the connection string without ever disclosing the password.
- In the Value column provide your database password.
- Click on the padlock icon on the right side of the Value column to change variable type to secret, preventing the password to be shown in Azure GUI and build logs (it will be replaced with ***).
- Click Save button located on a toolbar above the variables table.
Set the connection string for SmartAssembly
- On the pipeline edit screen make go to Tasks tab.
- From the list on the left-side of the screen select an agent job responsible for building your project.
- Click on the icon next to the agent job's name.
- Search for PowerShell task, then click Add button. The task will now be added to the tasks list as PowerShell Script.
- Drag the PowerShell Script task and drop it after previously added SmartAssembly installation task.
- With the task selected, change its Name to SmartAssembly.settings.
- Change the Type to Inline.
In the Script field, paste the following PowerShell script:
######################## $saDbHost = "YOUR_DATABASE_HOST"; # replace with host of your database server $saDbUser = "YOUR_DATABASE_USER"; # replace with your database username $saDbName = "SmartAssembly"; # database will be created if it doesn't exist ######################## $saDbPass = "$(SA_DB_PASSWORD)"; "<SmartAssembly.Settings><Options><Database ConnectionString='Server=$saDbHost;Initial Catalog=$saDbName;User ID=$saDbUser;Password=$saDbPass' Server='$saDbHost' /></Options></SmartAssembly.Settings>" | Out-File -FilePath "$($env:ProgramData)\Red Gate\SmartAssembly 7\SmartAssembly.settings" -Verbose
- Click Save button located on a toolbar above the tasks list.