出错时回退以成功部署Rollback on error to successful deployment

部署失败时,可以自动重新部署部署历史记录中先前成功的部署。When a deployment fails, you can automatically redeploy an earlier, successful deployment from your deployment history. 如果基础结构部署存在一个已知良好的状态,并且你希望还原到此状态,则此功能非常有用。This functionality is useful if you've got a known good state for your infrastructure deployment and want to revert to this state. 有许多需要注意的问题和限制:There are a number of caveats and restrictions:

  • 重新部署使用与以前运行它时相同的参数以相同的方式运行。The redeployment is run exactly as it was run previously with the same parameters. 无法更改参数。You can't change the parameters.
  • 以前的部署是使用完整模式运行的。The previous deployment is run using the complete mode. 以前的部署中未包括的任何资源都将被删除,任何资源配置都将设置为以前的状态。Any resources not included in the previous deployment are deleted, and any resource configurations are set to their previous state. 请确保你完全理解部署模式Make sure you fully understand the deployment modes.
  • 重新部署只会影响资源,而不会影响任何数据更改。The redeployment only affects the resources, any data changes aren't affected.
  • 只能将此功能用于资源组部署,而不能用于订阅或管理组级别部署。You can use this feature only with resource group deployments, not subscription or management group level deployments. 有关订阅级部署的详细信息,请参阅在订阅级别创建资源组和资源For more information about subscription level deployment, see Create resource groups and resources at the subscription level.
  • 只能将此选项用于根级别部署。You can only use this option with root level deployments. 从嵌套模板进行的部署不可用于重新部署。Deployments from a nested template aren't available for redeployment.

若要使用此选项,部署必须具有唯一的名称,以便可以在历史记录中标识它们。To use this option, your deployments must have unique names so they can be identified in the history. 如果没有唯一名称,则当前失败的部署可能会覆盖历史记录中以前成功的部署。If you don't have unique names, the current failed deployment might overwrite the previously successful deployment in the history.

PowerShellPowerShell

若要重新部署最后一个成功的部署,请将 -RollbackToLastDeployment 参数添加为标志。To redeploy the last successful deployment, add the -RollbackToLastDeployment parameter as a flag.

New-AzResourceGroupDeployment -Name ExampleDeployment02 `
  -ResourceGroupName $resourceGroupName `
  -TemplateFile c:\MyTemplates\azuredeploy.json `
  -RollbackToLastDeployment

若要重新部署某个特定部署,请使用 -RollBackDeploymentName 参数并提供部署名称。To redeploy a specific deployment, use the -RollBackDeploymentName parameter and provide the name of the deployment. 指定的部署必须已成功。The specified deployment must have succeeded.

New-AzResourceGroupDeployment -Name ExampleDeployment02 `
  -ResourceGroupName $resourceGroupName `
  -TemplateFile c:\MyTemplates\azuredeploy.json `
  -RollBackDeploymentName ExampleDeployment01

Azure CLIAzure CLI

若要重新部署最后一个成功的部署,请将 --rollback-on-error 参数添加为标志。To redeploy the last successful deployment, add the --rollback-on-error parameter as a flag.

az group deployment create \
  --name ExampleDeployment \
  --resource-group ExampleGroup \
  --template-file storage.json \
  --parameters storageAccountType=Standard_GRS \
  --rollback-on-error

若要重新部署某个特定部署,请使用 --rollback-on-error 参数并提供部署名称。To redeploy a specific deployment, use the --rollback-on-error parameter and provide the name of the deployment. 指定的部署必须已成功。The specified deployment must have succeeded.

az group deployment create \
  --name ExampleDeployment02 \
  --resource-group ExampleGroup \
  --template-file storage.json \
  --parameters storageAccountType=Standard_GRS \
  --rollback-on-error ExampleDeployment01

REST APIREST API

若要在当前部署失败的情况下,重新部署上一个成功部署,请使用:To redeploy the last successful deployment if the current deployment fails, use:

{
  "properties": {
    "templateLink": {
      "uri": "http://mystorageaccount.blob.core.chinacloudapi.cn/templates/template.json",
      "contentVersion": "1.0.0.0"
    },
    "mode": "Incremental",
    "parametersLink": {
      "uri": "http://mystorageaccount.blob.core.chinacloudapi.cn/templates/parameters.json",
      "contentVersion": "1.0.0.0"
    },
    "onErrorDeployment": {
      "type": "LastSuccessful",
    }
  }
}

若要在当前部署失败的情况下,重新部署特定部署,请使用:To redeploy a specific deployment if the current deployment fails, use:

{
  "properties": {
    "templateLink": {
      "uri": "http://mystorageaccount.blob.core.chinacloudapi.cn/templates/template.json",
      "contentVersion": "1.0.0.0"
    },
    "mode": "Incremental",
    "parametersLink": {
      "uri": "http://mystorageaccount.blob.core.chinacloudapi.cn/templates/parameters.json",
      "contentVersion": "1.0.0.0"
    },
    "onErrorDeployment": {
      "type": "SpecificDeployment",
      "deploymentName": "<deploymentname>"
    }
  }
}

指定的部署必须已成功。The specified deployment must have succeeded.

后续步骤Next steps