【发布时间】:2020-10-21 11:56:57
【问题描述】:
我想在部署 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://docs.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 中,我添加了一个密钥库并使用 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 获取令牌。如何对 Azure Pipelines 进行身份验证?
【问题讨论】:
标签: azure-devops entity-framework-core azure-sql-database entity-framework-migrations arm-template