Setup publish to folder using VSTS
W

2

6

I was using publish to folder option through Visual Studio by right-clicking on the project -> publish -> publish to folder. Result was always ready-to-copy project with applied transformations. I wanted to automate this process using VSTS and have setup build on VSTS.
I used next steps:
- NuGet restore
- Build solution
- Publish Build Artifacts to $(build.artifactstagingdirectory)
- Windows machine file copy from $(build.artifactstagingdirectory) to remote machine using admin login and password

And finally I'm getting zip package on remote machine with complicated folder structure without applied transformations inside at all.
What is wrong? How I can setup same "publish to folder" as in Visual Studio but using VSTS?

Wishful answered 5/1, 2018 at 14:23 Comment(1)
Do you solve this issue?Virgel
W
5

Add below Target to your .csproj to enable transforming config files

<Target Name="TransformConfigFiles" AfterTargets="AfterBuild" Condition="'$(TransformConfigFiles)'=='true'">
<ItemGroup>
  <DeleteAfterBuild Include="$(WebProjectOutputDir)\Web.*.config" />
</ItemGroup>
<TransformXml Source="Web.config" Transform="$(ProjectConfigTransformFileName)" Destination="$(WebProjectOutputDir)\Web.config" />
<Delete Files="@(DeleteAfterBuild)" /></Target>

In your build solution step add the following build arguments "/p:TransformConfigFiles=true" will make the config transformation using the above added target to .csproj

/p:TransformConfigFiles=true /p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:OutDir="$(build.stagingDirectory)"

enter image description here

Then you can use a publish step to publish your $(build.stagingDirectory) contents. You can use $(build.stagingDirectory)_PublishedWebsites as path to publish if you only need the website output.

enter image description here

This will allow you to get the ms deploy package as well as xcopy deploy published website files.

enter image description here

You can use copy files task before the publish task to copy any additional files if you have any to $(build.stagingDirectory) and get them published as build artifacts.

Use VSTS release management with deployment groups to deploy your application to target server. You can use IIS deploy task to deploy to IIS using ms deploy package. If you are using web deploy package you can use a parameters.xml in your web app to get the web config parameters assigned to .setparameters.xml so that you can change values in the deployment time using IIS deployment task.

Waadt answered 5/1, 2018 at 15:3 Comment(3)
Thanks! Just checked _PublishedWebsites folder and there is a Web.config file without applied transformations. How can I force to transform?Wishful
@Wishful I have added a target that should be added to your web project .csproj file to enable transform configuration, to the answer, assuming that this is ASP.NET MVC projectWaadt
This may have solved OP problem, but this way '/p:WebPublishMethod=Package /p:PackageAsSingleFile=true' you end up with a '.zip' instead of the default behaviour of VS publish to folder, which is achieved with @starian chen-MSFT answer. That answer should be the one marked as resolved, given it actually answers OP questions.Philipson
V
7

You are publishing web application through File System method, it is based on the specified configuration (e.g. Debug, Release) to transform web.config. So you need to check which configuration you specified in build solution task (e.g. Visual Studio Build task)

Simple tasks:

  1. NuGet Tool Installer task
  2. NuGet restore task
  3. Visual Studio Build task (MSBuild Arguments: /p:SkipInvalidConfigurations=true /p:DeployOnBuild=true /p:WebPublishMethod=FileSystem /p:publishUrl="$(build.artifactstagingdirectory)\\" /p:DeployDefaultTarget=WebPublish; Platform: $(BuildPlatform); Configuration: $(BuildConfiguration)) Note: BuildPlatform and BuildConfiguration are build variables. It will publish web app to artifacts directory ([agent working folder]/1/a)
  4. Publish Build Artifacts (Path to publish: $(build.artifactstagingdirectory))
Virgel answered 8/1, 2018 at 5:44 Comment(2)
This gives the expected output of a VS publish to folder, with all it's contents unzipped, unlike @ChamindaC approach. It worked for me, and "saved my life" because I needed the unzipped output in next steps. Thanks.Philipson
I was missing the /p:DeployDefaultTarget=WebPublish, I needed both that and /p:WebPublishMethod=FileSystem for the files to be copiedSerica
W
5

Add below Target to your .csproj to enable transforming config files

<Target Name="TransformConfigFiles" AfterTargets="AfterBuild" Condition="'$(TransformConfigFiles)'=='true'">
<ItemGroup>
  <DeleteAfterBuild Include="$(WebProjectOutputDir)\Web.*.config" />
</ItemGroup>
<TransformXml Source="Web.config" Transform="$(ProjectConfigTransformFileName)" Destination="$(WebProjectOutputDir)\Web.config" />
<Delete Files="@(DeleteAfterBuild)" /></Target>

In your build solution step add the following build arguments "/p:TransformConfigFiles=true" will make the config transformation using the above added target to .csproj

/p:TransformConfigFiles=true /p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:OutDir="$(build.stagingDirectory)"

enter image description here

Then you can use a publish step to publish your $(build.stagingDirectory) contents. You can use $(build.stagingDirectory)_PublishedWebsites as path to publish if you only need the website output.

enter image description here

This will allow you to get the ms deploy package as well as xcopy deploy published website files.

enter image description here

You can use copy files task before the publish task to copy any additional files if you have any to $(build.stagingDirectory) and get them published as build artifacts.

Use VSTS release management with deployment groups to deploy your application to target server. You can use IIS deploy task to deploy to IIS using ms deploy package. If you are using web deploy package you can use a parameters.xml in your web app to get the web config parameters assigned to .setparameters.xml so that you can change values in the deployment time using IIS deployment task.

Waadt answered 5/1, 2018 at 15:3 Comment(3)
Thanks! Just checked _PublishedWebsites folder and there is a Web.config file without applied transformations. How can I force to transform?Wishful
@Wishful I have added a target that should be added to your web project .csproj file to enable transform configuration, to the answer, assuming that this is ASP.NET MVC projectWaadt
This may have solved OP problem, but this way '/p:WebPublishMethod=Package /p:PackageAsSingleFile=true' you end up with a '.zip' instead of the default behaviour of VS publish to folder, which is achieved with @starian chen-MSFT answer. That answer should be the one marked as resolved, given it actually answers OP questions.Philipson

© 2022 - 2024 — McMap. All rights reserved.