I'm trying to deploy an ASP.NET Core application to IIS using VSTS with the following tasks
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.
Any idea how to achieve that?
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>
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>
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:
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)
Refer to these steps below:
restore
)build
)publish
; Check Publish Web Projects option; Arguments: --configuration $(BuildConfiguration) --output $(build.artifactstagingdirectory)
; Check Zip Published Projects
option)$(build.artifactstagingdirectory)
)$(System.DefaultWorkingDirectory)\**\*.zip
; Check XML transformation
option (it is based on Environment name to look for transform source file)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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With