我正在尝试进入 Visual Studio 资源组模板。 到目前为止,它看起来不错,我已经为网络应用程序添加了一些应用程序设置,但我的问题是,如何使它们特定于部署槽? json中有模板或参数文件的内容吗?
接受的答案是正确的,但遗漏了一条重要的信息。在当前的实现下,交换插槽时,插槽的 AppSettings 配置将随部署一起交换。如果您担心特定于插槽的配置,那么这可能不适合您。
要使配置“粘性”到插槽,请在 ARM 模板中使用以下资源。请注意,slotconfignames 部分已添加到上面 Tom 的回答中的 ARM 模板片段中。
"resources": [
{
"apiVersion": "2015-08-01",
"name": "appsettings",
"type": "config",
"dependsOn": [
"[resourceId('Microsoft.Web/Sites/Slots', variables('webSiteName'), 'Staging')]"
],
"properties": {
"AppSettingKey1": "Some staging value",
"AppSettingKey2": "My second staging setting",
"AppSettingKey3": "My third staging setting"
},
{
"apiVersion": "2015-08-01",
"name": "slotconfignames",
"type": "config",
"dependsOn": [
"[resourceId('Microsoft.Web/Sites', variables('webSiteName'))]"
],
"properties": {
"appSettingNames": [ "AppSettingKey1", "AppSettingKey2" ]
}
}
]
这将使 AppSettingKey1 和 AppSettingKey2 粘在暂存槽中(它们不会随部署一起交换)。
请参阅 Anthony Chu 的 “Azure 资源管理器模板提示和技巧”,了解有关粘性插槽设置以及其他 ARM 模板提示的更多详细信息。
请尝试添加ARM模板中截取的json代码。我已经测试过了。运行成功。
"resources": [
{
"apiVersion": "2015-08-01",
"name": "appsettings",
"type": "config",
"dependsOn": [
"[resourceId('Microsoft.Web/Sites/Slots', variables('webSiteName'), 'Staging')]"
],
"properties": {
"AppSettingKey1": "Some staging value",
"AppSettingKey2": "My second staging setting",
"AppSettingKey3": "My third staging setting"
}
}
]
以下是我的详细步骤:
1。创建一个新的Azure资源组项目(更多详细信息请参阅文档)
2。该Demo仅针对Azure Website Slot App设置配置,因此请从项目中删除其他资源。
4。发布部署
完整的json代码:
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"hostingPlanName": {
"type": "string",
"minLength": 1
},
"skuName": {
"type": "string",
"defaultValue": "S1",
"allowedValues": [
"F1",
"D1",
"B1",
"B2",
"B3",
"S1",
"S2",
"S3",
"P1",
"P2",
"P3",
"P4"
],
"metadata": {
"description": "Describes plan's pricing tier and instance size. Check details at https://azure.microsoft.com/en-us/pricing/details/app-service/"
}
},
"skuCapacity": {
"type": "int",
"defaultValue": 1,
"minValue": 1,
"metadata": {
"description": "Describes plan's instance count"
}
}
},
"variables": {
"webSiteName": "[concat('webSite', uniqueString(resourceGroup().id))]"
},
"resources": [
{
"apiVersion": "2015-08-01",
"name": "[parameters('hostingPlanName')]",
"type": "Microsoft.Web/serverfarms",
"location": "[resourceGroup().location]",
"tags": {
"displayName": "HostingPlan"
},
"sku": {
"name": "[parameters('skuName')]",
"capacity": "[parameters('skuCapacity')]"
},
"properties": {
"name": "[parameters('hostingPlanName')]"
}
},
{
"apiVersion": "2015-08-01",
"dependsOn": [
"[concat('Microsoft.Web/serverfarms/', parameters('hostingPlanName'))]"
],
"location": "[resourceGroup().location]",
"name": "[variables('webSiteName')]",
"properties": {
"name": "[variables('webSiteName')]",
"serverFarmId": "[resourceId('Microsoft.Web/serverfarms', parameters('hostingPlanName'))]"
},
"resources": [
{
"apiVersion": "2015-08-01",
"name": "Staging",
"type": "slots",
"location": "[resourceGroup().location]",
"dependsOn": [
"[resourceId('Microsoft.Web/Sites', variables('webSiteName'))]"
],
"properties": {
},
"resources": [
{
"apiVersion": "2015-08-01",
"name": "appsettings",
"type": "config",
"dependsOn": [
"[resourceId('Microsoft.Web/Sites/Slots', variables('webSiteName'), 'Staging')]"
],
"properties": {
"AppSettingKey1": "Some staging value",
"AppSettingKey2": "My second staging setting",
"AppSettingKey3": "My third staging setting"
}
}
]
}
],
"tags": {
"[concat('hidden-related:', resourceGroup().id, '/providers/Microsoft.Web/serverfarms/', parameters('hostingPlanName'))]": "Resource",
"displayName": "Website"
},
"type": "Microsoft.Web/sites"
}
]
}
如果 Azure 门户上有任何插槽,我们还可以从 azure 资源 获取插槽类型。 我还在 SO 中找到了类似的thread。
“slotconfignames”只能在生产插槽上指定,以告知哪些设置是特定于插槽的,即使它们甚至不存在于插槽上。插槽特定设置的实际值仍应在插槽设置中指定。
"resources": [
{
"apiVersion":"[variables('siteApiVersion')]",
"name":"[variables('WebAppName')]",
"type":"Microsoft.Web/sites",
"kind":"api",
"location":"[variables('location')]",
"tags":{
"[concat('hidden-related:', resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName')))]":"empty"
},
"properties":{
"name":"[variables('WebAppName')]",
"serverFarmId":"[resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName'))]",
"siteConfig":{
"AlwaysOn":"[parameters('AppServiceAlwaysOn')]"
}
},
"resources":[
{
"apiVersion":"[variables('apiVersion')]",
"type":"config",
"name":"appsettings",
"dependsOn":[
"[variables('WebAppName')]"
],
"properties":{}
},
{
"apiVersion":"[variables('siteApiVersion')]",
"type": "config",
"name": "slotconfignames",
"dependsOn": [
"[concat('Microsoft.Web/sites/', variables('WebAppName'))]"
],
"properties": {
"appSettingNames": [ "WEBJOBS_DISABLE_SCHEDULE" ]
}
},
{
"apiVersion":"[variables('siteApiVersion')]",
"condition":"[parameters('stagingSlotEnabled')]",
"name":"[parameters('stagingSlotName')]",
"type":"slots",
"tags":{
"displayName":"[concat(variables('WebAppName'), ' ', parameters('stagingSlotName'))]"
},
"location":"[variables('location')]",
"dependsOn":[
"[resourceId('Microsoft.Web/Sites', variables('WebAppName'))]"
],
"properties":{},
"resources":[
{
"apiVersion":"[variables('apiVersion')]",
"type":"config",
"name":"appsettings",
"dependsOn":[
"[resourceId('Microsoft.Web/Sites/Slots', variables('WebAppName'), parameters('stagingSlotName'))]"
],
"properties":{
"WEBJOBS_DISABLE_SCHEDULE" : "1"
}
}
]
}
]
}
]
挠头千遍之后我找到了解决方案!
对于生产槽(默认槽),您需要添加资源(站点的子资源),如下所示
"resources":[
{
"name":"appsettings",
"type":"config",
"apiVersion":"2018-11-01",
"dependsOn":[
"[concat('Microsoft.Web/sites/', parameters('webAppName'))]"
],
"tags":{
"displayName":"uisettings"
},
"properties":{
"AppSettingKey1":"myvalue",
"WEBSITE_LOCAL_CACHE_OPTION":"Always",
"WEBSITE_LOCAL_CACHE_SIZEINMB":"2000"
}
},
{
"name":"connectionstrings",
"type":"config",
"location":"[parameters('location')]",
"tags":{
"displayName":"uisettings"
},
"apiVersion":"2018-11-01",
"dependsOn":[
"[resourceId('Microsoft.Web/sites',parameters('webAppName'))]"
],
"properties":{
"ConnString2":{
"value":"[parameters('connectionstring')]",
"type":"SQLServer"
}
}
},
{
"apiVersion":"2018-11-01",
"name":"slotconfignames",
"type":"config",
"dependsOn":[
"[concat('Microsoft.Web/sites/', parameters('webAppName'))]"
],
"properties":{
"appSettingNames":[
"WEBSITE_LOCAL_CACHE_OPTION",
"WEBSITE_LOCAL_CACHE_SIZEINMB"
],
"connectionStringNames":[
"ConnString2"
]
}
}
]
对于不需要“slotconfignames”的插槽,资源 slotconfignames 仅适用于默认插槽(生产插槽) PFA 插槽的工作代码
"resources":[
{
"name":"appsettings",
"type":"config",
"apiVersion":"2018-11-01",
"dependsOn":[
"[resourceId('Microsoft.Web/sites/slots',parameters('webAppName'),'Staging')]"
],
"tags":{
"displayName":"uisettings"
},
"properties":{
"AppSettingKey1":"myvalue",
"WEBSITE_LOCAL_CACHE_OPTION":"Always",
"WEBSITE_LOCAL_CACHE_SIZEINMB":"2000"
}
},
{
"name":"connectionstrings",
"type":"config",
"location":"[parameters('location')]",
"tags":{
"displayName":"uisettings"
},
"apiVersion":"2018-11-01",
"dependsOn":[
"[resourceId('Microsoft.Web/sites/slots',parameters('webAppName'),'Staging')]"
],
"properties":{
"ConnString2":{
"value":"[parameters('connectionstring')]",
"type":"SQLServer"
}
}
}
],
@Joe Newtons 的答案是正确的,但对于较新的 API 版本来说不是最新的。 这是他的答案,但针对 api 版本
2022-09-01
进行了格式化,并使应用程序设置粘在您的Production插槽中
仅显示感兴趣的字段
"resources": [
{
"type": "Microsoft.Web/sites",
"apiVersion": "2022-09-01",
"name": "variables('webSiteName')",
"dependsOn": [
"[resourceId('Microsoft.Web/serverfarms', variables('serverFarmsName'))]"
],
"properties": {
"siteConfig": {
"appSettings": [
{
"name": "AppSettingKey1",
"value": "Value1"
},
{
"name": "AppSettingKey2",
"value": "Value2"
},
{
"name": "AppSettingKey3",
"value": "Value3"
}
],
}
},
},
{
"type": "Microsoft.Web/sites/config",
"apiVersion": "2022-09-01",
"name": "[concat(variables('webSiteName'), '/slotconfignames')]",
"dependsOn": [
"[resourceId('Microsoft.Web/Sites', variables('webSiteName'))]"
],
"properties": {
"appSettingNames": [ "AppSettingKey1", "AppSettingKey2" ]
}
},
]