Skip to content

Commit d3cfe0f

Browse files
Improve NuGet preview package naming for non-master builds (#239)
* Initial plan * Update nuget.yml: use pwsh, fix PR branch name, add PR number and run number to version suffix Co-authored-by: 304NotModified <5808377+304NotModified@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: 304NotModified <5808377+304NotModified@users.noreply.github.com>
1 parent 0650012 commit d3cfe0f

1 file changed

Lines changed: 35 additions & 14 deletions

File tree

.github/workflows/nuget.yml

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,45 @@ jobs:
3333

3434
- name: Determine version suffix
3535
id: version
36-
shell: bash
36+
shell: pwsh
3737
run: |
38-
if [[ "$GITHUB_REF" == "refs/heads/master" ]]; then
39-
echo "suffix=" >> $GITHUB_OUTPUT
40-
else
41-
CLEAN=$(echo "$GITHUB_REF_NAME" | sed 's/[^a-zA-Z0-9]/-/g' | sed 's/-\+/-/g' | sed 's/^-//;s/-$//')
42-
echo "suffix=$CLEAN" >> $GITHUB_OUTPUT
43-
fi
38+
$ref = "${{ github.ref }}"
39+
if ($ref -eq "refs/heads/master") {
40+
echo "suffix=" >> $env:GITHUB_OUTPUT
41+
} else {
42+
# For a PR: use head_ref (actual branch name), otherwise the ref name
43+
$branchName = "${{ github.head_ref }}"
44+
if ([string]::IsNullOrEmpty($branchName)) {
45+
$branchName = "${{ github.ref_name }}"
46+
}
47+
48+
# Sanitize: non-alphanumeric -> '-', multiple '-' -> one, trim '-'
49+
$clean = $branchName -replace '[^a-zA-Z0-9]', '-' `
50+
-replace '-+', '-' `
51+
-replace '^-|-$', ''
52+
# Truncate to max 40 characters and remove trailing '-'
53+
$short = if ($clean.Length -gt 0) { $clean.Substring(0, [Math]::Min(40, $clean.Length)).TrimEnd('-') } else { "" }
54+
55+
# PR number prefix
56+
$prNumber = "${{ github.event.pull_request.number }}"
57+
$prPrefix = if ($prNumber) { "pr${prNumber}-" } else { "" }
58+
59+
# Build number
60+
$build = "${{ github.run_number }}"
61+
62+
$suffix = "${prPrefix}${short}.${build}"
63+
echo "suffix=$suffix" >> $env:GITHUB_OUTPUT
64+
}
4465
4566
- name: Pack
46-
shell: bash
67+
shell: pwsh
4768
run: |
48-
VERSION_SUFFIX="${{ steps.version.outputs.suffix }}"
49-
if [ -n "$VERSION_SUFFIX" ]; then
50-
dotnet pack src/NLog.MailKit.sln --no-build --configuration Release --output artifacts --version-suffix "$VERSION_SUFFIX"
51-
else
69+
$versionSuffix = "${{ steps.version.outputs.suffix }}"
70+
if ($versionSuffix) {
71+
dotnet pack src/NLog.MailKit.sln --no-build --configuration Release --output artifacts --version-suffix "$versionSuffix"
72+
} else {
5273
dotnet pack src/NLog.MailKit.sln --no-build --configuration Release --output artifacts
53-
fi
74+
}
5475
5576
- name: Upload nuget package
5677
uses: actions/upload-artifact@v4
@@ -67,5 +88,5 @@ jobs:
6788

6889
- name: Push to NuGet.org
6990
if: github.ref == 'refs/heads/master'
70-
shell: bash
91+
shell: pwsh
7192
run: dotnet nuget push artifacts/*.nupkg --api-key ${{ steps.login.outputs.NUGET_API_KEY }} --source https://api.nuget.org/v3/index.json --skip-duplicate

0 commit comments

Comments
 (0)