Allow VSTS to update test database

百般思念 提交于 2019-12-01 14:21:27
starian chen-MSFT

You can add and remove firewall rule by calling New-AzureRmSqlServerFirewallRule and Remove-AzureRmSqlServerFirewallRule powershell command.

Refer to these thread below to do it during the build/release: Deploy Dacpac packages via power shell script to Azure SQL Server

First, you need to add firewall rule in order to connect to Azure SQL Server.

1.Edit your build definition

2.Select Option tab and check Allow Scripts to Access OAuth Token

3.Add Azure PowerShell step (arguments: -RestAddress https://[account].vsdtl.visualstudio.com/DefaultCollection/_apis/vslabs/ipaddress -Token $(System.AccessToken) -RG [resource group] -Server [server name] -ruleName $(Build.BuildNumber)

Code:

param (
    [string]$RestAddress,
    [string]$Token,
    [string]$RG,
    [string]$Server
    )
$basicAuth = ("{0}:{1}" -f 'test',$Token)
$basicAuth = [System.Text.Encoding]::UTF8.GetBytes($basicAuth)
$basicAuth = [System.Convert]::ToBase64String($basicAuth)
$headers = @{Authorization=("Basic {0}" -f $basicAuth)}
$result = Invoke-RestMethod -Uri $RestAddress -headers $headers -Method Get
Write-Host $result.value
New-AzureRmSqlServerFirewallRule -ResourceGroupName $RG -ServerName $Server -FirewallRuleName "UnitTestRule" -StartIpAddress "$($result.value)" -EndIpAddress "$($result.value)"  

Update:

Allow Scripts to Access OAuth Token for release:

  1. Edit release definition
  2. Click Run On Agent
  3. Check Allow Scripts to Access OAuth Token option

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!