In this blog post we will deploy a console app using MSDeploy. We use a similiar approach at EBSCO to deploy Scheduled Tasks and Windows Services.
Step 1: Extending MSBuild
The setup for this follows the same pattern we used in the Deploying a Databased Project with MSDeploy to extend MSBuild by adding a targets file (ConsoleAppMSDeployPackage.app.targets) which is loaded by the project file. Refer to the previous post for further details.
Step 2: Creating the MSDeploy Package
In our app.targets file, we will use the MSDeploy task again by referencing Microsoft.Web.Publishing.Tasks.dll from the VisualStudio install path.
<UsingTask TaskName="MSDeploy" AssemblyFile="$(MSBuildExtensionsPath)MicrosoftVisualStudiov14.0WebMicrosoft.Web.Publishing.Tasks.dll"/>
Again we will create a target named “AfterBuild” so that packaging occurs after the build completes and use the MSDeploy task to create a package using the dirPath MSDeploy provider as the source which simply copies the application files from a directory.
<PropertyGroup> <FullOutputPath>$(MSBuildProjectDirectory)$(OutputPath)</FullOutputPath> <FullIntermediateOutputPath>$(MSBuildProjectDirectory)$(IntermediateOutputPath)</FullIntermediateOutputPath> <PackageFolderPath>$(FullIntermediateOutputPath)Package</PackageFolderPath> <PackageFilename>$(MSBuildProjectName).zip</PackageFilename> <PackageFullPath>$(PackageFolderPath)$(PackageFilename)</PackageFullPath> </PropertyGroup> <MSdeploy Verb="sync" Source="dirPath=&quot;$(FullOutputPath)&quot;" Destination="package=&quot;$(PackageFullPath)&quot;" ExePath="$(_MSDeployDirPath_FullPath)" ImportDeclareParametersItems="$(MSBuildProjectDirectory)parameters.xml" />
Step 3: Pre/Post Sync Commands
In the case of a non-web application deployment you will likely need to also run a command before the sync, after the sync or in some cases both. So we will create a property for each to hold the MSDeploy flags for this. MSDeploy supports running a command before and after a sync natively.
<PropertyGroup> <PreSyncCommand> -preSync:runCommand=&quot;deploymentpreSync.bat&quot;</PreSyncCommand> <PostSyncCommand> -postSync:runCommand=&quot;deploymentpostSync.bat&quot;</PostSyncCommand> </PropertyGroup>
Step 4: Creating a Deploy Command
Just like the database deployment we will again create a deploy command with helpful details to make it easier for another user to execute the deployment.
<ItemGroup> <DeployCMDLines Include="@ECHO OFF" /> <DeployCMDLines Include="IF %22%1%22==%22%22 GOTO Readme" /> <DeployCMDLines Include="IF %22%2%22==%22%22 GOTO Readme" /> <DeployCMDLines Include=":Deploy" /> <DeployCMDLines Include="ECHO Starting deployment ..." /> <DeployCMDLines Include="@ECHO ON" /> <DeployCMDLines Include="%22$(MSDeployPath)%22 -verb:sync -source:package=$(PackageFilename) -dest:dirPath=$(TargetPath),computername=%2 -verbose$(PreSyncCommand)$(PostSyncCommand) -setParamFile:SetParameters.%1.xml %~3 %~4 %~5 %~6 %~7 %~8 %~9" /> <DeployCMDLines Include="@ECHO OFF" /> <DeployCMDLines Include="GOTO End" /> <DeployCMDLines Include=":Readme" /> <DeployCMDLines Include="ECHO." /> <DeployCMDLines Include="ECHO Deploys application to target path using MSDeploy" /> <DeployCMDLines Include="ECHO." /> <DeployCMDLines Include="ECHO %(DeployTypes.DeployCommandFilenameOnly) ENVIRONMENT TARGETCOMPUTER [Other MSDeploy parameters (up to 8)]" /> <DeployCMDLines Include="ECHO." /> <DeployCMDLines Include="ECHO ENVIRONMENTttThe environment to use for the SetParameters.[environment].xml file." /> <DeployCMDLines Include="ECHO TARGETCOMPUTERttThe target computer for the deployment." /> <DeployCMDLines Include="ECHO MSDeploy paramsttYou may optionally add any other MSDeploy parameters to the call. For example:" /> <DeployCMDLines Include="ECHO -whatif" /> <DeployCMDLines Include="ECHO -retryAttempts" /> <DeployCMDLines Include="ECHO -verbose" /> <DeployCMDLines Include="ECHO." /> <DeployCMDLines Include="GOTO End" /> <DeployCMDLines Include=":End" /> </ItemGroup> <WriteLinesToFile File="$(PackageFolderPath)$(MSBuildProjectName).SQL.deploy.cmd" Lines="@(DeployCMDLines)" Overwrite="true" />
In this case we have two required parameterso on the command. In addition to the desired configuration we also have the TARGETCOMPUTER argument to specify which server to deploy the package to.
Also notice we added the preSync and postSync commands to the MSDeploy.exe call.
The Source
A working example of this solution is on GitHub if you want to try it yourself.
https://github.com/rschiefer/ConsoleAppMSDeployPackage
You should be able to clone the repo and build successfully without any changes.
Feedback
Please let me know if you found this post helpful in the comments or on Twitter. Also, leave feedback if you have a suggestion for a furture msdeployallthethings post.
Pingback: Continuous Delivery… Incrementally – dotnet Catch