Few days ago I spoke with one of my colleagues about building and deploying the apps developed in AL. Seems that his team was doing it manually. Ok let’s say semi automatically.
What does it mean? Let’s say we want to have our app deployed on our Dynamics NAV instance. They first were building the package directly in Visual Studio Code then move the file to special folder where the script was doing Publish-NAVApp and Install-NAVApp.
Being lazy is not bad thing
I thought hmm it would be good to do it all automatically I am to lazy to build it in VSC and put to folder. At the end this part was not so complicated. It is easily possible to run AL compiler from PowerShell you just need to know where your compiler is and run it with some parameters. The example you can find below:
F:\ALLang\bin\alc.exe /project:$ProjectPath /out:$OutputPath /packagecachepath:$PackageCachePath
$ProjectPath – is the path where our project is stored
$PackageCachePath – is the path to our .alpackages\ folder (so $ProjectPath + .alpackages\)
$OutputPath – is the path where our app should be exported (remember to add the extension .app)
Normally our compiler is in user folder, for example:
but you can copy it to any folder which you want (just copy whole extension). Remember only to update it every time when you will get new version.
Easy? Right? So let’s do something more
After figure it out I though let’s do script more complicated. Why not do in one script – unpublish the app, build and publish it again. So one click and I have all ready to use. But then they said – you know but we change version of our app when we run the build so anyway you need to go and build it by your own. No you don’t! Let’s do it with the script.
I am very happy that now AL is using a lot of things which are nowadays standards. For example app file is json file. Means with few lines of code I can change the parameter and save it again. for that I just need get the content and use ConvertFrom-Json, change the value and at the end ConvertTo-Json and save the file.
Example how to change the version in app.json file you can find at the end of this post
…But we use Git
Ok but what if your solution is somewhere on git repository? Then we can, before building the app and publish in Dynamics NAV, pull the repository to specific folder and later build the app from the most updated source.
A script for you
So at the end, after all questions and all ideas I came up with the one script which is doing below things in the order which you see:
- Pull latest version from Git repository
- Uninstall and Unpublish App in Dynamics NAV
- Change version of Application in App.json
- Build the app file ready for publishing
- Publish and Install Application in Dynamics NAV
- Push app.json file with newest version to Git repository
I added options that it can be more flexible.
You can find the script here: https://github.com/mynavblog/PowerShell-for-NAV/blob/master/al_build_and_deploy.ps1
And Wiki for a script here: https://github.com/mynavblog/PowerShell-for-NAV/wiki/Script:-al_build_and_deploy.ps1 (I tried to write as much as possible how to setup the script)
Hope you will find it useful.
Example how to change version from PowerShell
$appfile = Get-Content $SetupFileName -raw | ConvertFrom-Json
$ver1,$ver2,$ver3,$ver4 = $appfile.version.Split(‘.’)
$newversionint = [int]$ver4 + 1;
$ver4 = [string]$newversionint
$newversion = $ver1 + ‘.’ + $ver2 + ‘.’ + $ver3 + ‘.’ + $ver4
$appfile.version = $newversion
$appfile | ConvertTo-Json | set-content $SetupFileName
Where $SetupFileName is path to app.json file