Skip to content

Instantly share code, notes, and snippets.

@nickalbrecht
Last active December 14, 2022 20:35
Show Gist options
  • Save nickalbrecht/51aa4a9ae2535ccc1b74110d1cf98d14 to your computer and use it in GitHub Desktop.
Save nickalbrecht/51aa4a9ae2535ccc1b74110d1cf98d14 to your computer and use it in GitHub Desktop.
Versioning build in YAML using a combination of SemVer, and days since Jan 1, 2000
# I use the days since Jan 1, 2000 as part of my build number to mimic a behavior found with .NET Full Framework
# that would autoincrement the 4th piece of the version information based on this math. I started using it as a
# way to reverse enginer the build date at runtime. After switching to .NET Core,I lost this, and start using this
# logic to get that behavior back
# Example build numbers resulting from this...
# 7.0.featurebranch.1+7550
# 7.0.1+7550
# Inspiration for the versioning differently between branches
# https://medium.com/@ychetankumarsarma/build-versioning-in-azure-devops-pipelines-94b5a79f80a0
# https://gist.github.com/chetanku/c51526d7322989ba5a62f186df261cd3
pool:
vmImage: 'windows-2019'
variables:
buildConfiguration: 'Release'
# Manually defined Major and Minor build version
major: 7
minor: 0
stages:
- stage: Versioning
displayName: Versioning
jobs:
# Build number to use when on the master branch
- job: Build_Master_Version_Number
displayName: Build version for Master branch
condition: eq(variables['Build.SourceBranch'], 'refs/heads/master')
variables:
patch: $[counter(variables['minor'], 0)]
steps:
- checkout: none
- powershell: |
Write-Host "Generating Build Number"
$baseDate = [datetime]"01/01/2000"
$currentDate = $(Get-Date)
$interval = NEW-TIMESPAN –Start $baseDate –End $currentDate
$days = $interval.Days
Write-Host "##vso[build.updatebuildnumber]$(major).$(minor).$(patch)+$days"
# Slightly different build number when this is on any other branch than master
- job: Build_Branch_Version_Number
displayName: Build version for other branch
condition: ne(variables['Build.SourceBranch'], 'refs/heads/master')
variables:
brpatch: $[counter(variables['build.sourcebranchname'], 0)]
steps:
- checkout: none
- powershell: |
Write-Host "Generating Build Number"
$baseDate = [datetime]"01/01/2000"
$currentDate = $(Get-Date)
$interval = NEW-TIMESPAN –Start $baseDate –End $currentDate
$days = $interval.Days
Write-Host "##vso[build.updatebuildnumber]$(major).$(minor)-$(Build.SourceBranchName).$(brpatch)+$days"
# The normal tests, build, and packaging
- stage:
dependsOn: Versioning
displayName: Build
jobs:
- job:
steps:
- task: DotNetCoreCLI@2
displayName: Tests
inputs:
command: 'test'
projects: 'test/*[Tt]ests/*.csproj'
arguments: '--configuration $(BuildConfiguration)'
- task: DotNetCoreCLI@2
displayName: Build & Package
inputs:
command: 'publish'
publishWebProjects: true
arguments: '--configuration $(BuildConfiguration) --output $(build.artifactstagingdirectory) -p:Version=$(Build.BuildNumber)'
- task: PublishBuildArtifacts@1
displayName: Publish Build Artifacts
inputs:
PathtoPublish: '$(Build.ArtifactStagingDirectory)'
ArtifactName: 'drop'
publishLocation: 'Container'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment