Search code examples
azureiisasp.net-coreazure-devopswebdeploy

Proper way of deploying an asp.net core web application to on premise IIS with Azure Devops


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 ?


Solution

  • 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.