Inject a step into Web Deploy

I really like Web Deploy, it is a powerful tool for injecting parameters whilst deploying web applications to both Azure or an on-premise IIS Server.

Every project is different, and sometimes you need to be able to inject a step into the Web Deploy package creation process to complete some extra step. This can be done by adding a target to the .csproj project file.

The following example shows how you could sign the assemblies before the Web Deploy package is created.

 <Target Name="SignWebExe" AfterTargets="GenerateMsdeployManifestFiles" BeforeTargets="PackageUsingManifest" Condition="'$(Configuration)' == 'Release'">
    <PropertyGroup>
      <Cmd>signtool.exe sign /debug /f "$(certPath)" /p "$(certPassword)" "$(ProjectDir)obj\$(Configuration)\Package\PackageTmp\bin\MyNamespace*.dll"</Cmd>
    </PropertyGroup>
    <Message Text="Signing web deploy executable with command: $(Cmd)" />
    <Exec Command="$(Cmd)" />
  </Target>

The required parameters for this extra step are then passed in as MSBuild arguments. For example, in an Azure DevOps pipeline, you could use the following task:

- task: VSBuild@1
displayName: 'Build Core Services Solution'
inputs:
    solution: src/MySolution.sln
    msbuildArgs: '/p:DeployOnBuild=true;PublishProfile=Release /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:DeployIisAppPath="__SITENAME__" /p:certPassword="$(SigningPassword)" /p:certPath="$(SigningCertFilePath)"'
    platform: '$(BuildPlatform)'
    configuration: '$(BuildConfiguration)'
    clean: true

For the original version of this post see Richard Fennell's personal blog at Inject a step into Web Deploy