In my previous post I covered the use of the d365fo.tools to setup a NuGet source and to upload NuGet packages to an Azure DevOps artifact feed.
In this post I want to show one way, how we can use the d365fo.tools to update the NuGet packages with the newest version available in the LCS asset library.

To achieve this, we will use the following commands from the d365fo.tools:

So we’re using a handful of commands, and still it’ll reflect in just some lines of code in powershell and save us quite some time compared to doing the work manually.

With the d365fo.tools we have a powerful toolset, to automate many workloads in the D365 world.
I heard some rumors, that @braul (msdyn365fo.wordpress.com) is working on a blog post about a YAML template for the Azure DevOps pipeline to perform the update 😉 I’ll add the link as soon as it is available.

To update the packages we need the NuGet source setup, as well as an Azure DevOps PAT, an Azure AD application and an user account without MFA. If you follow the instructions in Part 1 everything should be in place to update the packages.

So we have our artifact feed, in my example it holds the packages from 10.0.15.

Import the latest packages to your project asset library
Lets update the build pipeline to 10.0.17. We’ll start by importing the NuGet packages into our LCS project asset library.

This would be the only manual step, as everything besides this step will be handled by our powershell script.

Setup d365fo.tools
To start, we will configure the LCS project we’re working against. We need to get a authentication token and set it for the LCS API config of the project.

Get-D365LcsApiToken -Username "LCS User" -Password "password" -ClientId "AAD Application Client Id" -LcsApiUri "https://lcsapi.lcs.dynamics.com" | Set-D365LcsApiConfig -ProjectId "LCS Project Id"

Nothing special here, we need to specify the user that is present in the LCS project and doesn’t have MFA activated, and the client id from our AAD application.
For the LCS API Uri it depends where your LCS project is located: LCS API Uri
With this set up, every LCS API command from the d365fo.tools is performed against the LCS project we just declared.

Now lets install AzCopy that we can download the packages to our computer.

Invoke-D365InstallAzCopy 

Check NuGet version and push update
Basically we’re iterating over the NuGet packages in the artifact library. For every item we search for all versions available in the asset library. Finally, we download the package with the highest version and push it into the artifact feed.
The comments should clarify the intent of the script which you can also download here

#Get the NuGet packages currently present in the Azure DevOps Artifact feed
$currentFeedNuGetPackages = Get-D365AzureDevOpsNuget -Uri "https://dev.azure.com/DevOps/D365/" -FeedName "D365Update" -PeronalAccessToken "Your PAT" -Latest
#iterate through the packages in the artifact feed
foreach ($NuGetPackage in $currentFeedNuGetPackages) 
{
    #find the NuGet package in LCS with the name from the package in the artifact feed
    $lcsNuGetPackages = Get-D365LcsAssetFile -FileType NuGetPackage -AssetFilename "$($NuGetPackage.Name)*" 
    $PackageVersion = $NuGetPackage.Version
    foreach ($NuGetPackageLCS in $lcsNuGetPackages) 
    {  
        $fileName = $NuGetPackageLCS.FileName 
        #get the filename from the LCS NuGet package        
        if ($fileName -Match "\d+.\d+.\d+.\d+") 
        { 
            #check if the version from the selected LCS package is greater than the artifact feed package
            if ($([Version]$Matches[0]) -gt [Version]$PackageVersion) 
            {                 
                $NuGetPackageToDownload = $NuGetPackageLCS
                $PackageVersion = $([Version]$Matches[0])
            } 
        }  
    } 
    $fileName = $NuGetPackageToDownload.FileName 
    #download the newest LCS package
    $NuGetPackageToDownload |  Invoke-D365AzCopyTransfer -DestinationUri "C:\Temp\d365fo.tools\nuget\$FileName" 
    #push the LCS package into the artifact feed
    Invoke-D365AzureDevOpsNugetPush -Path $fileName -source "Your NuGet Source" -ShowOriginalProgress  
}

Please note, that the above script is intended to be used locally. But with some modification it should also be possible to run it in a pipeline if you want you.

After we call the script in an elevated powershell prompt we should see the download and upload of the packages.

And then the updated packages in our artifact feed.

So instead of manually identifying, downloading and uploading the newest NuGet package, we can simply run the script and the packages are being updated automatically.

Advertisement