Skip to content

Commit

Permalink
task: added build versioning
Browse files Browse the repository at this point in the history
  • Loading branch information
dtanglr committed Nov 8, 2023
1 parent 1cc8d49 commit 3f01ad7
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 28 deletions.
63 changes: 41 additions & 22 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
@@ -1,32 +1,51 @@
# This workflow will build a .NET project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net

name: .NET
name: CI

on:
push:
branches: [ "main" ]
branches:
- main
- release/**
pull_request:
branches: [ "main" ]

branches:
- main

jobs:
build:

env:
BUILD_CONFIG: 'Release'
SOLUTION: 'Primitively.sln'

runs-on: ubuntu-latest
strategy:
matrix:
dotnet-version: [ '7.0.x' ]


steps:
- uses: actions/checkout@v3
- name: Setup .NET ${{ matrix.dotnet-version }}
uses: actions/setup-dotnet@v3
with:
dotnet-version: ${{ matrix.dotnet-version }}
- name: Display dotnet version
run: dotnet --version
- uses: actions/checkout@v2

- name: Get Build Version
run: |
Import-Module .\build\GetBuildVersion.psm1
Write-Host $Env:GITHUB_REF
$version = GetBuildVersion -VersionString $Env:GITHUB_REF
echo "BUILD_VERSION=$version" | Out-File -FilePath $Env:GITHUB_ENV -Encoding utf-8 -Append
shell: pwsh

- name: Setup NuGet
uses: NuGet/[email protected]

- name: Restore dependencies
run: dotnet restore
run: nuget restore $SOLUTION

- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: 7.0.x

- name: Build
run: dotnet build --no-restore
- name: Test
run: dotnet test --no-build --verbosity normal
run: dotnet build $SOLUTION --configuration $BUILD_CONFIG -p:Version=$BUILD_VERSION --no-restore

- name: Run tests
run: dotnet test /p:Configuration=$env:BUILD_CONFIG --no-restore --no-build --verbosity normal

- name: Publish
if: startsWith(github.ref, 'refs/heads/release')
run: nuget push **\*.nupkg -Source 'https://api.nuget.org/v3/index.json' -ApiKey ${{secrets.NUGET_API_KEY}}
34 changes: 34 additions & 0 deletions build/GetBuildVersion.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Function GetBuildVersion {
Param (
[string]$VersionString
)

# Process through regex
$VersionString -match "(?<major>\d+)(\.(?<minor>\d+))?(\.(?<patch>\d+))?(\-(?<pre>[0-9A-Za-z\-\.]+))?(\+(?<build>\d+))?" | Out-Null

if ($matches -eq $null) {
return "1.0.0-build"
}

# Extract the build metadata
$BuildRevision = [uint64]$matches['build']
# Extract the pre-release tag
$PreReleaseTag = [string]$matches['pre']
# Extract the patch
$Patch = [uint64]$matches['patch']
# Extract the minor
$Minor = [uint64]$matches['minor']
# Extract the major
$Major = [uint64]$matches['major']

$Version = [string]$Major + '.' + [string]$Minor + '.' + [string]$Patch;
if ($PreReleaseTag -ne [string]::Empty) {
$Version = $Version + '-' + $PreReleaseTag
}

if ($BuildRevision -ne 0) {
$Version = $Version + '.' + [string]$BuildRevision
}

return $Version
}
8 changes: 2 additions & 6 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
<CheckEolTargetFramework>false</CheckEolTargetFramework>
<Deterministic>true</Deterministic>
<ContinuousIntegrationBuild Condition="'$(TF_BUILD)' == 'true'">true</ContinuousIntegrationBuild>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<PackageOutputPath>$(MSBuildThisFileDirectory)..\artifacts</PackageOutputPath>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<!--PackageOutputPath>$(MSBuildThisFileDirectory)..\artifacts</PackageOutputPath-->
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)' == 'Debug'">
Expand All @@ -44,10 +44,6 @@

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All " />
<!--PackageReference Include="Nerdbank.GitVersioning" Condition="!Exists('packages.config')">
<PrivateAssets>all</PrivateAssets>
<Version>3.6.133</Version>
</PackageReference-->
</ItemGroup>

</Project>

0 comments on commit 3f01ad7

Please sign in to comment.