Previously I have posted about how to publish a ClickOnce release for multiple environments. Whilst this works well there are two reasons (that I can think of) why it is not appropriate or good enough.
First, you or your manager may be a bit of a purist in terms of releasing to Production ‘exactly’ what has been tested, specifically the exact same compiled assemblies.
Second, you may need to create a release for several different customers who each have several of their own ‘environments’ but you do not want to provide them with your source code.
Third…both of the above. 🙂
When you ‘Publish’ with MsBuild it requires the source code and it recompiles your assemblies. So how can we easily create releases for multiple environments but only Publish once? This posting offers one solution which has worked for me in the past, it’s not a complete ‘how to’ guide I just wanted to cover the idea and problems I ran into along the way.
Master Build Environment
Make sure you take a look at my previous posting as this process builds on those ideas.
One of the final steps of this process is to re-sign the setup.exe, however in order for that final step to work you have to change one of your ClickOnce deployment settings in your .csproj file.
<SignManifests>false</SignManifests>
SignManifests must be false! Otherwise the signtool gets confused when re-signing the setup.exe. Check this out for a bit more info on this known issue.
So the first thing to do is create a BuildEnvironment called Master. The configuration details of this (web service addresses or database connnection strings etc) should point to nowhere, i.e. if someone deployed this build then it should not work. Publishing this is easy just follow the steps defined in the multiple environment posting, the process of creating a release with environment specific information that is tricky.
MsBuild Changes for Master Build Publish
When you publish with ClickOnce it creates a folder ‘Application Files’ where it puts the contents of the release. The space in that file name causes a problem with MAGE when re-signing the app. Now the only way I could see to change this was to cut and paste the _CopyFilesToPublishFolder target from Microsoft.Common.Targets and modifying the _DeploymentApplicationFolderName property in my version to remove the space. I think it is a really BAD idea to change the Microsoft.Common.Targets directly so that option was ruled out immediately and as you can see in the code below, Application Files (version below already has the space removed) is a hardcoded value, not a settable property…therefore even though it is ugly, cut and paste seems the only viable solution.
<!--
This Target code has been cut and paste from Microsoft.Common.Targets.
Only ONE thing has been changed.
Application Files changed to ApplicationFiles (i.e. the space removed)
The only other way of changing that value is to modify Microsoft.Common.Targets
which is a really bad idea (would have to ensure this was changed on ALL machines.)
The space causes a problem when re-signing the app with MAGE
============================================================
_CopyFilesToPublishFolder
============================================================
-->
<Target
Name="_CopyFilesToPublishFolder">
<!-- Compute name of application folder, which includes the assembly name plus formatted application version.
The application version is formatted to use "_" in place of "." chars (i.e. "1_0_0_0" instead of "1.0.0.0").
This is done because some servers misinterpret "." as a file extension. -->
<FormatVersion Version="$(ApplicationVersion)" Revision="$(ApplicationRevision)" FormatType="Path">
<Output TaskParameter="OutputVersion" PropertyName="_DeploymentApplicationVersionFragment"/>
</FormatVersion>
<PropertyGroup>
<_DeploymentApplicationFolderName>ApplicationFiles\$(AssemblyName)_$(_DeploymentApplicationVersionFragment)</_DeploymentApplicationFolderName>
<_DeploymentApplicationDir>$(PublishDir)$(_DeploymentApplicationFolderName)\</_DeploymentApplicationDir>
</PropertyGroup>
<!-- Copy files to publish folder -->
<Copy
SourceFiles=
"@(_ApplicationManifestFinal);
@(_DeploymentResolvedManifestEntryPoint);
@(_DeploymentManifestFiles);
@(ReferenceComWrappersToCopyLocal);
@(ResolvedIsolatedComModules);
@(_DeploymentLooseManifestFile)"
DestinationFiles=
"@(_ApplicationManifestFinal->'$(_DeploymentApplicationDir)%(TargetPath)');
@(_DeploymentManifestEntryPoint->'$(_DeploymentApplicationDir)%(TargetPath)$(_DeploymentFileMappingExtension)');
@(_DeploymentManifestFiles->'$(_DeploymentApplicationDir)%(TargetPath)$(_DeploymentFileMappingExtension)');
@(ReferenceComWrappersToCopyLocal->'$(_DeploymentApplicationDir)%(FileName)%(Extension)$(_DeploymentFileMappingExtension)');
@(ResolvedIsolatedComModules->'$(_DeploymentApplicationDir)%(FileName)%(Extension)$(_DeploymentFileMappingExtension)');
@(_DeploymentLooseManifestFile->'$(_DeploymentApplicationDir)%(FileName)%(Extension)$(_DeploymentFileMappingExtension)')"
SkipUnchangedFiles="true"
OverwriteReadOnlyFiles="$(OverwriteReadOnlyFiles)"/>
<Copy
SourceFiles="@(_DeploymentManifestDependencies)"
DestinationFiles="@(_DeploymentManifestDependencies->'$(_DeploymentApplicationDir)%(TargetPath)$(_DeploymentFileMappingExtension)')"
SkipUnchangedFiles="true"
OverwriteReadOnlyFiles="$(OverwriteReadOnlyFiles)"
Condition="'%(_DeploymentManifestDependencies.DependencyType)'=='Install'"/>
<Copy
SourceFiles="@(_ReferenceScatterPaths)"
DestinationFiles="@(_ReferenceScatterPaths->'$(_DeploymentApplicationDir)%(Filename)%(Extension)$(_DeploymentFileMappingExtension)')"
SkipUnchangedFiles="true"
OverwriteReadOnlyFiles="$(OverwriteReadOnlyFiles)"/>
<FormatUrl InputUrl="$(_DeploymentApplicationUrl)">
<Output TaskParameter="OutputUrl" PropertyName="_DeploymentFormattedApplicationUrl"/>
</FormatUrl>
<FormatUrl InputUrl="$(_DeploymentComponentsUrl)">
<Output TaskParameter="OutputUrl" PropertyName="_DeploymentFormattedComponentsUrl"/>
</FormatUrl>
</Target>
Note that the space only causes a problem with MAGE.exe, it does not cause a problem with MAGEUI.exe! MAGEUI.exe happily re-signs the release even with the space in the directory name. We cannot use MAGEUI.exe however because this process needs to be automated, if you use MAGEUI.exe someone has to run it and manually set the values…not good enough.
It took a while to figure out that the space in the dir name was causing a problem so I hope this saves someone else some time…
Creating a Release
Ok so now we have a published build that no one can use, the first place we want to release to will be QA, then from there UAT so what we need now is an automated way of creating a release configured for these environments.
Creating a release should be as easy as publishing a build, it should be a one step process.
Create a batch file ‘createRelease.bat’ which performs all of the necessary steps. Those steps are:
- Create or clean the ‘working’ directory.
- Copy the master build into the working directory.
- Run the MsBuild steps.
- Create a zip file of the new release. (not necessary just makes it a little easier to move the releases around)
Run the MsBuild Steps
Add a new target in your customized.targets file, ModifyMasterBuildForEnvironment.
<Target Name="ModifyMasterBuildForEnvironment" DependsOnTargets="SetPropertyValues; ConfigureForEnvironment; RecreateManifests" />
SetPropertyValues
There are a few properties which need to be set for the resigning process.
<Target Name="SetPropertyValues" >
<Message Text="FullReleasePath $(FullReleasePath)" />
<MSBuild.ExtensionPack.Framework.TextString TaskAction="Replace" OldString="$(ApplicationVersion)" OldValue="." NewValue="_">
<Output PropertyName="buildX" TaskParameter="NewString"/>
</MSBuild.ExtensionPack.Framework.TextString>
<PropertyGroup>
<WorkingDirectory>WorkingDirectory</WorkingDirectory>
<FullReleasePath>$(WorkingDirectory)\ApplicationFiles\$(AssemblyName)_$(buildX)</FullReleasePath>
<ApplicationManifestName>$(AssemblyName).exe.manifest</ApplicationManifestName>
<ApplicationManifestPath>$(FullReleasePath)\$(ApplicationManifestName)</ApplicationManifestPath>
<DeploymentManifestPath>$(WorkingDirectory)\$(DeploymentManifestName)</DeploymentManifestPath>
<CertFileName>cert\pmsKey.pfx</CertFileName>
<ProviderFullUrl>$(ProviderBaseUrl)$(DeploymentManifestName)</ProviderFullUrl>
</PropertyGroup>
</Target>
ConfigureForEnvironment
Add your own target(s) here that does the work to configure the release for a given environment. e.g. update the config file setting database connection strings or web service urls.
RecreateManifests
Now that you have modified the published release it is no longer a valid ClickOnce release. Try to install it and it will fail. What you need to do is update and re-sign the manifests (the Application Manifest and the Deployment Manifest). You also need to update and resign the Setup.exe!
<Target Name="RecreateManifests">
<!-- Application manifest update and resign -->
<Message Text="Removing .deploy suffix..." />
<RenameFiles DirectoryPath="$(MSBuildProjectDirectory)\$(FullReleasePath)" RemoveSuffix=".deploy" IncludeSubDirectories="True" />
<Message Text="Updating application manifest" />
<Exec Command="mage -Update $(ApplicationManifestPath) -FromDirectory $(FullReleasePath) -ToFile $(ApplicationManifestPath)" />
<Message Text="Adding .deploy suffix..." />
<RenameFiles DirectoryPath="$(MSBuildProjectDirectory)\$(FullReleasePath)" AppendSuffix=".deploy" IncludeSubDirectories="True" ExcludedFiles="$(ApplicationManifestName)" />
<Message Text="ApplicationManifestPath $(ApplicationManifestPath)" />
<Exec Command="mage -Sign $(ApplicationManifestPath) -CertFile $(CertFileName)" />
<!-- Deployment manifest update and resign -->
<Message Text="Updating Deployment manifest (ProviderFullUrl=$(ProviderFullUrl))" />
<Exec Command="mage -Update $(DeploymentManifestPath) -AppManifest $(ApplicationManifestPath) -ProviderUrl $(ProviderFullUrl)" />
<Message Text="DeploymentManifestPath $(DeploymentManifestPath)" />
<Exec Command="mage -Sign $(DeploymentManifestPath) -CertFile $(CertFileName) " />
<!-- Setup.exe update and resign -->
<Message Text="Updating $(WorkingDirectory)\Setup.exe..." />
<Exec Command="$(WorkingDirectory)\Setup.exe /url=$(ProviderBaseUrl)" />
<Message Text="Signing $(WorkingDirectory)\Setup.exe..." />
<Exec Command="signtool sign /v /f $(CertFileName) $(WorkingDirectory)\Setup.exe" />
</Target>
Note that before Updating the Application Manifest we have to remove the .deploy extension from the file names, then add the .deploy extension back onto the file names before Signing. (if you have configured ClickOnce to NOT append .deploy then obviously just remove these steps).
RenameFiles Task
To get the renaming behaviour I wanted I had to create my own MsBuild task. You will need to do the same, it’s a little crude as I had limited time but this is the simple little task I created….
public class RenameFiles : Task
{
public override bool Execute()
{
var directory = new DirectoryInfo(DirectoryPath);
if(directory == null)
throw new ApplicationException(string.Format("Directory path is invalid: {0}", DirectoryPath));
Console.WriteLine(string.Format("DirectoryPath:{0}", DirectoryPath));
ProcessDirectory(directory);
return true;
}
private void ProcessDirectory(DirectoryInfo directory)
{
RenameAllFiles(directory);
if(! IncludeSubDirectories)
return;
var subs = directory.GetDirectories();
foreach (DirectoryInfo sub in subs)
ProcessDirectory(sub);
}
private void RenameAllFiles(DirectoryInfo directory)
{
var files = directory.GetFiles();
if(files.Length == 0)
return;
foreach (FileInfo file in files)
{
string fileName = file.Name;
if (ExcludedFileList.Exists(name => name.Equals(fileName, StringComparison.InvariantCultureIgnoreCase)))
{
Console.WriteLine(string.Format("{0} is excluded from renaming.", file.Name));
continue;
}
Console.WriteLine(string.Format("Renaming {0}", file.Name));
RemoveExtensions(file);
AppendExtensions(file);
}
}
private void RemoveExtensions(FileInfo file)
{
if (string.IsNullOrEmpty(RemoveSuffix))
return;
file.MoveTo(file.FullName.Replace(RemoveSuffix, string.Empty));
}
private void AppendExtensions(FileInfo file)
{
if (string.IsNullOrEmpty(AppendSuffix))
return;
file.MoveTo(string.Format("{0}{1}", file.FullName, AppendSuffix));
}
public string DirectoryPath { get; set; }
public string RemoveSuffix { get; set; }
public string AppendSuffix { get; set; }
public string ExcludedFiles { get; set; }
public bool IncludeSubDirectories { get; set; }
private List<string> ExcludedFileList
{
get
{
if (excludedFileList == null)
excludedFileList = CommonFunctions.CreateListFromCommaSeparatedString(ExcludedFiles);
return excludedFileList;
}
}
private List<string> excludedFileList;
}
Summary
There are a number of other little steps we do to make this release process work here but these are really specific to how we want it to work. What I have outlined above is the core steps involved including some problems you will face and their solutions. I hope this posting helps you get over the most challenging of the hurdles you will face in setting a smooth ‘Master Build’ release process but there is still alot of extra steps you will need to create yourself to get it working end to end.
One thing I would recommend is, once you have a working createRelease.bat (note it doesn’t have to be a batch file, could be an exe or powershell or whatever works best in your situation) then create a createReleaseALL.bat file. In there make n calls to createRelease.bat…one for each environment you need to create releases for. That way if you have a dozen environments you can still create a release for all of them with a one step process 🙂
I have had to do this fairly quickly so if something is not clear please let me know and I will see if I can improve the post over time.
Final note – It does take time to set this up and get it working correctly, but it is so worth it! Over time you will save much more time than you spent setting it up, guaranteed!
Additional Resources
A few links which helped me to piece this puzzle together…
ClickOnce with bootstraper setup.exe need to change URL without any build
packaging-a-clickonce-server-deployment