2

I have an asp.net core app. I run the publish with the azure devops task :

dotnet restore
dotnet build

and finally

dotnet publish --configuration $(BuildConfiguration) --output $(build.artifactstagingdirectory)

The artifact can be a zip file, or not.

The result of the artifact is all dll, web.config, ...

Ok, that was the build part.

Now, I want to do the relase part and deploy to IIS staging and IIS production.

I can see in the documentation that web deploy is the recommend way https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/?view=aspnetcore-2.2#deploy-the-app

But :

  • dotnet publish don't create a web deploy package
  • I need to change the tag in web.config to have

:

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

for staging and

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

for production (note that I can't use env variables because the server can be the same for some environements)

So, how can I do proper release ?

Vova Bilyachat
  • 18,765
  • 4
  • 55
  • 80
Tim
  • 1,749
  • 3
  • 24
  • 48

2 Answers2

0

I am not sure if its proper way but my build.yml looks like this

steps:
  - task: DotNetCoreInstaller@0
    displayName: 'Use .NET Core sdk 2.2.203'
    inputs:
      version: 2.2.203


  - task: DotNetCoreCLI@2
    displayName: Restore
    inputs:
      command: restore
      projects: '**/*.csproj'
      vstsFeed: '--IHAVE_CUSTOM_FEED'


  - script: dotnet publish -c Release -r win-x64 --self-contained true 
    displayName: 'dotnet build web'
    workingDirectory: Source/Web

  - task: ArchiveFiles@2
    inputs:
      rootFolderOrFile: 'Source/Web/bin/Release/netcoreapp2.2/win-x64/publish' 
      includeRootFolder: false 
      archiveType: 'zip'
      archiveFile: '$(Build.ArtifactStagingDirectory)/Web.zip' 
      replaceExistingArchive: false 

  - task: PublishBuildArtifacts@1
    displayName: 'Publish Artifact: Release'
    inputs:
      PathtoPublish: '$(build.artifactstagingdirectory)'

      ArtifactName: Release

    condition: succeededOrFailed()

So as artifact I will have zip of my web project, then in release I am using 'Azure App Service Deploy' to deploy my zip to app service

PS. You asked if you need to build it twice. Yes if you need to target different OS if you target just windows then No. Build only once (Release only) and even remove environmentVariables from your webconfig if you are using any. Then set ASPNETCORE_ENVIRONMENT environment variable on each server. So you will achieve that when you test you have same build and In startup you will read appconfig.{env}.config depends on which server you deploy.

Vova Bilyachat
  • 18,765
  • 4
  • 55
  • 80
  • 1
    I'm not publishing to azure, but on-premise. And I need to do web.config transformation during the release phase – Tim May 07 '19 at 06:54
  • @Tim dotnet publish will do transformation automatically (-c Release will use release transformation). Also if you got zip what is stopping you to put content of that folder into your iis root folder? – Vova Bilyachat May 07 '19 at 07:26
  • @Tim Azure App Service Deploy - is using msdeploy so you can deploy even to your on premises from azure if they are visible or you can take zip and msdeploy and run command on your premises. – Vova Bilyachat May 07 '19 at 07:27
  • that means I have to do a dotnet publish for each environnement ? dotnet publish -c Staging and dotnet publish -c Production ? – Tim May 07 '19 at 07:28
  • the problem is that i set the ASPNETCORE_ENVIRONMENT in the web.config because I can have multiple environnement on the same server. So this variable must be set during the release process. In addition, if I generate multiple publish, with different target, theses values will be in the web.xxx.config and not in azure devops variables – Tim May 07 '19 at 08:08
  • @Tim https://stackoverflow.com/questions/31049152/publish-to-iis-setting-environment-variable look at this – Vova Bilyachat May 07 '19 at 09:21
  • ok, thanks for your answer. I've already tested all thing and can work, but for my needs it's not enough. I'm going to use msbuild instead of dotnet publish. Thanks for your patience and your help. – Tim May 07 '19 at 11:54
0

To be able to set ASPNETCORE_ENVIRONMENT you can simply use XML transformation in your release pipeline.

  1. Create a release pipeline and name your stage name Release.
  2. Make sure you have a Web.config file. Now create a file Web.Release.config which contains the transform, like so:
<system.webServer>
  <aspNetCore>
    <environmentVariables xdt:Transform="Replace">
      <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Release" />
    </environmentVariables>
  </aspNetCore>
</system.webServer>
  1. Enable XML transformation in your release pipeline task, for example in this task.

According to my understanding, the stage name is used in the XML transformation to determine which Web.{EnvironmentName}.config to transform.