簡體   English   中英

使用Azure DevOps管道和版本部署Virtual Assistant

[英]Deploy Virtual Assistant using Azure DevOps pipelines and releases

我正在嘗試為產品所有者創建一種簡單的方法,以站起來並拆除Virtual Assistant所需的Azure資源。 但是,部署腳本deploy.ps1需要PowerShell 6+,而Azure DevOps似乎不提供。

最好的進行方法是什么?

描述您想要的解決方案

目標是使用發布管道來自動站起機器人運行所需的Azure資源。

思考

  • 是否可以使用Azure PowerShell使用PowerShell 6+?
  • 我們是否不應該使用deploy.ps1在DevOps發布管道中部署資源?

這是deploy.ps1的內容,以及指向 deploy.ps1鏈接

#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 version6.7.06.2.16.0.0也不起作用。 這些似乎都沒有實際作用。 它總是以5.1.17763.316

首先,您的腳本使用的是Read-Host ,它在管道中不可用。 生成代理確實具有pwsh(因此是Powershell 6.0 +),但是您的腳本根本沒有使用Azure Powershell,而是使用Azure CLI(在生成代理上也可用)。

通常,您需要為管道重構腳本,它可以正常工作。

Build Agent確實具有PSv6,它們在每個Agent上也具有PSv5軟件,列在: https : //github.com/Microsoft/azure-pipelines-image-generation/tree/master/images/win

這里的問題是任務正在使用使用PS v5 cmdlet的“ Azure Powershell”,因此將強制使用PSv5(powershell.exe)而不是PSv6(pwsh.exe)。

如果要使用常規的“ Powershell”任務並選擇“使用Powershell Core”,則登錄將很困難(或不安全)。

對於這種情況,您需要從Azure CLI任務執行PSv6腳本。 為此,只需添加Azure CLI任務並執行下面的內聯腳本(將路徑和參數固定為所發布的內容)。

pwsh -File ${System.DefaultWorkingDirectory}\<some-path-to>\deploy.ps1 -script-arguments-copy-pasted-here

注意:如4c74356b41所述,唯一可行的方法是傳遞所有參數並且未命中讀取主機,則無法從管道中獲取輸入。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM