Skip to main content

CI/CD in NAV / Business Central Development using Azure DevOps

Introduction:

Azure DevOps is a very handy tool to manage project tasks, milestones, bugs, and documentation.
But it is not just limited to that, it can also be used to manage all your deployments and building pipelines to manage your deployments.
Lets take a look how to do this and how we can setup our repository to make auto deployments.

Pre-requisites:

  • VS Code
  • Microsoft Dynamics NAV / Business Central
  • Azure DevOps Subscription
  • Understanding of  Version Source Control System like Github or TFS
  • Understanding of PowerShell

Books & References:

Demonstration:

1. Creation of Azure DevOps Repository: Creation of Azure DevOps Repository is the starting point of the every code related activities in any projects. Azure DevOps Repository is one of the source control application from multiple application  such as TFS(VSTS) and GitHub.
Importing a Git Repository from AL Ops Template
Repository Created in Azure DevOps

2. Cloning Repository in VS Code:
After creating repository in Azure DevOps, the project is to be cloned to local project in the PC. Thus, after the cloning is done, you can create your project files which can then be synced to remote repository created on Azure DevOps.
Creation of Local Repository in VS Code

3. Creation of PowerShell script for auto compilation and deployment:
i. Copying the Compiler from VS Code existing extension compiler:
In order to compile the Business Central Project on the Windows machine, you require a compiler. Now, if the deployment has to happen automatically on random machine auto-created by Azure Pipelines, you also need to copy the Compiler from your Local Machine to your Remote Project folder. From there there the Compiler can be referenced and used to compile on the Remote Machine created.  

By Default the ALC (AL Compiler) is stored on path
C:\Users\<USERNAME>\.vscode\extensions\ms-dynamics-smb.al-<VERSION>\bin
In this case, I have copied this to the project folder like this

ii. Creating PowerShell to Compile the project based upon the objects created and  Symbols: Using the ALC, you can now compile the Project and Symbols as parameters.
The syntax for the project to compile is :
<Path to ALC>  /project:<PROJECT FOLDER PATH>  /out:<APP OUTPUT PATH> /packagecachepath:<PROJECT FOLDER PATH>\.AlPackages


AL Compile Syntax
iii. Creating PowerShell command to deploy the App File created to  Business Central using APIs: Whenever you hit Ctrl + F5 in VS Code, the VS Code actually calls the APIs in a PUT method to with the parameter as the .App File created after the compilation of the Business Central Project. Well even I'm going to do the same thing.
In terms of Authentication, I will be calling my APIs using Basic authentication which is Username and Webservice key defined in Users Page. Overall, I did it with the help of PowerShell below.
Deploy Business Central App using PowerShell

Thank you @FreddyK(https://twitter.com/freddydk) for providing me the reference material to call the Business Central APIs for App Development (http://aka.ms/cicdhol)

4. Creation of Azure Pipelines YAML: After all this has been set, now it is time to automate the procedure. Basically, I want the system to work as whenever I Commit and Push my code into Azure DevOps Repository, The Azure Pipelines will pickup from the Commit, Compile the project and Deploy it to Business Central using the PowerShell Script called above.
These auto deployments are done with the help of YAML files below.  
YAML File to automate the deployments
YAML files consists of set of Tasks, Scripts and Configurations used by Azure Pipelines to spin-up a temporary Virtual Machines and Run all the Codes configured in the YAML files.

5. Output:
Whenever I commited my code to Azure DevOps, a following screen is seen.
Code Commits with Azure Pipelines Status

A command line window appears showing the processing of the PowerShell Script called through the YAML Task.
Execution of Azure Pipelines and PowerShell Task.
Finally, after the code is deployed to Business Central you will get the following Output.
Business Central Output after the code is deployed!

Conclusion:
As you can see you can use this process for each of the code commits that happen ensuring that the code is deployed correctly. This can also be configured for each of the branch of repository and every developer can auto deploy it to their specific tenants.

Also, ALOps is one of the best tool to automate your Business Central deployments. Blog on that topic will follow soon.

Thank you for reading this! Hope this helps

Comments

  1. Hi Olister,

    Very good and informative article on CI/CD topic, do you have sample files on github for the article?

    Mohamed

    ReplyDelete
  2. Hi Mohammed,
    You can refer here https://github.com/olisterr/CI-CD-

    ReplyDelete
  3. Hi Olister,

    I am trying to replicate the same scenario but getting error while try to compile the app

    Getting error

    Version : 2.165.0
    Author : Microsoft Corporation
    Help : https://docs.microsoft.com/azure/devops/pipelines/tasks/utility/powershell
    ==============================================================================
    Generating script.
    Formatted command: . 'd:\a\1\s\CICDProject\Compile-PS.ps1'
    ========================== Starting Command Output ===========================
    "C:\windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'd:\a\_temp\ed8ad533-b3c9-431f-8cb9-875c25e6cf13.ps1'"
    .\CICDProject\ALCompiler\alc.exe/project:.\CICDProject/out:.\CICDProject\NAGCICD.app/packagecachepath:.\AlPackages :
    The term
    '.\CICDProject\ALCompiler\alc.exe/project:.\CICDProject/out:.\CICDProject\NAGCICD.app/packagecachepath:.\AlPackages'
    is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name,
    or if a path was included, verify that the path is correct and try again.
    At D:\a\1\s\CICDProject\Compile-PS.ps1:3 char:1
    + .\CICDProject\ALCompiler\alc.exe/project:.\CICDProject/out:.\CICDProj ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (.\CICDProject\A...th:.\AlPackages:String) [], ParentContainsErrorRecord
    Exception
    + FullyQualifiedErrorId : CommandNotFoundException

    ##[error]PowerShell exited with code '1'.
    Finishing: Compile the App

    Could you please suggest how to write the command in Compile.ps1.

    ReplyDelete
  4. Hi SD,

    It looks like you command is not pointing correctly to ALC and its parameters.

    I would recommend that you use everything in the same project folder.
    Also I noticed that there are no spaces to seperate ALC and its parameters.

    Generally you 'cd' into ProjectFolder then you call .\ALCompiler\alc.exe and then the project path is ./ and output path is ./OutputFile.app

    Refer http://dankinsella.blog/business-central-al-compiler/


    I hope this helps.

    ReplyDelete
  5. Hi Olister,
    Thanks for the reply but still i m not able to write the command successfully,
    My command is "Set-location C:\NAGRepo\NAGCICD\CICDProject\ALCompiler > alc.exe /project:C:\NAGRepo\NAGCICD\CICDProject /out:C:\NAGRepo\NAGCICD\CICDProject\NAGCICD.app /packagecachepath:C:\NAGRepo\NAGCICD\CICDProject\ALPackages"

    Does the above command work on the Azure?

    I am still getting error in the Azure Pipeline

    Starting: Compile the App
    ==============================================================================
    Task : PowerShell
    Description : Run a PowerShell script on Linux, macOS, or Windows
    Version : 2.165.0
    Author : Microsoft Corporation
    Help : https://docs.microsoft.com/azure/devops/pipelines/tasks/utility/powershell
    ==============================================================================
    Generating script.
    Formatted command: . 'd:\a\1\s\CICDProject\Compile-PS.ps1'
    ========================== Starting Command Output ===========================
    "C:\windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'd:\a\_temp\3b8ca8d4-6ed2-401a-8a5b-0c10b258841a.ps1'"
    Set-Location : Cannot find path 'C:\NAGRepo\NAGCICD\CICDProject\ALCompiler' because it does not exist.
    At D:\a\1\s\CICDProject\Compile-PS.ps1:5 char:1
    + Set-Location C:\NAGRepo\NAGCICD\CICDProject\ALCompiler
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (C:\NAGRepo\NAGC...ject\ALCompiler:String) [Set-Location], ItemNotFoundE
    xception
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetLocationCommand

    ##[error]PowerShell exited with code '1'.
    Finishing: Compile the App

    Could you help me to rectify the above.

    Thanks

    ReplyDelete

Post a Comment

Let me know your comments below. I'll try my best to answer your comment

Popular posts from this blog

Something went wrong. An Error occurred - Error Resolution

Introduction: With the installation of NAV 2018 or BC On-premise, I have observed that when creating New Server Instance and New WebServer Instance, you will get the error 'Something went wrong. An Error occurred '. I referred to the community questions below but didn't find my resolution. Hence, I decided to write this blog. Pre-requisites: Microsoft Dynamics Business Central - On-Premise / NAV 2018 Understanding of Business Central Authentication  Books & References: https://community.dynamics.com/nav/f/microsoft-dynamics-nav-forum/261301/nav-2018-web-client-an-error-has-occurred https://community.dynamics.com/business/f/dynamics-365-business-central-forum/421987/error-something-went-wrong-an-error-has-occurred-azure-ad-tenant Demonstration: 1. Creation of NAVServerInstance: In order to create NAVServerInstance, you can either add the Server Instance through Business Central Administration or Powershell command. Add Instance - Business Central Administration Add Insta

Setting up OAuth Authentication for Business Central Web Services / APIs

Introduction: After upgrading Business Central to version 17 (Wave2 2020), I found that there was an error telling me that WebService Key is going to be deprecated soon. Web Service Access Key is deprecated. Hence, I decided to explore and probably make it easier through this blog. Pre- requisites : Admin Access to Office 365 Admin Access to Azure Demonstration: 1. App Registration on Azure Portal: To enable OAuth authentication for any apps in Azure Active Directory, you will need to perform App Registration and set up the permissions and security details. i.) Register the App: Search for 'App Registration' on the Azure Portal. Click on New Registration Type in the App Names, Account Types and Redirect URI. Registering Application ii.) Setup API Permissions: Go to API Permissions > Add Permissions > Business Central App > Select Delegated Permissions > Select Other Permissions and Financials > Add Permissions Button. Setup API Permissions iii.) Setup Client Secr

How to Upgrade Data from Microsoft Dynamics NAV to Microsoft Dynamics Business Central On Premise - Part 2

 Introduction: Now that the first step of the problem preprocessing of the NAV Database to make it ready to be migrated to BC is done(refer Part 1 ), we can now proceed further with connecting the database in Business Central On-Premise. Pre- requisites : Microsoft Dynamics 365 Business Central Microsoft Dynamics for NAV Microsoft SQL Server Management Studio (SSMS) Powershell ISE Understand NAV to BC Upgrade Considerations -  Click here Understand Upgrade Path -  Click here Books & References: https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/upgrade/upgrade-considerations#online https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/upgrade/upgrade-paths https://www.sauravdhyani.com/2018/10/msdyn365bc-data-upgrade-to-microsoft.html https://olisterr.blogspot.com/2022/03/how-to-upgrade-data-from-microsoft.html https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/upgrade/upgrade-permissions Demonstration: 1. Install Business