gpt4 book ai didi

azure-devops - 仅在 ARM 模板部署后才知道 ConnectionString 时如何生成 EF Core 迁移脚本?

转载 作者:行者123 更新时间:2023-12-05 02:04:19 26 4
gpt4 key购买 nike

我想在部署 Web 应用程序之前将应用程序发布到 Azure 并将迁移部署到数据库。这听起来相对简单,您可以在构建管道中使用 dotnet-ef 创建一个 migrations.sql 脚本,然后在您的发布管道中应用该脚本。

但是,我无法在构建管道中创建 migrations.sql 脚本,因为我在 DTAP 环境中使用了四个不同的数据库。因此,我需要为每个环境生成一个 migrations.sql 脚本,并对每个数据库分别执行这些脚本。 (据我了解)

在我的发布管道中,我使用增量 ARM 模板来部署资源并在 Azure Web App 应用程序设置配置中设置 ConnectionString(来自 Azure Key Vault)。

如何/在哪里生成 migrations.sql 脚本?我是否在发布管道中执行此操作?我的推理是否犯了重大错误?


编辑:

感谢 Madej 的回答表明环境无关紧要。我尝试在我的管道中创建 migrations.sql 脚本。

# ASP.NET Core (.NET Framework)
# Build and test ASP.NET Core projects targeting the full .NET Framework.
# Add steps that publish symbols, save build artifacts, and more:
# https://learn.microsoft.com/azure/devops/pipelines/languages/dotnet-core

trigger:
- master

pool:
vmImage: 'windows-latest'

variables:
projects: '**/*.csproj'
buildPlatform: 'Any CPU'
buildConfiguration: 'Release'

steps:
- task: DotNetCoreCLI@2
displayName: "Install dotnet-ef"
inputs:
command: 'custom'
custom: 'tool'
arguments: 'install --global dotnet-ef'

- task: DotNetCoreCLI@2
displayName: "Restore tools"
inputs:
command: 'custom'
custom: 'tool'
arguments: 'restore'

- task: DotNetCoreCLI@2
displayName: "Restore"
inputs:
command: 'restore'
projects: '$(projects)'
feedsToUse: 'select'

- task: DotNetCoreCLI@2
displayName: "Build"
inputs:
command: 'build'
projects: '$(projects)'
arguments: '--configuration $(BuildConfiguration)'

- task: DotNetCoreCLI@2
displayName: "Create migrations.sql"
inputs:
command: 'custom'
custom: 'ef'
arguments: 'migrations script --configuration $(BuildConfiguration) --no-build --idempotent --output $(Build.ArtifactStagingDirectory)\migrations.sql'
workingDirectory: 'WebApi.api'

- task: DotNetCoreCLI@2
displayName: "Publish"
inputs:
command: 'publish'
publishWebProjects: true
arguments: '--configuration $(BuildConfiguration) --output $(Build.ArtifactStagingDirectory)'
zipAfterPublish: false

- task: PublishBuildArtifacts@1
displayName: "Publish to Azure Pipelines"
inputs:
PathtoPublish: '$(Build.ArtifactStagingDirectory)'
ArtifactName: 'drop'
publishLocation: 'Container'

我的管道不工作,在任务 "Create migrations.sql" 中我遇到了以下错误:

An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: DefaultAzureCredential failed to retrieve a token from the included credentials.
- EnvironmentCredential authentication unavailable. Environment variables are not fully configured.
- ManagedIdentityCredential authentication unavailable. No Managed Identity endpoint found.
- Visual Studio Token provider can't be accessed at C:\Users\VssAdministrator\AppData\Local\.IdentityService\AzureServiceAuth\tokenprovider.json
- Stored credentials not found. Need to authenticate user in VSCode Azure Account.
- Please run 'az login' to set up account

这是因为在我的 Program.cs 中,我添加了一个 keystore 并使用 Azure.Identity DefaultAzureCredential 进行身份验证,如下所示:

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.ConfigureAppConfiguration((hostingContext, config) =>
{
var settings = config.Build();

var credentials = new DefaultAzureCredential(
new DefaultAzureCredentialOptions() {
ExcludeSharedTokenCacheCredential = true,
VisualStudioTenantId = settings["VisualStudioTenantId"],
}
);

config.AddAzureKeyVault(new Uri(settings["KeyVault:Endpoint"]), credentials).Build();
})
.UseStartup<Startup>();
});

Azure Pipelines 无法从 DefaultAzureCredential 获取 token 。如何对 Azure Pipelines 进行身份验证?

最佳答案

我在编辑中找到了问题的解决方案。 The primary way that the DefaultAzureCredential class gets credentials is via environment variables.

因此,我必须在某处定义环境变量。我不想在管道变量中执行此操作以避免必须管理它们,因为它们应该以与 Azure 的服务连接的形式从项目中可用。

我做了以下事情:

  1. 在我的管道中添加了一个 AzureCLI 任务来读取服务主体 ID、 key 和租户 ID,并将它们设置为作业变量,如下所示:
- task: AzureCLI@2
inputs:
azureSubscription: '<subscription>'
scriptType: 'ps'
scriptLocation: 'inlineScript'
inlineScript: |
Write-Host '##vso[task.setvariable variable=AZURE_CLIENT_ID]'$env:servicePrincipalId
Write-Host '##vso[task.setvariable variable=AZURE_CLIENT_SECRET]'$env:servicePrincipalKey
Write-Host '##vso[task.setvariable variable=AZURE_TENANT_ID]'$env:tenantId
addSpnToEnvironment: true
  1. 在我的“Create migrations.sql”任务中,将这些变量作为环境变量传递如下:
- task: DotNetCoreCLI@2
displayName: "Create migrations.sql"
inputs:
command: 'custom'
custom: 'ef'
arguments: 'migrations script --configuration $(BuildConfiguration) --no-build --idempotent --output $(Build.ArtifactStagingDirectory)\migrations.sql'
workingDirectory: 'WebApi.api'
env:
AZURE_CLIENT_ID: $(AZURE_CLIENT_ID)
AZURE_CLIENT_SECRET: $(AZURE_CLIENT_SECRET)
AZURE_TENANT_ID: $(AZURE_TENANT_ID)
  1. 将服务主体作为 Key Vault Secrets User 添加到 Azure Key Vault RBAC。我只能用 az 来做到这一点:
az role assignment create --role 'Key Vault Secrets User (preview)' --scope '/subscriptions/<subscription ID>/resourcegroups/<resource group name>/providers/Microsoft.KeyVault/vaults/<vault name>' --assignee '<service principal object id>'

这绝对解决了我的问题,而无需管理任何更多的 secret /变量,因为它们都包含在管道本身中,不会构成任何安全威胁。

关于azure-devops - 仅在 ARM 模板部署后才知道 ConnectionString 时如何生成 EF Core 迁移脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64462993/

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