Skip to content

Simplify package versioning #36

Simplify package versioning

Simplify package versioning #36

Workflow file for this run

name: Package
on:
workflow_dispatch:
push:
branches:
- main
- '[0-9]+.x'
- 'release/*'
pull_request:
release:
types: [ published ]
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
permissions:
contents: read
env:
DOTNET_CLI_TELEMETRY_OPTOUT: 1
DOTNET_NOLOGO: true
SOLUTION_FILE: 'src/Steeltoe.All.sln'
jobs:
build:
name: Build
timeout-minutes: 15
runs-on: ubuntu-latest
steps:
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: |
8.0.*
9.0.*
- name: Git checkout
uses: actions/checkout@v4
- name: Restore packages
run: dotnet restore ${{ env.SOLUTION_FILE }} --verbosity minimal
- name: Calculate package version (for release)
if: ${{ github.event_name == 'release' }}
env:
TAG_NAME: ${{ github.ref_name }}
shell: pwsh
run: |
# Get the version suffix from the git tag. For example: '1.2.3-preview1-final' => '1.2.3' and 'preview1-final'
$tagSegments = '${{ env.TAG_NAME }}' -split '-'
$versionPrefix = $tagSegments[0]
$versionSuffix = $tagSegments.Length -eq 1 ? '' : $tagSegments[1..$($tagSegments.Length - 1)] -join '-'
[xml]$xml = Get-Content shared-package.props
$configuredVersionPrefix = $xml.Project.PropertyGroup.VersionPrefix | Select-Object -First 1
if ($configuredVersionPrefix -ne $versionPrefix) {
Write-Error "Version prefix from git release tag '$versionPrefix' does not match version prefix '$configuredVersionPrefix' stored in shared-package.props."
# To recover from this:
# - Delete the GitHub release
# - Run: git push --delete origin the-invalid-tag-name
# - Adjust VersionPrefix in shared-package.props, commit and push
# - Recreate the GitHub release
}
Write-Output "Using version suffix: $versionSuffix"
Write-Output "PACKAGE_VERSION_SUFFIX=$versionSuffix" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
- name: Calculate package version (for branch)
if: ${{ github.event_name == 'push' }}
env:
BRANCH_NAME: ${{ github.ref_name }}
shell: pwsh
run: |
# Get the version suffix from the branch name and auto-incrementing build number. For example: 'main' and '123' => 'main-00123'
$revision = "{0:D5}" -f [convert]::ToInt32(${{ github.run_number }}, 10)
$branchName = '${{ env.BRANCH_NAME }}'
$safeName = $branchName.Replace('/', '-').Replace('_', '-').Replace('$', '-').Replace(';', '-')
$versionSuffix = "$safeName-$revision"
Write-Output "Using version suffix: $versionSuffix"
Write-Output "PACKAGE_VERSION_SUFFIX=$versionSuffix" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
- name: Calculate package version (for pr)
if: ${{ github.event_name == 'pull_request' }}
shell: pwsh
run: |
# Get the version suffix from the PR number and auto-incrementing build number. For example: '18' and '123' => 'pr18-00123'
$revision = "{0:D5}" -f ${{ github.run_number }}
$versionSuffix = "pr${{ github.event.number }}-$revision"
Write-Output "Using version suffix: $versionSuffix"
Write-Output "PACKAGE_VERSION_SUFFIX=$versionSuffix" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
- name: Build solution
run: dotnet build ${{ env.SOLUTION_FILE }} --no-restore --configuration Release --verbosity minimal /p:VersionSuffix=${{ env.PACKAGE_VERSION_SUFFIX }}
- name: Collect packages
run: dotnet pack ${{ env.SOLUTION_FILE }} --no-build --configuration Release --output ${{ github.workspace }}/packages /p:VersionSuffix=${{ env.PACKAGE_VERSION_SUFFIX }}
- name: Upload packages
uses: actions/upload-artifact@v4
with:
if-no-files-found: error
name: unsigned-packages
path: ${{ github.workspace }}/packages/**/*.nupkg