@@ -23,6 +23,7 @@ permissions:
2323env :
2424 DOTNET_CLI_TELEMETRY_OPTOUT : 1
2525 DOTNET_NOLOGO : true
26+ VERSION_FILE : ' src/Steeltoe.NetCoreTool.Templates.csproj'
2627
2728jobs :
2829 build-and-test :
@@ -45,24 +46,76 @@ jobs:
4546
4647 - name : Git checkout
4748 uses : actions/checkout@v4
48- with :
49- fetch-depth : 0
50-
51- - name : Restore tools
52- run : dotnet tool restore
5349
5450 - name : Restore packages
5551 run : dotnet restore --verbosity minimal
5652
53+ - name : Calculate package version (for release)
54+ if : ${{ github.event_name == 'release' }}
55+ env :
56+ TAG_NAME : ${{ github.ref_name }}
57+ shell : pwsh
58+ run : |
59+ # Get the version suffix from the git tag. For example: '1.2.3-preview1-final' => 'preview1-final'
60+ $tagSegments = '${{ env.TAG_NAME }}' -split '-'
61+ $versionPrefix = $tagSegments[0]
62+ $versionSuffix = $tagSegments.Length -eq 1 ? '' : $tagSegments[1..$($tagSegments.Length - 1)] -join '-'
63+
64+ [xml]$xml = Get-Content $env:VERSION_FILE
65+ $configuredVersionPrefix = $xml.Project.PropertyGroup.VersionPrefix | Select-Object -First 1
66+
67+ if ($configuredVersionPrefix -ne $versionPrefix) {
68+ Write-Error "Version prefix from git release tag '$versionPrefix' does not match version prefix '$configuredVersionPrefix' stored in $env:VERSION_FILE."
69+ # To recover from this:
70+ # - Delete the GitHub release
71+ # - Run: git push --delete origin the-invalid-tag-name
72+ # - Adjust VersionPrefix in file, commit and push
73+ # - Recreate the GitHub release
74+ }
75+
76+ Write-Output "Using version suffix: $versionSuffix"
77+ Write-Output "PACKAGE_VERSION_SUFFIX=$versionSuffix" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
78+
79+ - name : Calculate package version (for branch)
80+ if : ${{ github.event_name == 'push' || github.event_name == 'workflow_dispatch' }}
81+ env :
82+ BRANCH_NAME : ${{ github.ref_name }}
83+ shell : pwsh
84+ run : |
85+ # Get the version suffix from the branch name and auto-incrementing build number. For example: 'main' and '123' => 'main-00123'
86+ $revision = "{0:D5}" -f ${{ github.run_number }}
87+ $branchName = '${{ env.BRANCH_NAME }}'
88+ $safeBranchName = $branchName -Replace '[^a-zA-Z0-9-]', '-'
89+ $versionSuffix = "$safeBranchName-$revision"
90+
91+ Write-Output "Using version suffix: $versionSuffix"
92+ Write-Output "PACKAGE_VERSION_SUFFIX=$versionSuffix" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
93+
94+ - name : Calculate package version (for pr)
95+ if : ${{ github.event_name == 'pull_request' }}
96+ shell : pwsh
97+ run : |
98+ # Get the version suffix from the PR number and auto-incrementing build number. For example: '18' and '123' => 'pr18-00123'
99+ $revision = "{0:D5}" -f ${{ github.run_number }}
100+ $versionSuffix = "pr${{ github.event.number }}-$revision"
101+
102+ Write-Output "Using version suffix: $versionSuffix"
103+ Write-Output "PACKAGE_VERSION_SUFFIX=$versionSuffix" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
104+
105+ - name : Verify package version
106+ if : ${{ !env.PACKAGE_VERSION_SUFFIX && github.event_name != 'release' }}
107+ run : |
108+ echo "Package version suffix is empty. This should never happen."
109+ exit 1
110+
57111 - name : Build solution
58- run : dotnet build --no-restore --configuration Release --verbosity minimal
112+ run : dotnet build --no-restore --configuration Release --verbosity minimal /p:VersionSuffix=${{ env.PACKAGE_VERSION_SUFFIX }}
59113
60114 - name : Test
61115 run : dotnet test --no-build --configuration Release --collect:"XPlat Code Coverage" --logger "GitHubActions;summary.includeSkippedTests=true"
62116
63117 - name : Collect packages
64- shell : pwsh
65- run : dotnet pack src --no-build --configuration Release --output ${{ github.workspace }}/packages
118+ run : dotnet pack src --no-build --configuration Release --output ${{ github.workspace }}/packages /p:VersionSuffix=${{ env.PACKAGE_VERSION_SUFFIX }}
66119
67120 - name : Upload unsigned packages
68121 if : ${{ matrix.os == 'ubuntu-latest' }}
@@ -192,4 +245,57 @@ jobs:
192245 path : packages
193246
194247 - name : Push packages to nuget.org
195- run : dotnet nuget push '${{ github.workspace }}/packages/*.nupkg' --skip-duplicate --api-key ${{ secrets.STEELTOE_NUGET_API_KEY }} --source 'nuget.org'
248+ run : dotnet nuget push '${{ github.workspace }}/packages/*.nupkg' --skip-duplicate --api-key '${{ secrets.STEELTOE_NUGET_API_KEY }}' --source 'nuget.org'
249+
250+ open_pr :
251+ name : Open pull request to bump templates version after stable release
252+ if : ${{ github.event_name == 'release' && !contains(github.ref_name, '-') }}
253+ needs : nuget-org-deploy
254+ timeout-minutes : 15
255+ runs-on : ubuntu-latest
256+ permissions :
257+ contents : write
258+ pull-requests : write
259+
260+ steps :
261+ - name : Git checkout
262+ uses : actions/checkout@v4
263+
264+ - name : Calculate next package version
265+ shell : pwsh
266+ run : |
267+ [xml]$xml = Get-Content $env:VERSION_FILE
268+ $oldVersionPrefix = $xml.Project.PropertyGroup.VersionPrefix | Select-Object -First 1
269+
270+ $versionSegments = $oldVersionPrefix.split('.')
271+ ([int]$versionSegments[-1])++
272+ $newVersionPrefix = $versionSegments -join('.')
273+
274+ Write-Output "OLD_PACKAGE_VERSION_PREFIX=$oldVersionPrefix" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
275+ Write-Output "NEW_PACKAGE_VERSION_PREFIX=$newVersionPrefix" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
276+
277+ - name : Open pull request
278+ env :
279+ GH_TOKEN : ${{ github.token }}
280+ shell : pwsh
281+ run : |
282+ $oldVersionPrefix = '${{ env.OLD_PACKAGE_VERSION_PREFIX }}'
283+ $newVersionPrefix = '${{ env.NEW_PACKAGE_VERSION_PREFIX }}'
284+ $prBranchName = "bump-version-to-$newVersionPrefix-${{ github.run_number }}"
285+ $commitMessage = "Bump templates version from $oldVersionPrefix to $newVersionPrefix.`n`n> [!TIP]`n> Close and reopen this pull request to run status checks."
286+
287+ $pattern = '(?<left>^\s*\<VersionPrefix\>)[^>]+(?<right>\<\/VersionPrefix\>)\s*$'
288+ $fileContent = Get-Content $env:VERSION_FILE
289+ $fileContent = $fileContent -Replace $pattern,"`${left}$newVersionPrefix`${right}"
290+ Set-Content $fileContent -Path $env:VERSION_FILE
291+
292+ Write-Output "Creating pull request with commit message:`n$commitMessage"
293+ git config --local user.name "github-actions[bot]"
294+ git config --local user.email "github-actions[bot]@users.noreply.github.com"
295+ git checkout -b $prBranchName
296+ git add -A
297+ git commit -m $commitMessage
298+ git push --set-upstream origin $prBranchName
299+
300+ Write-Output "Opening pull request to merge $prBranchName."
301+ gh pr create --head $prBranchName --title 'Bump templates version' --body $commitMessage
0 commit comments