Tom W
Tom W

Reputation: 43

How do I trigger a failure in azure pipelines when a bicep template build fails?

I using a bicep template deployment in Azure pipelines and I need to retreive the output from the build for subsequent tasks. Unfortunately, when I pass the output to the variable and the build fails, then it's not triggering a failure for the whole task.

- task: AzureCLI@2
  displayName: 'Deploy resource group'
  name: resourceGroup
  inputs:
    azureSubscription: 'ARM'
    scriptType: bash
    scriptLocation: inlineScript
    addSpnToEnvironment: true
    inlineScript: |
      resources=$(az deployment sub create \
        --name blog \
        --location WestUS2 \
        --template-file lib/infra/main.bicep | jq -r '.properties.outputs')
      echo "##vso[task.setvariable variable=apiUrl;isOutput=true]$(echo $resources | jq '.apiUrl.value')"
      echo "##vso[task.setvariable variable=staticUrl;isOutput=true]$(echo $resources | jq '.staticUrl.value')"

So how can I trigger a build failure in this scenario? Is there a way to read the error from the $resources variable and trigger the task failure manually?

Upvotes: 2

Views: 59

Answers (1)

Rui Jarimba
Rui Jarimba

Reputation: 18094

Try setting failOnStandardError property to true:

- task: AzureCLI@2
  displayName: 'Deploy resource group'
  name: resourceGroup
  inputs:
    # ...
    failOnStandardError: true
    # ...

If changing the script type to Powershell (pscore) is not a problem, you can also use the $? variable to see if the previous command failed:

az deployment sub create ...

if ($? -eq $false) {
  throw 'Deployment failed.'
}

See:

Upvotes: 0

Related Questions