Home > Mobile >  How to set Environment Variable for Azure Service Fabric application through Azure DevOps pipelines
How to set Environment Variable for Azure Service Fabric application through Azure DevOps pipelines

Time:12-07

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?

CodePudding user response:

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.

CodePudding user response:

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 = "path\ServiceManifest.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) 
  • Related