gpt4 book ai didi

Azure Pipeline部署重置应用程序配置

转载 作者:行者123 更新时间:2023-12-02 08:24:36 27 4
gpt4 key购买 nike

我创建了一个简单的 Azure Function,我想将其用作良好 DevOps 实践的练习。我准备了一个 azure-pipelines.yml,它执行以下操作:

  1. 构建应用的代码
  2. 运行测试
  3. 将二进制文件作为工件发布
  4. 创建 Azure 资源(应用服务计划、Azure 函数、存储帐户、App Insights)
  5. 将代码部署到 Azure Function。

我听说过很多关于基础设施即代码的内容,我真的很想尝试一下,这就是第 4 点的原因。

这是我的azure-pipelines.yml:

trigger:
- master

variables:
azureServiceConnection: service-connection
appName: az-func-123456123
resourceGroup: rg-1223456123
location: North Europe
buildConfiguration: Release

pool:
vmImage: 'ubuntu-latest'

stages:
- stage: CI
jobs:
- job: Azure_Function
displayName: 'Azure Functions'
steps:
- checkout: self
- task: DotNetCoreCLI@2
displayName: Restore
inputs:
command: 'restore'
projects: '**/*.csproj'

- task: DotNetCoreCLI@2
displayName: Build
inputs:
command: build
projects: '**/*.csproj'
arguments: '--configuration $(buildConfiguration)'

- task: DotNetCoreCLI@2
displayName: Test
inputs:
command: test
projects: '**/*Tests/*.csproj'
arguments: '--configuration $(buildConfiguration)'

- task: DotNetCoreCLI@2
displayName: Zip Artifact
inputs:
command: publish
publishWebProjects: false
arguments: '--no-build --configuration $(buildConfiguration) --output $(Build.ArtifactStagingDirectory)'
zipAfterPublish: True
workingDirectory: src/az-function-with-deployment

- publish: $(Build.ArtifactStagingDirectory)
artifact: AzureFunction


- stage: Deployment
condition: and(succeeded(), ne(variables['Build.Reason'], 'PullRequest'))
jobs:
- deployment: Deploy
environment: test-env
strategy:
runOnce:
deploy:
steps:
- checkout: self

- task: AzureResourceGroupDeployment@2
displayName: Deploy Azure resources
inputs:
deploymentScope: 'Resource Group'
ConnectedServiceName: '$(azureServiceConnection)'
action: 'Create Or Update Resource Group'
resourceGroupName: $(resourceGroup)
location: $(location)
templateLocation: 'Linked artifact'
csmFile: 'templates/function-app-deployment.json'
deploymentMode: 'Incremental'

- task: AzureFunctionApp@1
displayName: Deploy Azure Function
inputs:
azureSubscription: $(azureServiceConnection)
resourceGroupName: $(resourceGroup)
appType: functionAppLinux
appName: $(appName)
package: $(Pipeline.Workspace)/AzureFunction/*.zip

这是 AzureResourceGroupDeployment@2 任务使用的 templates/function-app-deployment.json:

{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"appName": {
"type": "string",
"defaultValue": "az-func-123456123",
"metadata": {
"description": "The name of the function app that you wish to create."
}
},
"storageAccountType": {
"type": "string",
"defaultValue": "Standard_LRS",
"allowedValues": [
"Standard_LRS",
"Standard_GRS",
"Standard_RAGRS"
],
"metadata": {
"description": "Storage Account type"
}
},
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]",
"metadata": {
"description": "Location for all resources."
}
},
"appInsightsLocation": {
"type": "string",
"defaultValue": "[resourceGroup().location]",
"metadata": {
"description": "Location for Application Insights"
}
},
"runtime": {
"type": "string",
"defaultValue": "dotnet",
"allowedValues": [
"node",
"dotnet",
"java"
],
"metadata": {
"description": "The language worker runtime to load in the function app."
}
}
},
"variables": {
"functionAppName": "[parameters('appName')]",
"hostingPlanName": "[parameters('appName')]",
"applicationInsightsName": "[parameters('appName')]",
"storageAccountName": "[concat(uniquestring(resourceGroup().id), 'azfunctions')]",
"functionWorkerRuntime": "[parameters('runtime')]"
},
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2019-06-01",
"name": "[variables('storageAccountName')]",
"location": "[parameters('location')]",
"sku": {
"name": "[parameters('storageAccountType')]"
},
"kind": "Storage"
},
{
"type": "Microsoft.Web/serverfarms",
"apiVersion": "2020-06-01",
"name": "[variables('hostingPlanName')]",
"location": "[parameters('location')]",
"kind": "linux",
"sku": {
"tier": "Dynamic",
"name": "Y1"
},
"properties": {
"name": "[variables('hostingPlanName')]",
"reserved": true
}
},
{
"type": "Microsoft.Web/sites",
"apiVersion": "2020-06-01",
"name": "[variables('functionAppName')]",
"location": "[parameters('location')]",
"kind": "functionapp",
"dependsOn": [
"[resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName'))]",
"[resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName'))]"
],
"properties": {
"serverFarmId": "[resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName'))]",
"siteConfig": {
"appSettings": [
{
"name": "AzureWebJobsStorage",
"value": "[concat('DefaultEndpointsProtocol=https;AccountName=', variables('storageAccountName'), ';EndpointSuffix=', environment().suffixes.storage, ';AccountKey=',listKeys(resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName')), '2019-06-01').keys[0].value)]"
},
{
"name": "WEBSITE_CONTENTAZUREFILECONNECTIONSTRING",
"value": "[concat('DefaultEndpointsProtocol=https;AccountName=', variables('storageAccountName'), ';EndpointSuffix=', environment().suffixes.storage, ';AccountKey=',listKeys(resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName')), '2019-06-01').keys[0].value)]"
},
{
"name": "WEBSITE_CONTENTSHARE",
"value": "[toLower(variables('functionAppName'))]"
},
{
"name": "FUNCTIONS_EXTENSION_VERSION",
"value": "~2"
},
{
"name": "APPINSIGHTS_INSTRUMENTATIONKEY",
"value": "[reference(resourceId('microsoft.insights/components', variables('applicationInsightsName')), '2020-02-02-preview').InstrumentationKey]"
},
{
"name": "FUNCTIONS_WORKER_RUNTIME",
"value": "[variables('functionWorkerRuntime')]"
}
],
"linuxFxVersion": "dotnet|3.1"
}
}
},
{
"type": "microsoft.insights/components",
"apiVersion": "2020-02-02-preview",
"name": "[variables('applicationInsightsName')]",
"location": "[parameters('appInsightsLocation')]",
"tags": {
"[concat('hidden-link:', resourceId('Microsoft.Web/sites', variables('applicationInsightsName')))]": "Resource"
},
"properties": {
"ApplicationId": "[variables('applicationInsightsName')]",
"Request_Source": "IbizaWebAppExtensionCreate"
}
}
]
}

部署工作正常,我创建了资源。但是,我注意到一些问题:

  1. 每次部署时,我的应用程序的配置都会重置!目前,部署后,我手动将所有配置值添加到我的Azure Function中,这非常不方便。我想我可以在管道本身中进行配置,但是,我不想在那里硬编码任何值!看来不对劲。我还没有找到任何有关云中应用程序配置的资源/最佳实践。我错过了什么?
  2. 我的管道中的某些值是硬编码的(例如应用程序的名称)。目前我确实没有看到任何问题,但我想知道,事情应该是这样的吗?我可以改进什么吗?

一般来说,我很高兴看到有关我发布的 YAML 和 JSON 的任何评论。我确信还有很多需要改进的地方。

最佳答案

每次要部署功能时都运行ARM部署的问题。因此它只为您提供 AppSettings 模板中声明的内容。请看一下here - Don't delete AppSettings not declared in a template .

你可以采取什么措施来解决这个问题?!

  1. 将 ARM 模板部署移至单独的管道 - 专用于更新管道基础设施的管道。每次要部署代码时,无需重新部署基础设施。您可以使用path filter确保您的管道仅在完成适当的更改后才会运行。但是,这并不能解决删除应用程序设置的问题。这使得这种情况发生的频率降低。

  2. 要解决已删除设置的问题,您需要执行以下操作之一:

    • 在 ARM 模板中处理它们
    • 在运行 ARM 模板之前调用 Azure CLI store appsetings/文件中的连接字符串,部署 ARM 模板,运行 Azute CLI 到 update appsettings/连接字符串

关于Azure Pipeline部署重置应用程序配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65794179/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com