Skip to content

Merge pull request #1 from malforge/container-disposal #16

Merge pull request #1 from malforge/container-disposal

Merge pull request #1 from malforge/container-disposal #16

name: Build and Publish NuGet Package
on:
pull_request:
workflow_dispatch:
inputs:
deployRelease:
description: 'Deploy release (publish NuGet package) (y/n)'
required: true
default: 'n'
push:
branches:
- main
- prerelease
paths:
- 'Source/**/PackageVersion.txt'
env:
NuGetDirectory: ${{ github.workspace }}/nuget
jobs:
validate-versions:
if: github.event_name == 'push' || (github.event_name == 'workflow_dispatch' && github.event.inputs.deployRelease == 'y')
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Validate version suffixes match branch
shell: pwsh
run: |
$branch = "${{ github.ref }}"
$isMain = $branch -eq "refs/heads/main"
$isPrerelease = $branch -eq "refs/heads/prerelease"
if (-not $isMain -and -not $isPrerelease) {
Write-Host "Skipping validation - not on main or prerelease branch"
exit 0
}
# Get list of changed PackageVersion.txt files in this push
$changedFiles = git diff --name-only ${{ github.event.before }} ${{ github.sha }} | Where-Object { $_ -like "*/PackageVersion.txt" }
if ($changedFiles.Count -eq 0) {
Write-Host "No PackageVersion.txt files changed in this push"
exit 0
}
$errors = @()
foreach ($file in $changedFiles) {
if (-not (Test-Path $file)) {
Write-Host "Skipping deleted file: $file"
continue
}
$version = (Get-Content $file).Trim()
$hasPrereleaseSuffix = $version -match '-'
if ($isMain -and $hasPrereleaseSuffix) {
$errors += "ERROR: $file contains prerelease version '$version' but branch is 'main'"
}
elseif ($isPrerelease -and -not $hasPrereleaseSuffix) {
$errors += "ERROR: $file contains stable version '$version' but branch is 'prerelease'"
}
else {
Write-Host "✓ $file : $version (valid for $branch)"
}
}
if ($errors.Count -gt 0) {
Write-Host "`nValidation failed:"
$errors | ForEach-Object { Write-Host $_ }
Write-Host "`nBranch/version rules:"
Write-Host " - 'main' branch must have stable versions (e.g., 1.5.0)"
Write-Host " - 'prerelease' branch must have prerelease versions (e.g., 1.5.0-beta.1)"
exit 1
}
Write-Host "`n✓ All changed versions valid for branch $branch"
build-package:
runs-on: ubuntu-latest
needs: [validate-versions]
if: always() && (needs.validate-versions.result == 'success' || needs.validate-versions.result == 'skipped')
defaults:
run:
working-directory: ./Source
shell: pwsh
steps:
- uses: actions/checkout@v4
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 9.x
- name: Restore dependencies
run: dotnet restore Mal.SourceGeneratedDI.slnx
- name: Build solution
run: dotnet build Mal.SourceGeneratedDI.slnx --configuration Release --no-restore
- name: Pack generator package
run: dotnet pack Mal.SourceGeneratedDI/Mal.SourceGeneratedDI.csproj --configuration Release --no-build --output ${{ env.NuGetDirectory }}
- name: Pack abstractions package
run: dotnet pack Mal.SourceGeneratedDI.Abstractions/Mal.SourceGeneratedDI.Abstractions.csproj --configuration Release --no-build --output ${{ env.NuGetDirectory }}
- name: Upload NuGet package artifact
uses: actions/upload-artifact@v4
with:
name: nuget
if-no-files-found: error
retention-days: 7
path: ${{ env.NuGetDirectory }}/*.nupkg
deploy:
if: (github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/prerelease')) || (github.event_name == 'workflow_dispatch' && github.event.inputs.deployRelease == 'y')
runs-on: ubuntu-latest
environment: ${{ github.ref == 'refs/heads/main' && 'Deploy' || 'Prerelease' }}
needs: build-package
defaults:
run:
shell: pwsh
steps:
- name: Download NuGet artifact
uses: actions/download-artifact@v4
with:
name: nuget
path: ./nuget
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 9.x
- name: Publish NuGet packages
run: |
foreach($file in (Get-ChildItem "./nuget" -Recurse -Include *.nupkg)) {
dotnet nuget push $file --api-key "${{ secrets.NUGET_KEY }}" --source https://api.nuget.org/v3/index.json --skip-duplicate
}