How to create resources in Azure Portal using Devops CI/CD pipeline and ARM Template

Viewed 186

I am working on creating qnamaker based Chatbot and for this I have prepared ARM template for creating and deploying resources.

PowerShell script is being used to create knowledge base once all the resources are created.

This process works just fine when I am executing PowerShell script from local PowerShell tool.

Now I want to do CI/CD for this deployment process so that deployment process can be automated

Creating resources and deploying to azure through Pipeline is quite possible through Azure ARM Template deployment task but I am not sure how to execute PowerShell script that is responsible for creating knowledge base once resources are created based on recently created qnamaker service

Any help greatly appreciated

2 Answers

Please clarify that your issue is creating and training the QnA Maker KB after the actual resources have been created by ARM? There are probably multiple ways to do this, but here is what I have done. First of all, I am using the Azure CLI Task version 2.* which simplifies some of the initial steps. You could probably use regular PowerShell for some of the subsequent steps. I have selected all as PowerShell script types and inline scripts. Each of these segments I do as a separate task but you could probably do all as one. I like being able to see which step fails if something happens, though.

First you need to get the key for your cognitive service

$QNAKEY= & az cognitiveservices account keys list -g "YOUR_RESOURCE_GROUP" --name "YOUR_QNA_RESOURCE_NAME" --query key1 -o tsv
Write-Output("##vso[task.setvariable variable=QNA_KEY;]$QNAKEY")

Next you need to create the KB. I have mine seeded with information via a json file in my project repository. You can do this from wherever, or just create a blank KB (I think). Note I'm checking here to see if the KB exists; KB name does not have to be unique, so if you don't do this you will end up creating a lot of duplicate KBs.

$header = @{
  "Content-Type"="application/json"
  "Ocp-Apim-Subscription-Key"="$(QNA_KEY)"
}

$res = Invoke-RestMethod -Uri "https://westus.api.cognitive.microsoft.com/qnamaker/v4.0/knowledgebases" -Method 'Get' -Headers $header

$kb = $res.knowledgebases | Where-Object name -eq "YOUR_KB_NAME"

Write-Host $res.knowledgebases
Write-Host $kb

if (!$kb) {
    Write-Host "KB does not exist, so creating new KB"
    $body = Get-Content '$(System.DefaultWorkingDirectory)/PATH_TO_MY_MODEL.json' | Out-String
    Invoke-RestMethod -Uri "https://westus.api.cognitive.microsoft.com/qnamaker/v4.0/knowledgebases/create" -Method 'Post' -Body $body -Headers $header
}

Finally, you will likely want to publish your KB. With LUIS I think you need to train it first (via separate CLI task), but QnA Maker you should be able to publish directly. I do this as a dependent stage to ensure the KB is created before I try to publish.

$QNAKEY= & az cognitiveservices account keys list -g "YOUR_RESOURCE_GROUP" --name "YOUR_QNA_RESOURCE_NAME" --query key1 -o tsv

$header = @{
  "Content-Type"="application/json"
  "Ocp-Apim-Subscription-Key"="$QNAKEY"
}

$res = Invoke-RestMethod -Uri "https://westus.api.cognitive.microsoft.com/qnamaker/v4.0/knowledgebases" -Method 'Get' -Headers $header

$kb = $res.knowledgebases | Where-Object name -eq "YOUR_KB_NAME"
$qnaId = $kb.id

Invoke-RestMethod -Uri "https://westus.api.cognitive.microsoft.com/qnamaker/v4.0/knowledgebases/$qnaId" -Method 'Post' -Headers $header

And that's it! At this point your QnA Maker knowledgebase should be created, published, and ready to go.

Related