How to: Create an Automated Build Process

This topic describes how to create an automated build process when you are managing your customizations to the application using the version control system built into Microsoft Dynamics AX.
The build is done on a dedicated machine which can be wiped before each build, to reduce risk of residual impacts.

The automated build process requires that all XPO files are combined into a single XPO file. You can use the CombineXPO tool to do this. The tool is available on PartnerSource. Download the tool and place it into the C:\Repository\Build\Tools folder. (You can place it in a different folder; change the first command in the template build script below if you do).
You should also create a folder in which to drop the builds. This is referred to as the DropFolder in the template script below.

The following steps are needed to build the application. A build script template is provided further down in this topic.
  1. Set variables for the layer, AOS, database, version control system, and build folders.
  2. Get the latest version of all Microsoft Dynamics AX files.
  3. Stop the AOS and delete the AOD file for the layer you want to build.
  4. Create a template database.
    This is a detached database created from a clean installation of Microsoft Dynamics AX. It provides an easy way to set up a clean environment to build on.
  5. Restart the AOS.
  6. Load the license file.
  7. Synchronize the application database.
  8. Synchronize the build number between the application and database.
    (This prevents the installation checklist from appearing the first time the new build is opened.)
  9. Set up the version control system settings in AX.
  10. Combine all the XPOs into a single XPO, and import the new version of the layer into AX.
  11. Synchronize the application database.
  12. Update the build number for the new layer.
  13. Compile the application.

Use the commands below to create your own build script, and then run this as a batch file. Change the values of the variables to fit your environment.
NoteNote
This script uses an installed Microsoft Dynamics AX application as the base for the build environment and makes use of a template database.

@rem 1.0 Set Variables

@rem Set the path to the location of the script.
set CmdPath=C:\Repository\Build
  
@rem Set the layer to build.
set Layer=VAR
 
@rem Set the layercode for the layer to build
set VARLayerCode=
set BUSLayerCode=
 
@rem
@rem Set environment variables used during the build.
@rem
 
@rem AX Settings
 
@rem Set the version of the AX application.
set AxVersion=40
set AxVersionWithDot=4.0
 
@rem Set the ID of the AOS.
Set AosInstanceId=01
 
@rem Set the name of the AOS service.
set AOSServiceName=AOS$%AosInstanceId%
 
@rem Set the login for the AOS.
Set AosLocalLogin=NT AUTHORITY\NETWORK SERVICE
 
@rem Set the path to the installed AX Application folder.
Set ApplicationInstallPath=C:\Program Files\Microsoft Dynamics AX\40\Application
 
@rem Set the path to the installed AX Client folder.
Set ClientInstallPath=C:\Program Files\Microsoft Dynamics AX\40\Client
 
@rem Set the name of the AX configuration.
Set ConfigurationName=Original (installed configuration)

@rem Set the AX registry key.
Set CURegKey=HKCU\Software\Microsoft\Dynamics\%AxVersionWithDot%
 
@rem Database settings
Set DbSqlServer=%COMPUTERNAME%
Set DbSqlDatabaseName=DynamicsAX
Set DbSqlOwner=DBO
 
@rem VSS settings
 
@rem Set the path to the VSS ini file.
Set VSSINIFILE=\\server\share\srcsafe.ini
 
@rem Set the project root in VSS.
Set VSSPROJECTROOT=$/Project
 
@rem Set the path to the VSS executable.
Set VSSCommand=%ProgramFiles%\Microsoft Visual SourceSafe\SS.exe
 
@rem Set the path to the VSS share.
Set SSDIR=\\CTDB07A\DAXVSS
 
@rem Set the the repository folder on the build machine.
Set RepositoryFolder=C:\Repository\Project
 
@rem Settings for Team Server (the ID server)
 
@rem VCSENABLED, 0=Yes 1=No
Set VCSENABLED=0
 
@rem VCSTYPE, 0=SourceDepot 1=VSS
Set VCSTYPE=1
 
@rem TSTYPE, 0=SQL 1=Oracle
Set TSTYPE=0
 
@rem Set the Team Server name.
Set TSNAME=CTDB07A
 
@rem Set the Team Server database.
Set TSDBNAME=AXTS
 
@rem Set the Team Server UI flag.
@rem Color code objects checked out from the AOT (+1) and on Revert (+2)
Set UIFLAGS=3
 
@rem Get the date and time for the VSS label and for the drop folder.
@rem This may vary according to language/location settings on the machine.

for /f "tokens=2,3,4 delims=^/ " %%i in ("%DATE%") do (
    Set StrDD=%%i
    Set StrMM=%%j
    Set StrYY=%%k
)
 
for /f "tokens=1,2,3 delims=:." %%i in ("%TIME%") do (
    Set /A StrHH=%%i
    Set StrMI=%%j
    Set StrSS=%%k
)
 
@rem Set the build label. 
Set BuildLabel=%StrYY%-%StrMM%-%StrDD%-%StrHH%-%StrMI%-%StrSS%
 
@rem Build settings
 
@rem Set the drop folder. 
@rem This folder should have been created before to hold all builds.
Set DropFolder=\\server\share\ax-builds
 
@rem Set the folder for the current drop ("daily drop folder").
Set DailyDropFolder=%DropFolder%\%BuildLabel%
 
@rem Create the daily drop folder.
MD %DailyDropFolder%
 
@rem Signal that a build has started.
echo In progress>%DailyDropFolder%\BuildInProgress.txt
 
@rem Set the log folder.
Set LogDir=%DailyDropFolder%\Logs
 
@rem Set the build log file name.
Set BuildLog=%LogDir%\Build.log
 
@rem Set the build error log file name.
Set BuildErrorLog=%LogDir%\Build.err
 
@rem Create the log folder.
MD %LogDir%
 

@rem 2.0 Get the Latest Files from VSS

@rem Execute the following in the Repository folder.
pushd %RepositoryFolder%
 
@rem Set the working folder for VSS.
"%VSSCommand%" Workfold %VSSPROJECTROOT% "%RepositoryFolder%"
 
@rem Create the file that contains the timestamp (label) for the build. 
echo %BuildLabel%>%DailyDropFolder%\Label.txt
 
@rem Add a label to all the VSS files used for the build.
"%VSSCommand%" Label %VSSPROJECTROOT% -L%BuildLabel% -C"Label for build %BuildLabel%"
 
@rem Get the latest files.
"%VSSCommand%" Get %VSSPROJECTROOT% -GWR -R -Vl%BuildLabel%
 
@rem Go back to the build script folder.
popd
 

@rem 3.0 Stop the AOS and Delete the AOD File

@rem Stop the AOS.
call net stop %AOSServiceName%
 
@rem Delete the layer to be built if it already exists in the Application folder.
del /f /q "%ApplicationInstallPath%\Appl\Standard\ax%Layer%.aod"
del /f /q "%ApplicationInstallPath%\Appl\Standard\ax%Layer%.add"
del /f /q "%ApplicationInstallPath%\Appl\Standard\ax%Layer%.ahd"
del /f /q "%ApplicationInstallPath%\Appl\Standard\ax%Layer%.khd"
del /f /q "%ApplicationInstallPath%\Appl\Standard\axapd.aoi"
 

@rem 4.0 Create a New AX Database

@rem Take the AX database offline.
osql.exe -S %DbSqlServer% -E -d master -Q "if (select count(*) from sysdatabases where name = '%DbSqlDatabaseName%') != 0 alter database %DbSqlDatabaseName% set offline with rollback immediate"
 
@rem Drop the AX database.
osql.exe -S %DbSqlServer% -E -d master -Q "if (select count(*) from sysdatabases where name = '%DbSqlDatabaseName%') != 0 drop database %DbSqlDatabaseName%"
 
@rem Add AOS login.
osql -S %DbSqlServer% -E -d master -Q "if not exists(select * from master.dbo.syslogins where loginname = '%AosLocalLogin%') exec sp_grantlogin '%AosLocalLogin%'"
 
@rem Attach %DbSqlDatabaseName% database files.
osql -S %DbSqlServer% -E -d master -Q "exec sp_attach_db @dbname = N'%DbSqlDatabaseName%',@filename1 = N'%CmdPath%\SQL\%DbSqlDatabaseName%.MDF',@filename2 = N'%CmdPath%\SQL\%DbSqlDatabaseName%_Log.LDF'"
 

@rem 5.0 Restart the AOS

@rem Start the AOS.
net start %AOSServiceName%

@rem 6.0 Load the License File

"%ClientInstallPath%\Bin\Ax32.exe -lazyclassloading -StartupCmd=LoadLicense_%LicenseFile%"

@rem 7.0 Synchronize the Application Database

"%ClientInstallPath%\Bin\Ax32.exe -lazyclassloading -StartupCmd=Synchronize" 
 

@rem 8.0 Synchronize the Build Number Between the Application and Database

"%ClientInstallPath%\Bin\Ax32.exe -lazyclassloading -StartupCmd=UpdateBuildNo"

@rem 9.0 Set up the Version Control System Settings in AX

call osql.exe -S %DbSqlServer% -E -d %DbSqlDatabaseName% -Q "UPDATE [%DbSqlOwner%].SYSVERSIONCONTROLPARAMETERS SET VCSENABLED = %VCSENABLED%"
call osql.exe -S %DbSqlServer% -E -d %DbSqlDatabaseName% -Q "UPDATE [%DbSqlOwner%].SYSVERSIONCONTROLPARAMETERS SET VCSTYPE = %VCSTYPE%"
call osql.exe -S %DbSqlServer% -E -d %DbSqlDatabaseName% -Q "UPDATE [%DbSqlOwner%].SYSVERSIONCONTROLPARAMETERS SET TSTYPE = %TSTYPE%"
call osql.exe -S %DbSqlServer% -E -d %DbSqlDatabaseName% -Q "UPDATE [%DbSqlOwner%].SYSVERSIONCONTROLPARAMETERS SET TSNAME = '%TSNAME%'"
call osql.exe -S %DbSqlServer% -E -d %DbSqlDatabaseName% -Q "UPDATE [%DbSqlOwner%].SYSVERSIONCONTROLPARAMETERS SET TSDBNAME = '%TSDBNAME%'"
call osql.exe -S %DbSqlServer% -E -d %DbSqlDatabaseName% -Q "UPDATE [%DbSqlOwner%].SYSVERSIONCONTROLPARAMETERS SET UIFLAGS = %UIFLAGS%"
call osql.exe -S %DbSqlServer% -E -d %DbSqlDatabaseName% -Q "UPDATE [%DbSqlOwner%].SYSVERSIONCONTROLPARAMETERS SET REPOSITORYFOLDER = '%RepositoryFolder%'"
call osql.exe -S %DbSqlServer% -E -d %DbSqlDatabaseName% -Q "UPDATE [%DbSqlOwner%].SYSVERSIONCONTROLPARAMETERS SET VSSINIFILE = '%VSSINIFILE%'"
call osql.exe -S %DbSqlServer% -E -d %DbSqlDatabaseName% -Q "UPDATE [%DbSqlOwner%].SYSVERSIONCONTROLPARAMETERS SET VSSPROJECTROOT = '%VSSPROJECTROOT%'"
 
@rem Recycle the AOS to make the Team Server settings effective.
net stop %AOSServiceName%
net start %AOSServiceName%
 

@rem 10.0 Combine all the XPOs into a Single XPO, and Import the New Version of the Layer into AX

 
@rem Skip setting the layer if the layer is USR.
@rem (USR layer is the default layer.)
if /i "%Layer%"=="USR" goto :SkipLayerSet
 
@rem Set the layer code.
if /i "%Layer%"=="VAR" Set LayerCode=%VARLayerCode%
if /i "%Layer%"=="BUS" Set LayerCode=%BUSLayerCode%
 
@rem Import the layer code and the layer into the registry.
REG ADD "%CURegKey%\Configuration\%ConfigurationName%" /v aol /t REG_SZ /d "%Layer%" /f
REG ADD "%CURegKey%\Configuration\%ConfigurationName%" /v aolcode /t REG_SZ /d "%LayerCode%" /f
 
:SkipLayerSet
 
@rem Combine the XPO files from the repository.
%CmdPrefix%Tools\CombineXPOs.exe -xpodir %RepositoryFolder%\%Layer% -combinedxpofile %RepositoryFolder%\%Layer%.xpo -utf8 -verbose > %LogDir%\%Layer%.log
 
@rem Import the layer into AX.
"%ClientInstallPath%\Bin\Ax32.exe -AOTIMPORTFILE=%RepositoryFolder%\%Layer%.xpo -lazyclassloading -lazytableloading -minimize"
 

@rem 11.0 Synchronize the Application Database

"%ClientInstallPath%\Bin\Ax32.exe -StartupCmd=Synchronize -lazyclassloading -lazytableloading -minimize"
 

@rem 12.0 Update the Build Number for the New Layer

"%ClientInstallPath%\Bin\Ax32.exe -StartupCmd=UpdateBuildNo -lazyclassloading -lazytableloading -minimize"
 

@rem 13.0 Compile the Application

"%ClientInstallPath%\Bin\Ax32.exe -StartupCmd=CompileAll -lazyclassloading -lazytableloading -minimize"
 
@rem Copy the Compile log to the drop share.
copy /y "%ClientInstallPath%\Log\AxCompileAll.html" %DailyDropFolder%\Logs\AxCompileAll%Layer%.html
 
@rem Stop the AOS to be able to copy the layer.
net stop %AOSServiceName%
 
@rem Copy the layer to the drop share.
copy /y "%ApplicationInstallPath%\Appl\Standard\ax%Layer%.aod" %DailyDropFolder%
 
@rem Update the progress file.
del /q %DailyDropFolder%\BuildInProgress.txt
echo Completed>%DailyDropFolder%\BuildCompleted.txt

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.