How to Set Up Git Version Control for Dynamics 365 Finance & Operations project

This blog describes the process of setting up Git as version control for the Dynamics 365 Finance and Operations project.

Before starting to set up make sure you have Visual Studio 2017 or above and the latest Git installed on your system.

Initial Steps:

  1. Create a new project in DevOps and select version control as Git.

2. Then go to repos and create a new repository if a default repository is not created upon creating a new project.

3. Enter repo name and select type as Git. Select the add README option and select visual studio in the “Add a .gitignore” type selection.

Clone Repo & Setup Folder Structure:

  1. Access your D365 Finance and operations development environment and open Visual Studio as an administrator
  2. Open Team Explorer and select the plug icon then select Manage connections and then select Connect to a project

3. Select the project and select connect. It might ask for credentials upon connecting, provide the credentials which you are using for DevOps sign In.

4. Clone the repository. You can change the clone directory. It is preferable that you create a root folder in C: Drive and clone your project there.

5. Following will be the structure of the folder after cloning completes.

6. Add these three folders in the cloned repo directory.

7. Create a power shell script using the provided script with the name “Mount” in the scripts folder.

Following is the script in Mount.ps1

if (Test-Path -Path K:\AosService)

{


$LocalDeploymentFolder = "K:\AosService"


}


elseif (Test-Path -Path C:\AosService)


{


$LocalDeploymentFolder = "C:\AosService"


}


elseif (Test-Path -Path E:\AosService)


{


$LocalDeploymentFolder = "E:\AosService"


}


else


{


throw "Cannot find the AOSService folder in any known location"


}


Write-Host "Using $LocalDeploymentFolder as the deployment folder"

$LocalPackagesFolder = Join-Path $LocalDeploymentFolder "PackagesLocalDirectory"

if (Get-Process devenv -ErrorAction SilentlyContinue) {
throw "Visual studio is running! Please close VS and run the script again."
}

# Install d365fo.tools if needed
if (Get-Module -ListAvailable -Name "d365fo.tools") {
Write-Host "Importing d365fo.tools"
Import-Module "d365fo.tools"
}
else {
Write-Host "Installing d365fo.tools"
Install-PackageProvider nuget -Scope CurrentUser -Force -Confirm:$false
Install-Module d365fo.tools -AllowClobber -SkipPublisherCheck -Force -Confirm:$false
}

Write-Host "Stopping D365FO environment"
Stop-D365Environment

# Get the list of models to junction
$ModelsToJunction = Get-ChildItem "$PSScriptRoot\..\Metadata\"
Write-Host "Enabling editing of the following models:" $ModelsToJunction

foreach ($Model in $ModelsToJunction)
{
$LocalModelPath = Join-Path $LocalPackagesFolder $Model
$RepoPath = Join-Path "$PSScriptRoot\..\Metadata" $Model

if (!(Test-Path $LocalModelPath -PathType Container))
{
Write-Host "Creating model folder: " $LocalModelPath
New-Item -ItemType Directory -Force -Path $LocalModelPath
}

$RepoSubfolders = Get-ChildItem $RepoPath
foreach ($RepoSubfolder in $RepoSubfolders)
{
$LocalSubfolderPath = Join-Path $LocalModelPath $RepoSubfolder
$RepoSubfolderPath = Join-Path $RepoPath $RepoSubfolder

if (Test-Path $RepoSubfolderPath -PathType Container)
{
# Use CMD and rmdir since Powershell Remove-Item tries to recurse subfolders
Write-Host "Removing existing $($Model)\$($RepoSubfolder) source code"
cmd /c rmdir /s /q $LocalSubfolderPath

# Create new symbolic links
Write-Host "Creating new symbolic link for $($Model)\$($RepoSubfolder)"
New-Item -ItemType:SymbolicLink -Path:$LocalSubfolderPath -Value:$RepoSubfolderPath
}
}
}

Write-Host "Starting D365FO environment"
Start-D365Environment

8. Go to team explorer in Visual Studio and select Changes.

9. Commit the initial changes.

10. Push these changes to the DevOps repository.

11. After the above steps create a new custom model with the project in D365 Visual Studio.

12. Stop the AOS service in IIS Manager. Then in the PackageLocalDirectory cut the newly created model folder.

13. Paste it in the Metadata folder created in the above steps.

14. Delete any xpp folder and bin or xref files if exists in your model’s folder.

15. After these steps open PowerShell command prompt as admin. Run the Mount.ps1 file created in the above steps. The project will be linked to source control and any changes can be pushed to the repo.

For Existing Model:

If a Project/model is deployed through a deployable package or imported through a app file it can be added into source control through the same process just skipping the new model creation step.

Related Post