我今天在 Azure Function App 的管道部署中遇到了错误
Unable to find the storage account associated with the function app
。我使用 Azure DevOps 和以下代码片段从我们的管道部署生成 Key Vault 参考(使用 AzureAppServiceSettings@1
):
{
"name": "AzureWebJobsStorage",
"value": "@Microsoft.KeyVault(VaultName=${{ variables.KEY_VAULT_NAME }};SecretName=AZUREWEBJOBSSTORAGE)",
"slotSetting": false
},
当将其设置为
AzureWebJobsStorage
的值时,我收到上面的错误。
我猜这是由于 Key Vault 引用在函数应用程序初始化(运行时)之前不会填充,而导致存储帐户出现此错误的检查之前发生。
将
AzureWebJobsStorage
值设置为 Azure 门户内解析的秘密值后,错误消失。
想知道:
AzureWebJobsStorage
(使用任务 AzureFunctionApp@1
和 AzureAppServiceSettings@1
吗?)是的,在部署 Function App 时,可以通过 Azure DevOps pipeline
在
@Microsoft.KeyVault(VaultName=myvault;SecretName=mysecret)
设置中添加
AzureWebJobsStorage
值。
我已经创建了一个使用 RBAC 作为访问管理的密钥保管库,如果您有访问策略,请确保您已向您的 Function App 托管身份和 Azure DevOps 服务连接授予获取、列表、设置、删除和其他所需的秘密权限。已用于Function App部署。
由于我已将 Azure RBAC 用于 Key Vault,我已将 Key Vault 管理员分配给我的 Function App 以及我的 Azure DevOps 服务连接:-
为我的函数应用程序启用托管身份:-
分配访问密钥保管库的角色:-
我的 Azure DevOps 管道与应用程序设置:-
trigger:
- main
variables:
# group: KeyVaultGrp
azureSubscription: 'xxxxx08-aaxxx6469165e7'
functionAppName: 'siliconfunc87'
vmImageName: 'ubuntu-latest'
workingDirectory: '$(System.DefaultWorkingDirectory)/'
stages:
- stage: Build
displayName: Build stage
jobs:
- job: Build
displayName: Build
pool:
vmImage: $(vmImageName)
steps:
- bash: |
if [ -f extensions.csproj ]
then
dotnet build extensions.csproj --runtime ubuntu.16.04-x64 --output ./bin
fi
workingDirectory: $(workingDirectory)
displayName: 'Build extensions'
- task: UsePythonVersion@0
displayName: 'Use Python 3.10'
inputs:
versionSpec: 3.10 # Functions V2 supports Python 3.6 as of today
- bash: |
pip install --target="./.python_packages/lib/site-packages" -r ./requirements.txt
workingDirectory: $(workingDirectory)
displayName: 'Install application dependencies'
- task: ArchiveFiles@2
displayName: 'Archive files'
inputs:
rootFolderOrFile: '$(workingDirectory)'
includeRootFolder: false
archiveType: zip
archiveFile: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
replaceExistingArchive: true
- publish: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
artifact: drop
- stage: Deploy
displayName: Deploy stage
dependsOn: Build
condition: succeeded()
jobs:
- deployment: Deploy
displayName: Deploy
environment: 'development'
pool:
vmImage: $(vmImageName)
strategy:
runOnce:
deploy:
steps:
- task: AzureFunctionApp@1
displayName: 'Azure functions app deploy'
inputs:
azureSubscription: 'PowershellSid'
appType: 'functionAppLinux'
appName: 'siliconfunc87'
package: '$(Pipeline.Workspace)/drop/$(Build.BuildId).zip'
appSettings: '-AzureWebJobsStorage @Microsoft.KeyVault(VaultName=valleyKeyvault;SecretName=AzureWebJobsStorage)'
确保 yaml 管道脚本中的设置格式与以下格式相同:-
appSettings: '-AzureWebJobsStorage @Microsoft.KeyVault(VaultName=valleyKeyvault;SecretName=AzureWebJobsStorage)'
输出:-
秘密已检索并添加成功:-
功能已部署并成功运行:-
还有一个替代方案是将您的 Key Vault 机密连接到变量组并直接在 yaml 代码中引用它:-
variables:
group: KeyVaultGrp
.
.
.
.
.
appSettings: '-AzureWebJobsStorage $(AzureWebJobsStorage)'