出错时回退以成功部署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. 你可以指定特定的较早部署或上次成功的部署。You can specify either a particular earlier deployment or the last successful deployment.

重要

此功能通过重新部署较早的部署来回滚失败的部署。This feature rollbacks a failed deployment by redeploying an earlier deployment. 其结果可能与撤消失败部署所得到的结果不同。This result may be different than what you would expect from undoing the failed deployment. 请确保自己了解如何重新部署较早的部署。Make sure you understand how the earlier deployment is redeployed.

重新部署注意事项Considerations for redeploying

使用此功能之前,请考虑以下有关如何处理重新部署的详细信息:Before using this feature, consider these details about how the redeployment is handled:

  • 即使在较早的部署中使用了增量模式,也可以使用完整模式来运行先前的部署。The previous deployment is run using the complete mode, even if you used incremental mode during the earlier deployment. 如果较早的部署使用增量模式,那么,以完整模式重新部署可能会产生意外的结果。Redeploying in complete mode could produce unexpected results when the earlier deployment used incremental. 完整模式意味着删除先前部署中未包括的所有资源。The complete mode means that any resources not included in the previous deployment are deleted. 指定一个较早的部署,该部署代表你希望包含在资源组中的所有资源及其状态。Specify an earlier deployment that represents all of the resources and their states that you want to exist in the resource group. 有关详细信息,请参阅部署模式For more information, see deployment modes.
  • 重新部署使用与以前运行它时相同的参数以相同的方式运行。The redeployment is run exactly as it was run previously with the same parameters. 无法更改参数。You can't change the parameters.
  • 重新部署只会影响资源,而不会影响任何数据更改。The redeployment only affects the resources, any data changes aren't affected.
  • 此功能只能用于资源组部署。You can use this feature only with resource group deployments. 它不支持订阅、管理组或租户级别的部署。It doesn't support subscription, management group, or tenant 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 in the deployment history. 只有唯一名称才能标识特定的部署。It's only with unique names that a specific deployment can be identified. 如果没有唯一名称,失败的部署可能会覆盖历史记录中的成功部署。If you don't have unique names, a failed deployment might overwrite a successful deployment in the history.

如果指定的较早部署在部署历史记录中不存在,则回滚将返回错误。If you specify an earlier deployment that doesn't exist in the deployment history, the rollback returns an error.

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 deployment group 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 deployment group 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