How to create MSBuild targets file to deploy files? - deployment

Given I have a post build process that generates files in \bin\out how can I use a MSBuild targets file to state that all files (recursively) in that directory should be published during web deploy?

Let's split this in two parts:
First, build the web project:
<Target Name="BuildWeb">
<MSBuild Projects="WebExtranet\WebExtranet.csproj"
Properties="Configuration=$(Environment);DeployOnBuild=True;CreatePackageOnPublish=True;BaseIntermediateOutputPath=..\$(DistDir)\Web\" />
</Target>
Then use this to publish to the local IIS (you might need to adjust the msdeploy command to suit your needs)
<Target Name="DeployWeb">
<ItemGroup>
<PackageSourceDir Include='$(DistDir)\Web\$(Environment)\Package\WebExtranet.zip' />
</ItemGroup>
<Message Text="Package path: %(PackageSourceDir.FullPath)" />
<Exec Command=""c:\Program Files\IIS\Microsoft Web Deploy V3\msdeploy.exe" -verb:sync -allowUntrusted -source:package="%(PackageSourceDir.FullPath)" -setParam:'IIS Web Application Name'='My Web App' -dest:auto"/>
</Target>
Common pitfalls:
The msbuild version is important. I'm using V3.
You have to have the web application already created on IIS. Only need to do it once.
Note that the $(Environment) variable is the build configuration. I have several, but run the first target and check the folder structure created. Then adjust the script.

Related

How to include Gulp generated dist folder in ProjectName.zip package created by the Visual Studio Build step

I have a build definition with the following build steps in the following order:
NuGet Installer
Npm
Gulp
Visual Studio Build
Azure Web App Deployment
In step 3, Gulp generates a dist folder in my application root. The dist folder contains some subfolders and files. The subfolders themselves can contain other subfolders and files.
In step 4, the Visual Studio Build step creates a <ProjectName>.zip zip file when completed.
The Azure Web App Deployment step then deploys that zip file to my Azure Web App.
How do I include the dist folder in <ProjectName>.zip?
I tried doing two Azure Web App Deployments deploying one zip file at a time in the same build definition but the second Azure Web App Deployment build step wipes whatever was deployed by the first one. Is there a way to tell the second Azure Web App Deployment step to "append" to whatever was deployed by the first Azure Web App Deployment step?
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.0\build\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props" Condition="Exists('..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.0\build\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props')" />
<Import Project="..\packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props" Condition="Exists('..\packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
...
...
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" Condition="false" />
...
...
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target> -->
<PropertyGroup>
<CopyAllFilesToSingleFolderForPackageDependsOn>
CustomCollectFiles;
$(CopyAllFilesToSingleFolderForPackageDependsOn);
</CopyAllFilesToSingleFolderForPackageDependsOn>
<CopyAllFilesToSingleFolderForMsdeployDependsOn>
CustomCollectFiles;
$(CopyAllFilesToSingleFolderForPackageDependsOn);
</CopyAllFilesToSingleFolderForMsdeployDependsOn>
</PropertyGroup>
<Target Name="CustomCollectFiles">
<ItemGroup>
<_CustomFiles Include="dist\**\*" />
<FilesForPackagingFromProject Include="%(_CustomFiles.Identity)">
<DestinationRelativePath>dist\%(RecursiveDir)%(Filename)%(Extension)</DestinationRelativePath>
</FilesForPackagingFromProject>
</ItemGroup>
</Target>
</Project>
The property CopyAllFilesToSingleFolderForPackageDependsOn must come after the import <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" Condition="false" />
dist\**\* means take the dist folder that is directly underneath web application root
dist\%(RecursiveDir)%(Filename)%(Extension) means place the dist folder directly underneath the web application root inside of the package
Example package path:
C:\_publish\BuildAgentDemo.zip\Content\C_C\Repos\BuildAgentDemoTest1\BuildAgentDemo\obj\Release\Package\PackageTmp\dist
BuildAgentDemo.zip is one of the artifacts created by the Web Deploy Package publish profile
PackageTmp is your web application root
dist is where your custom folders and files will end up
You can add a custom target in your project file to include the files generated by Gulp. Refer to this question for details: How do you include additional files using VS2010 web deployment packages?
And another way to get the Gulp folder published is enabling the FTP deployment of your Azure Web App. And then you can add a FTP Uploader task after "Azure Web App Deployment" task. Use "Azure Web App Deployment" task to deploy the package generated by VS Build and use "FTP Uploader" task to upload the "dist" folder into Azure Web App. Note: Make sure you unchecked the "Delete old Files" option of the FTP Uploader task.

create a deployment package for Service Fabric that includes all artifacts necessary to run the designed workflows at runtime

How to create a deployment package for Service Fabric that includes all artifacts necessary to run the designed workflows at run-time?
Another option is to package a service fabric application by building a solution in release mode (for example). This section can be added to the .sfproj file to achieve the goal:
<Target Name="ReleaseAfterBuild" AfterTargets="AfterBuild" Condition=" '$(Configuration)' == 'Release' ">
<MSBuild Projects="$(ProjectPath)" Targets="Package" />
</Target>
To create a Service Fabric application package you need to run the "Package" target on the sfproj file.
msbuild app.sfproj /t:Package
This will create a "pkg" directory next to the sfproj file with all the necessary files.

MSBuild project deploy to local folder with config transformed

I'm having trouble trying to find the right way to use MSBuild to build a web project and output the project with only deployable files (i.e. no .cs, .csproj, .Debug.config etc.), but published to a local folder that I can then FTP, RoboCopy, (or whatever) to a secondary location.
The published output must have the Web.config file pre-transformed as per the specified configuration and the transformation config files (e.g. Web.Debug.config) not included in the output. I don't need any fancy publishing to IIS, database deployment or anything like that, I just want clean file system output that I can then test. Note that this cannot be done using visual tools as I want to run it as part of an automated build process.
I can generate a web deployment package, but I can't get WebDeploy to work because it doesn't seem to handle quoted command line options anymore (seems to be some kind of bug) and the directory structure has spaces, so I was hoping to accomplish the whole task using MSBuild, seeing as MSBuild seems to have native capacity to transform the config file (TransformXml), which is the only real bit of proper deployment functionality I'd be using.
Got it figured out eventually. The following build script does the trick:
<?xml version="1.0"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll"/>
<PropertyGroup>
<OutputDir>obj\website-output</OutputDir>
</PropertyGroup>
<Target Name="PrepareDeploy">
<ItemGroup>
<DeployableFiles Include="App_Code/**/*.*;App_Data/**/*.*;Areas/**/Views/**/*.*;bin/**/*.*;Views/**/*.*;*.aspx;*.asax;*.html;*.htm;sitemap.xml;*.ico;*.png" Exclude="App_Data/**/*.log" />
</ItemGroup>
<RemoveDir ContinueOnError="true" Directories="$(OutputDir)" />
<MSBuild Projects="Website.csproj" />
<MakeDir ContinueOnError="true" Directories="$(OutputDir)" />
<Copy SourceFiles="#(DeployableFiles)" DestinationFiles="#(DeployableFiles->'$(OutputDir)\%(RelativeDir)%(Filename)%(Extension)')" />
<TransformXml Source="Web.config" Transform="Web.$(Configuration).config" Destination="$(OutputDir)\web.config" />
</Target>
</Project>

MSBuild: OutputPath directory is empty

I want to deploy my ASP.NET MVC site and have the following script.
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\MyProjName\MyProjName.csproj"/>
<PropertyGroup>
<NewInstallDir>C:\DeployFolder\</NewInstallDir>
<BinDir>$(NewInstallDir)bin\</BinDir>
</PropertyGroup>
<Target Name="Build">
<MSBuild Projects="..\MySlnName.sln"
Properties="Configuration=Release;Platform=Any CPU;OutputPath=$(BinDir)" />
<Copy SourceFiles="#(Content->'..\MyProjName\%(RelativeDir)%(FileName)%(Extension)')"
DestinationFiles="#(Content->'$(NewInstallDir)%(RelativeDir)%(FileName)%(Extension)')" />
<Copy SourceFiles="#(None->'..\MyProjName\%(RelativeDir)%(FileName)%(Extension)')"
DestinationFiles="#(None->'$(NewInstallDir)%(RelativeDir)%(FileName)%(Extension)')" />
<MakeDir Directories="#(Folder->'$(NewInstallDir)%(RelativeDir)')" />
</Target>
</Project>
Main idea.
I copied binary to C:\DeployFolder (take structure of folder from sources). I build my dll to C:\DeployFolder\Bin (I don't have this folder in sources folder so I need separately copy this).
I run my script - all works instead of copy DLLs to OutputPath. Same scripts works for other asp.net mvc project. I have no idea what is wrong in this case.
I complete this issue with workaround but I would like to know what is wrong with this script.
The first thing I'd try is to replace your use of the deprecated $(OutputPath) with $(OutDir). From when I've seen this error, 9 times out of 10 it is due to a mismatch between the Platform/Configuration requested and how a particular project is actually defined. Take care to keep track of the discrepency between "Any CPU" (with a space) preferred by solution files and "AnyCPU" actually used inside project files for $(Platform). Some project wizards only set up an "x86" Platform or otherwise omit "AnyCPU" which can cause the OutputPath to be empty.
Beyond that, the approach of importing a project file and then building a solution (presumbably that includes the very same project" is a bit off center. Consider making the deployment changes you desire within the project file itself, through an import. You can either wire into the existing build targets at the right place, or perhaps add an additional target.

Netbeans: deploying Java app to remote Tomcat

Is there any easy way to automatically deploy a web service / java web app, etc to a remote tomcat server? currently i have to manually copy the .war file.
Personally, I add a "deploy" target in build.xml that contains an <scp> tag to transfer the war file.
UPDATE:
Here is an example:
<target name="deploy" depends="dist">
<scp todir="${user.name}#www.myserver.com:tomcat-base/webapps/"
keyfile="${user.home}/.ssh/myserver.key"
passphrase="BlaBlaBla" trust="true">
<fileset dir="dist" includes="myapp.war"/>
</scp>
</target>