.net-core - GitlabCI不使用变量
问题描述
我将我的 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:值不能为空。参数名称:Microsoft.EntityFrameworkCore.Utilities.Check.NotEmpty(String value, String parameterName) 处的 connectionString
解决方案
推荐阅读
- memory-management - How does xv6 knows where last element of p->pgdir is?
- react-native - Anyone knows why the label of picker in react native doesn't work?
- python - 如何使用 groupby 创建新表并删除重复项
- azure - 如何找到 azure policy 的流量
- html - 3级子菜单创建
- json - Scala - 如何将 json 作为输入参数并解析它?
- react-native - 如何解决错误`Could not find "store" in the context`?
- ruby - rake neo4j:install[community-latest] - `neo4j-rake_tasks` gem 不再是 `neo4j-core` gem 的依赖项
- c# - 方法中无法识别名称
- python-3.x - 从python文件打印到kivy