@RodrigoQuintana The first thing you need to do is to establish your solution versioning convention. This will be unique to your circumstances (as an example you can base in on calendar or your sprint cycles if you like), but it will also need to consider how it aligns with your branching strategy. But as an example, here's the convention that I've implemented on a few projects and it has worked well so far:
Major portion of solution version number is reserved to hold the year in which the change is made and committed. And it is only incremented for changes made on the main branch. It does not change if the change is committed on the hotfix branch
Minor portion of solution version number is reserved to hold the numeric representation of the calendar month in which the change is made and committed. And it is only incremented for changes made on the main branch. It does not change if the change is committed on the hotfix branch
Build portion of solution version number holds the auto-incremented value for every change made and committed on the main branch. It does not change if the change is committed on the hotfix branch
Version portion solution version number holds the auto-incremented value for every change made and committed on the hotfix branch. For changes on the main branch is is always set to 0
Here's what my PowerShell script looks like:
param(
$serverURL,
$clientId,
$secret,
$solutionUniqueName,
[bool]$incrementBuild,
[bool]$incrementRevision
)
$ErrorActionPreference = 'Stop'
Set-PSRepository PSGallery -InstallationPolicy Trusted
Install-Module Microsoft.Xrm.Data.PowerShell -Scope CurrentUser -Force
Import-Module Microsoft.Xrm.Data.Powershell -Scope Global -Force
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Write-Host "Connecting to CRM"
Connect-CrmOnline -ServerUrl $serverURL -OAuthClientId $clientId -ClientSecret $secret
Write-Host "Successfully connected to CRM"
Write-Host "Retrieving current version of" $solutionUniqueName "solution"
$results = Get-CrmRecords -EntityLogicalName "solution" -FilterAttribute "uniquename" -FilterOperator "eq" -FilterValue $solutionUniqueName -Fields solutionid,uniquename,friendlyname, version
$solutionRecord = $results.CrmRecords[0]
$currentVersion = $solutionRecord.version
Write-Host "Current Version: " $currentVersion
$versionComponents = $currentVersion.Split(".")
Write-Host "Major: " $versionComponents[0]
Write-Host "Minor: " $versionComponents[1]
Write-Host "Build: " $versionComponents[2]
Write-Host "Revision: " $versionComponents[3]
$newMajor = $versionComponents[0]
$newMinor = $versionComponents[1]
$newBuild = [int]$versionComponents[2]
$newRevision = [int]$versionComponents[3]
if ($incrementBuild -eq $true){
$currentYear = Get-Date -Format "yyyy"
$currentMonth = Get-Date -Format "MM"
if (($currentYear -ne $newMajor) -or ($currentMonth -ne $newMinor))
{
$newBuild = 0
}
else{
$newBuild++
}
$newMajor = $currentYear
$newMinor = $currentMonth
$newRevision = 0
}
else {
if ($incrementRevision -eq $true){
$newRevision++
}
}
$fullVersion = $newMajor + "." + $newMinor + "." + [string]$newBuild + "." + [string]$newRevision
Write-Host "New Major: " $newMajor
Write-Host "New Minor: " $newMinor
Write-Host "New Build: " ([string]$newBuild)
Write-Host "New Revision: " ([string]$newRevision)
Write-Host "New Version: " $fullVersion
$solutionRecord.version = $fullVersion
Write-Host "Setting version of" $solutionUniqueName "solution to" $fullVersion
Set-CrmRecord -CrmRecord $solutionRecord
Write-Host "Success"
Write-Output "Solution_Version=$fullVersion" >> $GITHUB_ENV
And here's how I call it from my GitHub Action workflow before performing the solution export, unpack and commit to source control:
- name: Sync Solution Version for Main Branch
id: sync-solution-for-main-branch
if: github.ref == 'refs/heads/main'
shell: powershell
run: |
.\Build\Scripts\SyncSolutionVersion.ps1 -serverURL ${{vars.URL}} -clientId ${{env.CLIENT_ID}} -secret ${{env.SECRET}} -solutionUniqueName ${{ github.event.inputs.solution_name }} -incrementBuild $true -incrementRevision $false
- name: Sync Solution Version for Hotfix Branch
id: sync-solution-for-hotfix-branch
if: github.ref == 'refs/heads/hotfix'
shell: powershell
run: |
.\Build\Scripts\SyncSolutionVersion.ps1 -serverURL ${{vars.URL}} -clientId ${{env.CLIENT_ID}} -secret ${{env.SECRET}} -solutionUniqueName ${{ github.event.inputs.solution_name }} -incrementBuild $false -incrementRevision $true