Skip to content

Improve NuGet preview package naming for non-master builds#239

Merged
304NotModified merged 2 commits intomasterfrom
copilot/update-nuget-package-naming
Mar 9, 2026
Merged

Improve NuGet preview package naming for non-master builds#239
304NotModified merged 2 commits intomasterfrom
copilot/update-nuget-package-naming

Conversation

Copy link
Contributor

Copilot AI commented Mar 9, 2026

PR builds produced -merge in the version suffix due to GITHUB_REF_NAME resolving to GitHub's internal 42/merge ref. There was also no PR number, no build counter, and scripts used bash on a windows-latest runner.

Changes

  • Shell: All shell: bash replaced with shell: pwsh; scripts rewritten in PowerShell Core
  • Branch name: Use github.head_ref for PRs (real branch name), fall back to github.ref_name for push/dispatch
  • PR prefix: Prepend pr{N}- when github.event.pull_request.number is set
  • Build counter: Append .{run_number} to distinguish repeated builds
  • Sanitization: Non-alphanumeric → -, collapse -+-, trim edges, truncate to 40 chars; guards against empty result

Version suffix examples

Trigger Branch Run Suffix
PR #42 feature/oauth2-support 7 pr42-feature-oauth2-support.7
PR #7 fix/null-ref-exception 3 pr7-fix-null-ref-exception.3
Push my-branch 12 my-branch.12
Push master any (none — stable release)
Original prompt

Goal

Update .github/workflows/nuget.yml to generate better NuGet preview package names for non-master builds.

Problems with the current workflow

  1. On pull requests, GITHUB_REF_NAME is set to 42/merge (GitHub's internal merge ref), resulting in -merge appearing in the package name.
  2. No PR number is included in the preview name.
  3. No build number is included, making repeated builds indistinguishable.
  4. Scripts use bash instead of PowerShell Core (pwsh).

Required changes

Update .github/workflows/nuget.yml with the following:

  1. Replace all shell: bash with shell: pwsh and rewrite the scripts in PowerShell Core.
  2. Fix the -merge issue by using github.head_ref (the real branch name) for PRs, with github.ref_name as fallback.
  3. Prefix the version suffix with pr{N}- using github.event.pull_request.number when available.
  4. Append .{run_number} (the GitHub Actions build number) at the end of the suffix.
  5. Sanitize the branch name: replace non-alphanumeric characters with -, collapse multiple - into one, trim leading/trailing -, and truncate to a maximum of 40 characters.

Expected version suffix format

Full desired file content

name: NuGet

on:
  push:
    branches:
      - master
  pull_request:
  workflow_dispatch:

jobs:
  pack:
    runs-on: windows-latest
    permissions:
      contents: read
      id-token: write

    steps:
      - uses: actions/checkout@v4

      - name: Setup .NET
        uses: actions/setup-dotnet@v4
        with:
          dotnet-version: '8.0.x'

      - name: Restore
        run: dotnet restore src/NLog.MailKit.sln

      - name: Build
        run: dotnet build src/NLog.MailKit.sln --configuration Release --no-restore

      - name: Determine version suffix
        id: version
        shell: pwsh
        run: |
          $ref = "${{ github.ref }}"
          if ($ref -eq "refs/heads/master") {
            echo "suffix=" >> $env:GITHUB_OUTPUT
          } else {
            # Bij een PR: gebruik head_ref (echte branchnaam), anders de ref naam
            $branchName = "${{ github.head_ref }}"
            if ([string]::IsNullOrEmpty($branchName)) {
              $branchName = "${{ github.ref_name }}"
            }

            # Sanitize: niet-alfanumeriek → '-', meerdere '-' → één, trim '-'
            $clean = $branchName -replace '[^a-zA-Z0-9]', '-' `
                                 -replace '-+', '-' `
                                 -replace '^-|-$', ''
            # Inkorten tot max 40 tekens en afsluitende '-' weggooien
            $short = $clean.Substring(0, [Math]::Min(40, $clean.Length)).TrimEnd('-')

            # PR-nummer prefix
            $prNumber = "${{ github.event.pull_request.number }}"
            $prPrefix = if ($prNumber) { "pr${prNumber}-" } else { "" }

            # Build-nummer
            $build = "${{ github.run_number }}"

            $suffix = "${prPrefix}${short}.${build}"
            echo "suffix=$suffix" >> $env:GITHUB_OUTPUT
          }

      - name: Pack
        shell: pwsh
        run: |
          $versionSuffix = "${{ steps.version.outputs.suffix }}"
          if ($versionSuffix) {
            dotnet pack src/NLog.MailKit.sln --no-build --configuration Release --output artifacts --version-suffix "$versionSuffix"
          } else {
            dotnet pack src/NLog.MailKit.sln --no-build --configuration Release --output artifacts
          }

      - name: Upload nuget package
        uses: actions/upload-artifact@v4
        with:
          name: nupkg
          path: artifacts/*pkg

      - name: NuGet login
        if: github.ref == 'refs/heads/master'
        uses: NuGet/login@v1
        id: login
        with:
          user: ${{ secrets.NUGET_USER }}

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

This pull request was created from Copilot chat.


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

… number to version suffix

Co-authored-by: 304NotModified <5808377+304NotModified@users.noreply.github.com>
@pull-request-size pull-request-size bot added size/M and removed size/XS labels Mar 9, 2026
Copilot AI changed the title [WIP] Update NuGet preview package names for non-master builds Improve NuGet preview package naming for non-master builds Mar 9, 2026
@304NotModified 304NotModified marked this pull request as ready for review March 9, 2026 03:58
@sonarqubecloud
Copy link

sonarqubecloud bot commented Mar 9, 2026

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarQube Cloud

@304NotModified 304NotModified merged commit d3cfe0f into master Mar 9, 2026
7 checks passed
@304NotModified 304NotModified deleted the copilot/update-nuget-package-naming branch March 9, 2026 23:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants