Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VSTS/TFS set environment variable ASP.NET core

I'm trying to deploy an ASP.NET Core application to IIS using VSTS with the following tasks

enter image description here

However, after much googling and browsing through MS docs I couldn't find a way to set environment variables for the deployment. The variables I set in the release definition in environment scope aren't getting set as environment variables.

enter image description here

Any idea how to achieve that?

like image 601
ubi Avatar asked Dec 12 '17 00:12

ubi


4 Answers

The environment variables you set in VSTS are just used for the deployment itself (ie anything that VSTS is doing such as building your application or running unit tests), but the runtime application will use whichever ones are on the server hosting it.

You will need to set the environment variables on the IIS server that VSTS is deploying to if you want your deployed application to use them as well. Microsoft docs show how to set this depending on your server: Setting the environment

Update in response to comments:

The reccommended way to set environment variables is on the machine itself - ie. log in to the IIS server you are deploying to and add the ASPNETCORE_ENVIRONMENT environment variable there in system properties -> advanced settings -> environment variables

If for some reason you aren't able to do this, you can set them in the Web.config file (according to that documentation). If you are always setting the same value you should be able to just put what you need in the Web.config like so

<environmentVariables>
  <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
</environmentVariables>

If you really need the XML transforms (which, honestly, I'm not sure you do in this situation - this is for altering the Web.config file at deployment time based on the build configuration. As somebody else mentioned, with asp.net core the reccommended config setup is appsettings[.environment].json files which are automagically loaded based on the matching machine level ASPNETCORE_ENVIRONMENT env variable), you need to actually define the transformations in a transform file using the correct syntax and have it replace the parts you want to change. This is obviously the more difficult option.

See: How to: Transform Web.config When Deploying a Web Application Project for creating the transformation files and Web.config Transformation Syntax for Web Project Deployment Using Visual Studio for the configuration syntax if you choose to go down that path

Something like this (unable to currently test but this should give you an idea - note the transform namespace on the transform file and the xdt: attributes). I believe the transform file that gets loaded matches the build configuration which you may need to configure as part of the VSTS task:

Web.config

<configuration>
  <system.webServer>
    <aspNetCore ...>
      <environmentVariables>
        <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
      </environmentVariables>
    </aspNetCore>
  </system.webServer>
</configuration>

Web.Release.config (transform file for build configuration "Release")

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <system.webServer>
    <aspNetCore ...>
      <environmentVariables>
        <environmentVariable xdt:Transform="Replace" xdt:Locator="Match(name)" name="ASPNETCORE_ENVIRONMENT" value="Production" />
      </environmentVariables>
    </aspNetCore>
  </system.webServer>
</configuration>
like image 136
Kirlac Avatar answered Nov 10 '22 20:11

Kirlac


For ASP.NET Core 1.x projects with a web.config you can use the below.

Since your deployment has a "Dev" environment, commit to your project the following config file:

web.Dev.config

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0" xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <system.webServer>
    <aspNetCore>
      <environmentVariables xdt:Transform="InsertIfMissing" />
      <environmentVariables>
        <environmentVariable xdt:Transform="InsertIfMissing" xdt:Locator="Match(name)" name="ASPNETCORE_ENVIRONMENT" />
        <environmentVariable xdt:Transform="Replace" xdt:Locator="Match(name)" name="ASPNETCORE_ENVIRONMENT" value="Development" />
      </environmentVariables>
    </aspNetCore>
  </system.webServer>
</configuration>
  • The above will create the environmentVariables section in your web.config if it does not already exist.
  • Replace "Dev" in web.Dev.config with other environment names as necessary.
  • ASPNETCORE_ENVIRONMENT used as an example above, change for other variables.
  • Remove the xmlns="" attribute from the configuration element above if your web.config does not have the same namespace attribute on the configuration element.

In your project.json, add under publishOptions => include:

"web.dev.config"

In VSTS deployment make sure to check "XML transformation" under the IIS Web App Deploy task: XML transformation checkbox

like image 35
Tolga Avatar answered Nov 10 '22 20:11

Tolga


Here is the powershell script I use within Release pipeline (I don't like setting ASPNETCORE_ENVIRONMENT within the build)

arguments:

-p $(System.DefaultWorkingDirectory)\$(Build.DefinitionName)\drop\testbld-Test\web.config -e Development

Inline Script:

param ([string]$p,[string]$e)
$doc = new-object System.Xml.XmlDocument
$location = "$p"
$doc.Load($location)
$subNode = $doc.CreateElement("environmentVariable")
$node = $doc.CreateElement("environmentVariables")
$doc.SelectSingleNode("//aspNetCore").AppendChild($node)
$doc.SelectSingleNode("//environmentVariables").AppendChild($subNode)
foreach($nd in $subNode) {$nd.SetAttribute("name", "ASPNETCORE_ENVIRONMENT");$nd.SetAttribute("value", "$e");}
$doc.Save($location)
like image 2
Post Impatica Avatar answered Nov 10 '22 20:11

Post Impatica


Refer to these steps below:

  1. Set config files properties (e.g. web.config, web.QA.config), Copy to Output Directory: Copy if newer)
  2. .NET Core task (Command: restore)
  3. .NET Core task (command: build)
  4. .NET Core task (Command: publish; Check Publish Web Projects option; Arguments: --configuration $(BuildConfiguration) --output $(build.artifactstagingdirectory); Check Zip Published Projects option)
  5. Publish Build Artifacts (Path to publish:$(build.artifactstagingdirectory))
  6. Open release definition, change environment name (e.g. QA, match the config file name)
  7. IIS Web Deploy task: (Package or Folder: $(System.DefaultWorkingDirectory)\**\*.zip; Check XML transformation option (it is based on Environment name to look for transform source file)
  8. Then the web.[environmentname].config file (e.g. web.QA.config) will be transformed to web.config file.

You also can do it through XDT transform task, (the files can’t be in the zip file, so un-check Zip Published Projects option: step4, and archive files through Archive Files task in release)

like image 1
starian chen-MSFT Avatar answered Nov 10 '22 19:11

starian chen-MSFT