首页 > 解决方案 > 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

标签: .net-corecontinuous-integrationgitlabgitlab-cicontinuous-deployment

解决方案


推荐阅读