我将我的connectionString保留在应用程序秘密中,并且如果从我自己的计算机进行部署,它也可以工作。现在,我想将此值存储为GitlabCI变量,并在部署期间使用它。为了实现这一点,我做了以下步骤:
在GitlabCI中创建CONNECTION_STRING变量
已修改.gitlab-ci.yml
以使用此变量
before_script:
- 'dotnet user-secrets set "ConnectionString" %CONNECTION_STRING%'
build:
stage: build
script:
- 'dotnet build -c Release /p:DeployOnBuild=true /p:PublishProfile=Properties/PublishProfiles/MyPublishProfile.pubxml'
已修改的Startup.cs
类以使用此机密
private IConfiguration SecretConfiguration { get; }
public Startup(IConfiguration configuration) {
SecretConfiguration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
services.AddDbContext<StudentRepository>(options =>
options.UseMySQL(SecretConfiguration["ConnectionString"]));
services.AddMvc();
}
部署成功,但连接设置不正确。当我点击在数据库中搜索内容的端点时,我得到了
System.ArgumentNullException:值不能为null。参数名称: connectionString在 Microsoft.EntityFrameworkCore.Utilities.Check.NotEmpty(字符串值, 字符串parameterName)