skip to Main Content

Our team has a Service Fabric application that has an environment variable defined in the ServiceManifest.xml file called connectionString

  <!-- Code package is your service executable. -->
  <CodePackage Name="Code" Version="1.0.0">
    <EntryPoint>
      <ExeHost>
        <Program>myMMAPI.exe</Program>
        <WorkingFolder>CodePackage</WorkingFolder>
      </ExeHost>
    </EntryPoint>
    <EnvironmentVariables>
      <EnvironmentVariable Name="ASPNETCORE_ENVIRONMENT" Value=""/>
      <EnvironmentVariable Name="connectionString" Value="Server=mydb.123abc.database.windows.net; Authentication=Active Directory Password; Encrypt=True; Database=mydevdb; User Id=myId;Password=****" />
    </EnvironmentVariables>
  </CodePackage>

We then can reference this environment variable in our Startup.cs class.

Constants.ConnectionString = Environment.GetEnvironmentVariable("connectionString");

However, we need to have this Environment Variable be set during the deployment using Azure DevOps Pipeline and not hard coded into this ServiceManifest.xml file.

Is there a way to pass this Environment Variable in Azure DevOps so we can change the connection string per environment?

2

Answers


  1. You can introduce tokens that you switch out with variables with this task in the pipeline:
    https://marketplace.visualstudio.com/items?itemName=qetza.replacetokens

    These variables can be defined in the pipeline, but also in a separate file.

    Login or Signup to reply.
  2. You can use PowerShell script in the pipeline to modify the value in ServiceMainifest.xml.

    For example:

    $myConnectionString = " Server=mydb.123abc.database.windows.net; Authentication=Active Directory Password; Encrypt=True; Database=mydevdb; User Id=myId;Password=****
    ";
    
    $serviceConfig = "pathServiceManifest.xml"
    
    Function updateConfig($config) 
    { 
    $doc = (Get-Content $config) -as [Xml]
    $root = $doc.get_DocumentElement();
    $activeConnection = $root.EnvironmentVariables.SelectNodes("EnvironmentVariable");
    $activeConnection.SetAttribute("connectionString", $myConnectionString);
    $doc.Save($config)
    } 
    
    updateConfig($serviceConfig) 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search