IPABS 3 Configuration and Build Management

Download Report

Transcript IPABS 3 Configuration and Build Management

IPABS 3 Configuration and Build Management
Richard Rush
IPABS 3 Configuration and Build Management
Web.config Management
Installer extension
Build automation
Version Numbering
Future work
1
Web.config Management
Implemented specifically to avoid the woes of multiple
web.configs
Use VisualStudio’s built-in Build Configuration features, as
well as pre-compile commands
2
Web.config Management
Each target environment gets its own named Build
Configuration.
For IPABS, these are:
Debug
Test
UAT
Staging
Release
Each Build Configuration gets a Web.config file, named
Web.config.[Build Configuration]
3
Web.config Management
We’re using source control, however, which means
Web.config might not always be editable, so first:
attrib -R "$(ProjectDir)Web.config"
Then, we use a pre-compile command to copy the
appropriate Web.config.[Build Configuration] over
Web.config
"$(ProjectDir)utils\CopyIfDifferent.bat"
"$(ProjectDir)web.config.$(ConfigurationName)"
"$(ProjectDir)web.config“
4
CopyIfDifferent.bat
@echo off
echo Comparing two files: %1 with %2
if not exist %1 goto File1NotFound
if not exist %2 goto File2NotFound
fc %1 %2
if %ERRORLEVEL%==0 GOTO NoCopy
echo Files are not the same.
copy %1 %2 /y & goto END
:NoCopy
echo Files are the same.
goto END
Copying %1 over %2
Did nothing
:File1NotFound
echo %1 not found.
goto END
:File2NotFound
copy %1 %2 /y
goto END
:END
echo Done.
5
Pre-build events
6
Installer Extension
Automate tedious, repetitive tasks
Produce a more professional looking product
Enable non-developers to deploy the application
7
Example: Web.config Encryption
Encrypt sections of Web.config
AppSettings
ConnectionStrings
Previously done with a batch file
aspnet_regiis -pe "appSettings" -app "/IPABS3"
aspnet_regiis -pe "connectionStrings" -app "/IPABS3"
Now done as part of the installation process
8
Extending the Installer
1.
2.
3.
4.
5.
Add the custom actions to the installer project
Add new dialogs to the installer user interface
Provide parameters you need to the custom actions
Define the custom Installer class
Override the action methods
9
View → Custom Actions
10
User Interface → Add Dialog
11
View → User Interface
12
Encrypt Web.Config Dialog
13
Custom Action Parameters
Follow the format ‘/ParameterName=“ParameterValue”’
14
Custom Action Parameters
The parameters to the custom Commit action are:
/targetvdir="[TARGETVDIR]"
/EncryptAppSettingsCheckbox="[ENCRYPTAPPSETTINGSCHECKBOX]"
/EncryptConnectionStringsCheckbox="[ENCRYPTCONNECTIONSTRINGSCHECKBOX]"
These values are being pulled from the Installer
configuration and Installer UI controls.
15
Custom Installer Actions
Define an Installer class
Inherit from System.Configuration.Install
[RunInstaller(true)]
Implement the custom action behavior
public override void Commit(IDictionary savedState) { }
Call the base implementation
base.Commit(savedState);
Access Installer data
EncryptWebConfig(Context.Parameters["TargetVDir"]);
16
Example: Custom Installer Actions
using System.Configuration.Install;
namespace PPC.Ipabs {
/// <summary>
/// Custom IPABS Web Application Installer
/// </summary>
[RunInstaller(true)]
public partial class IpabsInstaller : Installer {
/// <summary>
/// Handles the Commit action for IPABS 3
/// </summary>
/// <param name="savedState">State Dictionary</param>
public override void Commit(IDictionary savedState) {
base.Commit(savedState);
try {
EncryptWebConfig(Context.Parameters["TargetVDir"]);
} catch (Exception ex) {
// Handle the exception!
}
}
}
17
Example: Encrypting Web.config
/// <summary>
/// Encrypts the Web.config
/// </summary>
/// <param name="applicationName">Application to encrypt the Web.config for</param>
private void EncryptWebConfig(string applicationName) {
bool encryptConnectionStrings;
Configuration configuration;
// Get the installer
encryptConnectionStrings =
Convert.ToBoolean((int)Context.Parameters["EncryptConnectionStringsCheckbox"]);
configuration = WebConfigurationManager.OpenWebConfiguration("/"+ applicationName);
// Protect the ConnectionStrings section
if (encryptConnectionStrings && !configuration.ConnectionStrings.SectionInformation.IsProtected) {
configuration.ConnectionStrings.SectionInformation.ProtectSection(String.Empty);
}
// Save the configuration
try { configuration.Save(); }
catch (Exception ex) { //TODO: Handle the exception! }
}
18
Build Automation
Done to avoid tedious, repetitive tasks
Produce a build for each configuration simultaneously
Currently done with a batch file
19
BuildAllConfigurations.bat
Called with the format:
BuildAllConfigurations InstallProjectName
BuildConfigurationName[,BuildConfigurationName+]
So:
BuildAllConfigurations IPABS Debug,Test,UAT,Release
20
BuildAllConfigurations.bat Pseudocode
1.
2.
3.
4.
Load the VisualStudio 2005 environment variables
Determine the timestamp
Clean the solution
Build each specified configuration
:PERFORM_BUILD
IF (%1)==() GOTO DONE_BUILDING
ECHO Building %1...
RMDIR /S /Q %1
DEVENV %ProjectName%.vdproj /build %1
COPY /Y "%1\%ProjectName%.msi" "Builds\%date% %ProjectName%\%1 %date%
%ProjectName%.msi"
ECHO Done building %1...
SHIFT
GOTO PERFORM_BUILD
:DONE_BUILDING
21
BuildAllConfigurations.bat Results
22
Version Numbering
Version numbers are attributes of an assembly
Can be accessed at runtime through reflection
Can be significant when referencing external libraries
Useful for tracking features (versioning? crazy talk!)
23
Version Numbering
Microsoft’s Recommended format:
[Major Version].[Minor Version].[Build Number (yyMMdd)].[Revision]
Software Solutions Development’s common library format:
[Major Version].[Minor Version].[Build Number (yyMM)].[Revision]
Why the difference?
Revision is handled as a short (int16) internally
(int)070101 > 65,535
With the yyMMdd format, anything from 2007 onward is too large
24
Version Numbering
Are set in AssemblyInfo.cs (or AssemblyInfo.vb)
Two attributes:
AssemblyVersion
Can be automatically incremented
<Assembly: AssemblyVersion("1.0.0.*")>
Displayed in the Properties Screen of the .dll
AssemblyFileVersion
Cannot be automatically incremented
<Assembly: AssemblyFileVersion("1.0.0802.3")>
Displayed in Windows Explorer
25
Version Numbering
We want to use AssemblyFileVersion
We want it automated
Custom MSBuild task: AssemblyInfoTask
Developed by the MSBuild team
Source available from GotDotNet
Modified installer assembly with SSD’s version number format
VisualStudio build process does not provide the Build
Configuration to its implementation of MSBuild
This will… complicate things
26
Using PPC AssemblyInfoTask
1. Run “PPC AssemblyInfoTask.msi”
Select “Install to User’s Application Data folder”
2. Update the project definition file (.csproj or .vbproj)
Add a call to the AssemblyInfoTask build task
3. Check out AssemblyInfo.cs (or AssemblyInfo.vb)
AssemblyInfoTask will be updating this file
4. Build the library using MSBuild, not VisualStudio
Set up the MSBuild external tool
Build using this when building to Release
27
PPC AssemblyInfoTask Installer
28
Update .csproj or .vbproj
<Project>
<!-– Lots of stuff omitted here -->
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- Or, as appropriate:
<Import Project="$(MSBuildToolsPath)\Microsoft.VisualBasic.targets" />
-->
<!-- Here’s where we make the call to the custom build action -->
<Import
Project="$(APPDATA)\Microsoft\MSBuild\AssemblyInfoTask\Microsoft.VersionNumber.Targets"
Condition=" '$(Configuration)' == 'Release' "
/>
</Project>
29
External Tools Dialog
Command:
Path to MSBuild.exe
Arguments:
$(ProjectDir)$(ProjectFileName)
/m
/p:Configuration=Release
Initial Directory
$(ProjectDir)
Use Output window
Prompt for arguments
30
Future Work
Automate source control access
Visual SourceSafe
Team Foundation
Is this as bad an idea as I think it is?
MSBuild automation?
Why use DEVENV in the build script when we could use MSBuild?
Custom MSBuild tasks won’t work with DEVENV
.Net Console Application?
Why use a .bat file when we could use managed code?
Third Party Tools
Who likes reinventing the wheel?
31