使用 Azure DevOps 管道和版本部署虚拟助手
Deploy Virtual Assistant using Azure DevOps pipelines and releases
我正在尝试创建一种简单的方法,让产品所有者站起来拆除虚拟助手所需的 Azure 资源。但是,部署脚本 deploy.ps1
需要 PowerShell 6+,它似乎不适用于 Azure DevOps。
最好的方法是什么?
描述您想要的解决方案
目标是使用发布管道自动建立机器人运行所需的 Azure 资源。
想法
- 有没有办法通过 Azure PowerShell 使用 PowerShell 6+?
- 我们不应该使用
deploy.ps1
在 DevOps 发布管道中部署资源吗?
代码
这里是 deploy.ps1
的内容和一个 link to the latest.
#Requires -Version 6
Param(
[string] $name,
[string] $resourceGroup,
[string] $location,
[string] $appId,
[string] $appPassword,
[string] $luisAuthoringKey,
[string] $luisAuthoringRegion,
[string] $parametersFile,
[string] $languages = "en-us",
[string] $outFolder = $(Get-Location),
[string] $logFile = $(Join-Path $PSScriptRoot .. "deploy_log.txt")
)
# Reset log file
if (Test-Path $logFile) {
Clear-Content $logFile -Force | Out-Null
}
else {
New-Item -Path $logFile | Out-Null
}
# Get mandatory parameters
if (-not $name) {
$name = Read-Host "? Bot Name (used as default name for resource group and deployed resources)"
}
if (-not $resourceGroup) {
$resourceGroup = $name
}
if (-not $location) {
$location = Read-Host "? Azure resource group region"
}
if (-not $appPassword) {
$appPassword = Read-Host "? Password for MSA app registration (must be at least 16 characters long, contain at least 1 special character, and contain at least 1 numeric character)"
}
if (-not $luisAuthoringRegion) {
$luisAuthoringRegion = Read-Host "? LUIS Authoring Region (westus, westeurope, or australiaeast)"
}
if (-not $luisAuthoringKey) {
Switch ($luisAuthoringRegion) {
"westus" {
$luisAuthoringKey = Read-Host "? LUIS Authoring Key (found at https://luis.ai/user/settings)"
Break
}
"westeurope" {
$luisAuthoringKey = Read-Host "? LUIS Authoring Key (found at https://eu.luis.ai/user/settings)"
Break
}
"australiaeast" {
$luisAuthoringKey = Read-Host "? LUIS Authoring Key (found at https://au.luis.ai/user/settings)"
Break
}
default {
Write-Host "! $($luisAuthoringRegion) is not a valid LUIS authoring region." -ForegroundColor DarkRed
Break
}
}
if (-not $luisAuthoringKey) {
Break
}
}
if (-not $appId) {
# Create app registration
$app = (az ad app create `
--display-name $name `
--password $appPassword `
--available-to-other-tenants `
--reply-urls 'https://token.botframework.com/.auth/web/redirect')
# Retrieve AppId
if ($app) {
$appId = ($app | ConvertFrom-Json) | Select-Object -ExpandProperty appId
}
if(-not $appId) {
Write-Host "! Could not provision Microsoft App Registration automatically. Review the log for more information." -ForegroundColor DarkRed
Write-Host "! Log: $($logFile)" -ForegroundColor DarkRed
Write-Host "+ Provision an app manually in the Azure Portal, then try again providing the -appId and -appPassword arguments. See https://aka.ms/vamanualappcreation for more information." -ForegroundColor Magenta
Break
}
}
# Get timestamp
$timestamp = Get-Date -f MMddyyyyHHmmss
# Create resource group
Write-Host "> Creating resource group ..."
(az group create --name $name --location $location) 2>> $logFile | Out-Null
# Deploy Azure services (deploys LUIS, QnA Maker, Content Moderator, CosmosDB)
Write-Host "> Deploying Azure services (this could take a while)..." -ForegroundColor Yellow
if ($parametersFile) {
(az group deployment create `
--name $timestamp `
--resource-group $resourceGroup `
--template-file "$(Join-Path $PSScriptRoot '..' 'Resources' 'template.json')" `
--parameters "@$($parametersFile)" `
--parameters microsoftAppId=$appId microsoftAppPassword="`"$($appPassword)`"") 2>> $logFile | Out-Null
}
else {
(az group deployment create `
--name $timestamp `
--resource-group $resourceGroup `
--template-file "$(Join-Path $PSScriptRoot '..' 'Resources' 'template.json')" `
--parameters microsoftAppId=$appId microsoftAppPassword="`"$($appPassword)`"") 2>> $logFile | Out-Null
}
# Get deployment outputs
$outputs = (az group deployment show `
--name $timestamp `
--resource-group $resourceGroup `
--query properties.outputs)
# If it succeeded then we perform the remainder of the steps
if ($outputs)
{
# Log and convert to JSON
$outputs >> $logFile
$outputs = $outputs | ConvertFrom-Json
# Update appsettings.json
Write-Host "> Updating appsettings.json ..."
if (Test-Path $(Join-Path $outFolder appsettings.json)) {
$settings = Get-Content $(Join-Path $outFolder appsettings.json) | ConvertFrom-Json
}
else {
$settings = New-Object PSObject
}
$settings | Add-Member -Type NoteProperty -Force -Name 'microsoftAppId' -Value $appId
$settings | Add-Member -Type NoteProperty -Force -Name 'microsoftAppPassword' -Value $appPassword
if ($outputs.appInsights) { $settings | Add-Member -Type NoteProperty -Force -Name 'appInsights' -Value $outputs.appInsights.value }
if ($outputs.storage) { $settings | Add-Member -Type NoteProperty -Force -Name 'blobStorage' -Value $outputs.storage.value }
if ($outputs.cosmosDb) { $settings | Add-Member -Type NoteProperty -Force -Name 'cosmosDb' -Value $outputs.cosmosDb.value }
if ($outputs.contentModerator) { $settings | Add-Member -Type NoteProperty -Force -Name 'contentModerator' -Value $outputs.contentModerator.value }
$settings | ConvertTo-Json -depth 100 | Out-File $(Join-Path $outFolder appsettings.json)
# Delay to let QnA Maker finish setting up
Start-Sleep -s 30
# Deploy cognitive models
Invoke-Expression "$(Join-Path $PSScriptRoot 'deploy_cognitive_models.ps1') -name $($name) -luisAuthoringRegion $($luisAuthoringRegion) -luisAuthoringKey $($luisAuthoringKey) -qnaSubscriptionKey $($outputs.qnaMaker.value.key) -outFolder $($outFolder) -languages `"$($languages)`""
Write-Host "> Done."
}
else
{
# Check for failed deployments
$operations = az group deployment operation list -g $resourceGroup -n $timestamp | ConvertFrom-Json
$failedOperations = $operations | Where { $_.properties.statusmessage.error -ne $null }
if ($failedOperations) {
foreach ($operation in $failedOperations) {
switch ($operation.properties.statusmessage.error.code) {
"MissingRegistrationForLocation" {
Write-Host "! Deployment failed for resource of type $($operation.properties.targetResource.resourceType). This resource is not avaliable in the location provided." -ForegroundColor DarkRed
Write-Host "+ Update the .\Deployment\Resources\parameters.template.json file with a valid region for this resource and provide the file path in the -parametersFile parameter." -ForegroundColor Magenta
}
default {
Write-Host "! Deployment failed for resource of type $($operation.properties.targetResource.resourceType)."
Write-Host "! Code: $($operation.properties.statusMessage.error.code)."
Write-Host "! Message: $($operation.properties.statusMessage.error.message)."
}
}
}
Write-Host "+ To delete this resource group, run 'az group delete -g $($resourceGroup) --no-wait'" -ForegroundColor Magenta
Break
}
}
设置屏幕截图
正在尝试 运行 Azure PowerShell
当我尝试使用 Azure PowerShell 运行 脚本时,我收到以下错误。
##[error]The script 'deploy.ps1' cannot be run because it contained a "#requires" statement for Windows PowerShell 6.0. The version of Windows PowerShell that is required by the script does not match the currently running version of Windows PowerShell 5.1.17763.316.
这是 Azure PowerShell Version
设置为 Latest installed version
。使用 Specify other version
和 6.7.0
、6.2.1
或 6.0.0
也不起作用。 None 其中似乎确实有效果。它总是以 5.1.17763.316
.
的形式返回
首先,您的脚本正在使用 Read-Host
,这在管道中不可用。构建代理确实有 pwsh(因此 Powershell 6.0 +),但您的脚本根本不使用 Azure Powershell,它使用 Azure CLI(也可在构建代理上使用)。
一般来说,您需要为管道重构脚本,它就可以正常工作。
Build Agents 有 PSv6,他们也有 PSv5
每个代理上的软件列于:https://github.com/Microsoft/azure-pipelines-image-generation/tree/master/images/win
这里的问题是任务正在使用 "Azure Powershell",它使用 PS v5 cmdlet,因此将强制使用 PSv5 (powershell.exe) 而不是PSv6 (pwsh.exe).
如果您要使用正常的 "Powershell" 任务和 select "Use Powershell Core",您将很难(或不安全的时间)登录。
对于这种情况,您需要从 Azure CLI 任务执行 PSv6 脚本。为此,只需添加 Azure CLI 任务并执行下面的内联脚本(修复您发布的路径和参数)。
pwsh -File ${System.DefaultWorkingDirectory}\<some-path-to>\deploy.ps1 -script-arguments-copy-pasted-here
注意:唯一可行的方法是,如果传递了所有参数并且未命中 read-host,如 4c74356b41 所述,您将无法从管道获取输入。
我正在尝试创建一种简单的方法,让产品所有者站起来拆除虚拟助手所需的 Azure 资源。但是,部署脚本 deploy.ps1
需要 PowerShell 6+,它似乎不适用于 Azure DevOps。
最好的方法是什么?
描述您想要的解决方案
目标是使用发布管道自动建立机器人运行所需的 Azure 资源。
想法
- 有没有办法通过 Azure PowerShell 使用 PowerShell 6+?
- 我们不应该使用
deploy.ps1
在 DevOps 发布管道中部署资源吗?
代码
这里是 deploy.ps1
的内容和一个 link to the latest.
#Requires -Version 6
Param(
[string] $name,
[string] $resourceGroup,
[string] $location,
[string] $appId,
[string] $appPassword,
[string] $luisAuthoringKey,
[string] $luisAuthoringRegion,
[string] $parametersFile,
[string] $languages = "en-us",
[string] $outFolder = $(Get-Location),
[string] $logFile = $(Join-Path $PSScriptRoot .. "deploy_log.txt")
)
# Reset log file
if (Test-Path $logFile) {
Clear-Content $logFile -Force | Out-Null
}
else {
New-Item -Path $logFile | Out-Null
}
# Get mandatory parameters
if (-not $name) {
$name = Read-Host "? Bot Name (used as default name for resource group and deployed resources)"
}
if (-not $resourceGroup) {
$resourceGroup = $name
}
if (-not $location) {
$location = Read-Host "? Azure resource group region"
}
if (-not $appPassword) {
$appPassword = Read-Host "? Password for MSA app registration (must be at least 16 characters long, contain at least 1 special character, and contain at least 1 numeric character)"
}
if (-not $luisAuthoringRegion) {
$luisAuthoringRegion = Read-Host "? LUIS Authoring Region (westus, westeurope, or australiaeast)"
}
if (-not $luisAuthoringKey) {
Switch ($luisAuthoringRegion) {
"westus" {
$luisAuthoringKey = Read-Host "? LUIS Authoring Key (found at https://luis.ai/user/settings)"
Break
}
"westeurope" {
$luisAuthoringKey = Read-Host "? LUIS Authoring Key (found at https://eu.luis.ai/user/settings)"
Break
}
"australiaeast" {
$luisAuthoringKey = Read-Host "? LUIS Authoring Key (found at https://au.luis.ai/user/settings)"
Break
}
default {
Write-Host "! $($luisAuthoringRegion) is not a valid LUIS authoring region." -ForegroundColor DarkRed
Break
}
}
if (-not $luisAuthoringKey) {
Break
}
}
if (-not $appId) {
# Create app registration
$app = (az ad app create `
--display-name $name `
--password $appPassword `
--available-to-other-tenants `
--reply-urls 'https://token.botframework.com/.auth/web/redirect')
# Retrieve AppId
if ($app) {
$appId = ($app | ConvertFrom-Json) | Select-Object -ExpandProperty appId
}
if(-not $appId) {
Write-Host "! Could not provision Microsoft App Registration automatically. Review the log for more information." -ForegroundColor DarkRed
Write-Host "! Log: $($logFile)" -ForegroundColor DarkRed
Write-Host "+ Provision an app manually in the Azure Portal, then try again providing the -appId and -appPassword arguments. See https://aka.ms/vamanualappcreation for more information." -ForegroundColor Magenta
Break
}
}
# Get timestamp
$timestamp = Get-Date -f MMddyyyyHHmmss
# Create resource group
Write-Host "> Creating resource group ..."
(az group create --name $name --location $location) 2>> $logFile | Out-Null
# Deploy Azure services (deploys LUIS, QnA Maker, Content Moderator, CosmosDB)
Write-Host "> Deploying Azure services (this could take a while)..." -ForegroundColor Yellow
if ($parametersFile) {
(az group deployment create `
--name $timestamp `
--resource-group $resourceGroup `
--template-file "$(Join-Path $PSScriptRoot '..' 'Resources' 'template.json')" `
--parameters "@$($parametersFile)" `
--parameters microsoftAppId=$appId microsoftAppPassword="`"$($appPassword)`"") 2>> $logFile | Out-Null
}
else {
(az group deployment create `
--name $timestamp `
--resource-group $resourceGroup `
--template-file "$(Join-Path $PSScriptRoot '..' 'Resources' 'template.json')" `
--parameters microsoftAppId=$appId microsoftAppPassword="`"$($appPassword)`"") 2>> $logFile | Out-Null
}
# Get deployment outputs
$outputs = (az group deployment show `
--name $timestamp `
--resource-group $resourceGroup `
--query properties.outputs)
# If it succeeded then we perform the remainder of the steps
if ($outputs)
{
# Log and convert to JSON
$outputs >> $logFile
$outputs = $outputs | ConvertFrom-Json
# Update appsettings.json
Write-Host "> Updating appsettings.json ..."
if (Test-Path $(Join-Path $outFolder appsettings.json)) {
$settings = Get-Content $(Join-Path $outFolder appsettings.json) | ConvertFrom-Json
}
else {
$settings = New-Object PSObject
}
$settings | Add-Member -Type NoteProperty -Force -Name 'microsoftAppId' -Value $appId
$settings | Add-Member -Type NoteProperty -Force -Name 'microsoftAppPassword' -Value $appPassword
if ($outputs.appInsights) { $settings | Add-Member -Type NoteProperty -Force -Name 'appInsights' -Value $outputs.appInsights.value }
if ($outputs.storage) { $settings | Add-Member -Type NoteProperty -Force -Name 'blobStorage' -Value $outputs.storage.value }
if ($outputs.cosmosDb) { $settings | Add-Member -Type NoteProperty -Force -Name 'cosmosDb' -Value $outputs.cosmosDb.value }
if ($outputs.contentModerator) { $settings | Add-Member -Type NoteProperty -Force -Name 'contentModerator' -Value $outputs.contentModerator.value }
$settings | ConvertTo-Json -depth 100 | Out-File $(Join-Path $outFolder appsettings.json)
# Delay to let QnA Maker finish setting up
Start-Sleep -s 30
# Deploy cognitive models
Invoke-Expression "$(Join-Path $PSScriptRoot 'deploy_cognitive_models.ps1') -name $($name) -luisAuthoringRegion $($luisAuthoringRegion) -luisAuthoringKey $($luisAuthoringKey) -qnaSubscriptionKey $($outputs.qnaMaker.value.key) -outFolder $($outFolder) -languages `"$($languages)`""
Write-Host "> Done."
}
else
{
# Check for failed deployments
$operations = az group deployment operation list -g $resourceGroup -n $timestamp | ConvertFrom-Json
$failedOperations = $operations | Where { $_.properties.statusmessage.error -ne $null }
if ($failedOperations) {
foreach ($operation in $failedOperations) {
switch ($operation.properties.statusmessage.error.code) {
"MissingRegistrationForLocation" {
Write-Host "! Deployment failed for resource of type $($operation.properties.targetResource.resourceType). This resource is not avaliable in the location provided." -ForegroundColor DarkRed
Write-Host "+ Update the .\Deployment\Resources\parameters.template.json file with a valid region for this resource and provide the file path in the -parametersFile parameter." -ForegroundColor Magenta
}
default {
Write-Host "! Deployment failed for resource of type $($operation.properties.targetResource.resourceType)."
Write-Host "! Code: $($operation.properties.statusMessage.error.code)."
Write-Host "! Message: $($operation.properties.statusMessage.error.message)."
}
}
}
Write-Host "+ To delete this resource group, run 'az group delete -g $($resourceGroup) --no-wait'" -ForegroundColor Magenta
Break
}
}
设置屏幕截图
正在尝试 运行 Azure PowerShell
当我尝试使用 Azure PowerShell 运行 脚本时,我收到以下错误。
##[error]The script 'deploy.ps1' cannot be run because it contained a "#requires" statement for Windows PowerShell 6.0. The version of Windows PowerShell that is required by the script does not match the currently running version of Windows PowerShell 5.1.17763.316.
这是 Azure PowerShell Version
设置为 Latest installed version
。使用 Specify other version
和 6.7.0
、6.2.1
或 6.0.0
也不起作用。 None 其中似乎确实有效果。它总是以 5.1.17763.316
.
首先,您的脚本正在使用 Read-Host
,这在管道中不可用。构建代理确实有 pwsh(因此 Powershell 6.0 +),但您的脚本根本不使用 Azure Powershell,它使用 Azure CLI(也可在构建代理上使用)。
一般来说,您需要为管道重构脚本,它就可以正常工作。
Build Agents 有 PSv6,他们也有 PSv5 每个代理上的软件列于:https://github.com/Microsoft/azure-pipelines-image-generation/tree/master/images/win
这里的问题是任务正在使用 "Azure Powershell",它使用 PS v5 cmdlet,因此将强制使用 PSv5 (powershell.exe) 而不是PSv6 (pwsh.exe).
如果您要使用正常的 "Powershell" 任务和 select "Use Powershell Core",您将很难(或不安全的时间)登录。
对于这种情况,您需要从 Azure CLI 任务执行 PSv6 脚本。为此,只需添加 Azure CLI 任务并执行下面的内联脚本(修复您发布的路径和参数)。
pwsh -File ${System.DefaultWorkingDirectory}\<some-path-to>\deploy.ps1 -script-arguments-copy-pasted-here
注意:唯一可行的方法是,如果传递了所有参数并且未命中 read-host,如 4c74356b41 所述,您将无法从管道获取输入。