AdvaniaGIT: Configure Build and Test using Visual Studio Online

The goal of this post is to demo from start to finish the automated build and test of an AL solution for Microsoft Dynamics 365 Business Central.

Configure build steps

The build steps using AdvaniaGIT are similar to the build step Soren describes here.

The first two steps and the last one is automatically created.  We create PowerShell steps to be executed in between.

To begin with I like to make sure that the previous build environment is removed.  If everything is configured and working correctly the environment is removed in the end of every build.

Since I am working on daily builds I like to leave the environment active after the build so that I can access the latest environment any time to look at code and configuration changes.

When putting together this build I used some of the information found on Tobias Fenster blog and also from Kamil Sacek blog.

Remove existing container

Every PowerShell  task executes the basic command with different parameters.  The script path is

C:\AdvaniaGIT\Scripts\Start-CustomAction.ps1

The Arguments are defined for each task.  I always select which script to execute and always add the ‘BuildMode=$true’ settings.

-ScriptName Remove-NavEnvironment.ps1 -BuildSettings @{BuildMode=$true}

Remember that the ‘setup.json’ configuration and the machine configuration in the read as every action is started.

the ‘setup.json’ parameter ‘projectName’ is used as the docker container name.  This first action will look for a container matching the ‘projectName’ and remove it.

Build and Update Docker Container
-ScriptName Build-NavEnvironment.ps1 -BuildSettings @{dockerMemoryLimit='4G';BuildMode=$true;dockerTestToolkit=$true}

This task will start download the docker image that is defined in the ‘dockerImage’ property in ‘setup.json’. 

To make sure that docker allocates enough memory for the build use the ‘dockerMemoryLimit’ parameter and allow at least 4G of memory.

The ‘dockerTestToolkit’ parameter is required if the AL Test application uses the standard test toolkit libraries.  The G/L Source Names test application uses some of the standard libraries and therefore, using this parameter, I trigger the toolkit import into my docker container.

Initialize Test Company
-ScriptName Initialize-NAVCompany.ps1 -BuildSettings @{BuildMode=$true}

The daily builds are not shipped with the CRONUS company.  To be able to execute the test using the NAV Client I need to have a company marked for evaluation.

This action will remove the existing company, create a new evaluation company and run the Company Initialize Codeunit.

If you are not running tests then this step is not needed.

Download AL Add-in
-ScriptName Download-ALAddin.ps1 -BuildSettings @{BuildMode=$true}

The build machine will create a folder in ‘C:\AdvaniaGIT\Workspace’ using the ‘branchId’ from ‘setup.json’.  That said, we can only have one build process running for each GIT branch.  I have not found the need to allow multiple concurrent builds for a GIT branch.  

The AL extension for VS Code is downloaded and extracted to the ‘vsix’ folder.

Download AL Symbols
-ScriptName Download-ALSymbols.ps1 -BuildSettings @{BuildMode=$true}

The symbol files (app files) are required to build the AL application.  These two files are downloaded from the docker container into our work space ‘Symbols’ folder.

Build AL Solution
-ScriptName Build-ALSolution.ps1 -BuildSettings @{BuildMode=$true;buildID=$(Build.BuildID)}

The AL app will be compiled and created in the work space ‘out’ folder.  I add the ‘Build.BuildID’ from Visual Studio to my app version in every build.  Remember that the AL solution is in the AL folder on my GIT branch as stated in the machine configuration.  Overwrite that configuration in ‘setup.json’ or with ‘BuildSettings’ parameter if needed.

Copy AL Solution to Symbols
-ScriptName Copy-ALSolutionToSymbols.ps1 -BuildSettings @{BuildMode=$true}

The ‘APP’ file is copied from the work space ‘out’ folder to the work space ‘Symbols’ folder.  This is required since I am building the test app on top of the solution app.  If you are not running tests the you can skip this and the next five tasks.

Build AL Test Solution
-ScriptName Build-ALTestSolution.ps1 -BuildSettings @{BuildMode=$true;buildID=$(Build.BuildID)}

This task is almost identical to the AL solution build task.  Here the machine parameters for the AL solution and the Test solution are combined into a single folder ‘ALTests’ to locate the folder containing the AL test application source code.

The test application ‘APP’ file is copied to the work space ‘out’ folder.

Install AL Extension
-ScriptName Install-ALExtensionsToDocker.ps1 -BuildSettings @{BuildMode=$true}

All the ‘APP’ files from the work space ‘out’ folder are published and installed in the docker container.

Execute AL Test Codeunits
-ScriptName Start-ALTestsExecution.ps1 -BuildSettings @{BuildMode=$true}

This task requires the ‘idRange’ in the ‘app.json’ to be specified.

Every Codeunit in this number range with ‘subtype=upgrade’ will be added to the test pipeline.

The server instance is restarted before the tests are executed.  I have asked Microsoft to do one code change to be able to skip this step.

The tests are then executed using the NAV Client that is copied to the build machine and started from there.

Save Test Results
-ScriptName Save-TestResults.ps1 -BuildSettings @{BuildMode=$true;TestResultsPath='TEST-Build_$(Build.BuildID).trx'}

Test results are downloaded from the docker container database and formatted into the VSTest Xml format.  The ‘TestResultPath’ is a sub folder of the repository path.

Publish Test Results

This task is a built-in Visual Studio task.  The test result files must match the ‘TestResultPath’ in the previous step.

The ‘$(System.DefaultWorkingDirectory)’ is the repository path.

Copy AL Solution to Artifact folder
-ScriptName Copy-ALSolutionToArtifactStagingDirectory.ps1 -BuildSettings @{BuildMode=$true}

The ‘Artifact’ folder is created in the repository folder and every ‘APP’ in the work space ‘out’ folder is copied to this folder.

Sign AL Solution in Artifact folder
-ScriptName Sign-ArtifactAppPackage.ps1 -BuildSettings @{BuildMode=$true}

The code signing certificate is needed and must be available on the build machine.  The ‘signtool.exe’ to be used, the certificate path and the certificate password are specified in the machine setup ‘GITSettings.json’.

Publish Artifact: App

This is also a Visual Studio task that will upload all files in the ‘Artifact’ sub folder in the GIT repository to the build artifacts.

Remove Container
-ScriptName Remove-NavEnvironment.ps1

In my setup I disable this and the next task.  This is done so that I can work with the results and the development environment after the build.  This task is also configured to be executed even if something fails in the build pipeline.

Remove Branch Work Folder
-ScriptName Remove-BranchWorkFolder.ps1 -BuildSettings @{BuildMode=$true}

The folder created in the work space by adding the branch ID will take up space on the build machine.  This task will remove that folder and everything it contains.

Delete Logs
-ScriptName Delete-OldLogs.ps1 -BuildSettings @{BuildMode=$true}

When AdvaniaGIT executes custom actions a log folder is created.  The log folders are located in ‘C:\AdvaniaGIT\Log’ folder.

When the NAV Client is copied to then host it is stored in a log folder.

Every sub folder that is older than seven days will be removed from the log folder in this task.

Conclusion

I configured my build machine as described in my previous post.

I use my free subscription to Visual Studio Online to store my source code in GIT.

I installed the build agent on my build server and connected to my Visual Studio Online subscription.

I added the docker repository information and login credentials to the ‘DockerSettings.json’ to be able to build the daily builds.

AdvaniaGIT is accessible on GitHub.

Good luck!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.