Skip to content

NuGet

NuGet #17

Workflow file for this run

name: NuGet
on:
workflow_run:
workflows: ["Build and test"]
types:
- completed
branches:
- master
workflow_dispatch:
jobs:
pack:
if: ${{ github.event.workflow_run.conclusion == 'success' || github.event_name == 'workflow_dispatch' }}
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 {
# For a PR: use head_ref (actual branch name), otherwise the ref name
$branchName = "${{ github.head_ref }}"
if ([string]::IsNullOrEmpty($branchName)) {
$branchName = "${{ github.ref_name }}"
}
# Sanitize: non-alphanumeric -> '-', multiple '-' -> one, trim '-'
$clean = $branchName -replace '[^a-zA-Z0-9]', '-' `
-replace '-+', '-' `
-replace '^-|-$', ''
# Truncate to max 40 characters and remove trailing '-'
$short = if ($clean.Length -gt 0) { $clean.Substring(0, [Math]::Min(40, $clean.Length)).TrimEnd('-') } else { "" }
# PR number prefix
$prNumber = "${{ github.event.pull_request.number }}"
$prPrefix = if ($prNumber) { "pr${prNumber}-" } else { "" }
# Build number
$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