Closed SDedik closed 3 years ago
Multiline strings like the one in your example are not valid in JSON, a JSON parser/editor will highlight the error. Multilple lines need to be separated by \n
. You will not face issues with a valid JSON template file.
The task does JSON parsing to determine that the template is a valid JSON. The error you see is thrown during validations done by the task before the deployment is don, not during deployment. It appears that az cli and az powershell might not make this check, so the deployment succeeds.
This is by design and not a bug in the task, so will close it.
Please see the https://docs.microsoft.com/en-us/azure/azure-resource-manager/templates/template-syntax#multi-line-strings
Multiline strings are supported in ARM template json. Plus, applying the template via PowerShell cmdlet works without any problem, proving once again that json is valid. Also, see very related issue reported here: https://github.com/microsoft/azure-pipelines-tasks/issues/14268 which is basically the same issue, but reported for a different Task.
The error you see is thrown during validations done by the task before the deployment is don, not during deployment.
Exactly. During, or to be more precise, right before the deployment the json is validated once again by the Azure Deployment engine (Resource Manager?). So why validate it twice: by the task and by Azure itself? Isn't Azure knows better what is valid and what is not?
@SDedik could you please attach debug logs for the failure
Sure thing, please see full logs attached below
Same with comments. It fails if there are comments in document which are not allowed in JSON but allowed in JSONC
Hi @SDedik and @artisticcheese yes, you are right. This is the limitation of task as for parsing and validation csm file we are using javascript JSON.parse which expects valid JSON. The multiline string and comments is supported in the latest version of ARM templates as mentioned in this doc that you must use the --handle-extended-json-format switch in Azure CLI version above 2.3.0
@anuragc617, the root cause of the failure is well understood, as we as possible workarounds like switching to AzureCLI task or AzurePowerShell task. However... Think of it this way. I have an ARM template file which can be fed to the Azure Resource Manager via the Azure Portal, Azure API, Azure PowerShell and Azure CLI and it works as intended with all of the above. But, when I try to use the AzureResourceManagerTemplateDeployment task, the task fails. Do you acknowledge that this is a valid use-case and with such the behavior of the AzureResourceManagerTemplateDeployment task is a bug? What is the correct way of addressing this bug is a next question, but the fact that it needs to be addressed is of no doubt.
Also, you made a mistake in your post here:
you must use the --handle-extended-json-format switch in Azure CLI version above 2.3.0
According to doc provided it should be Azure CLI version 2.3.0 and below. Thus emphasizing that current versions of Azure CLI supports templates with multiline strings natively without additional switches. To quote the doc:
To deploy templates with multi-line strings by using Azure CLI with version 2.3.0 or older, you must use the --handle-extended-json-format switch.
In addition to that please at least provide better explanation for failures. It tooks me hours to figure out what the issue is since error message so non-descriptive
@SDedik yes, we acknowledge that this is a bug in AzureResourceManagerTemplateDeployment task. We have started internal discussion on how to address this bug.
Sorry, my bad, yes it is t should be Azure CLI version 2.3.0 and below
@artisticcheese we will look at it as well if we can provide better message while error in template parsing
The linked documentation is really bad and does not describe that multiline JSON support is done on the client-side as a template preprocessing step.
That is the reason why deploying templates with multiline strings or comments works with Az CLI or with Powershell. The documentation should clearly state that other deployment methods will FAIL:
Every ADO task doing ARM deployment should document that multiline and JSON comments are unsupported if they are unsupported. Also, the main documentation site MUST clearly state that multiline and JSON comments are selected client-only features. The current wording is really misleading.
edit: to make thing even worse this task strips out JSON comments before deployment and therefore only part of the extended JSON features are supported and is fully undocumented.
@jikuja You are saying that powershell cmdlet before submitting deployment to Azure backplane transpiles multi-line string into single line string?
Might be. Not sure what is the state of the current code but clients usually requires special code to handle JSON comments and/or multiline strings if they are parsing template file instead of passing contents of a file as a string to API.
Requires more testing by end-users for end-users to understand the current situation OR serious documentation updates.
Multiline strings are not supported if you are deploying through Azure portal, a DevOps pipeline, or the REST API https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/syntax#multi-line-strings
Required Information
Type: Bug
Enter Task Name: AzureResourceManagerTemplateDeployment@3
Environment
Issue Description
Consider following very minimal ARM template
poc.json
:and Azure Pipeline to run it:
Pipeline fails with the following error message **##[error]Check out the troubleshooting guide to see if your issue is addressed: https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/deploy/azure-resource-group-deployment?view=azure-devops#troubleshooting
[error]Error: Ensure the Template file ( '/home/vsts/work/1/s/poc.json' ) is valid. Task failed while parsing with following error: Unexpected token
in JSON at position 321**
Running the template manually from PowerShell like this works without any problems
New-AzSubscriptionDeployment -Location 'eastus' -TemplateFile .\poc.json
As soon as the parameter description in the ARM template is changed from multiline string to single line like so
the pipeline task works as expected