Skip to content

Update publish-all-nuget-packages.yml #49

Update publish-all-nuget-packages.yml

Update publish-all-nuget-packages.yml #49

name: Publish NuGet Packages
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
build-and-publish:
runs-on: windows-latest
strategy:
matrix:
package:
- { id: BlueTeamTools, folder: BlueTeam-Tools, nuspec: BlueTeamTools.nuspec }
- { id: CoreScriptLibrary, folder: Core-ScriptLibrary, nuspec: CoreScriptLibrary.nuspec }
- { id: ITSM-Templates-SVR, folder: ITSM-Templates-SVR, nuspec: ITSM-Templates-SVR.nuspec }
- { id: ITSM-Templates-WKS, folder: ITSM-Templates-WKS, nuspec: ITSM-Templates-WKS.nuspec }
- { id: SysAdminTools, folder: SysAdmin-Tools, nuspec: SysAdminTools.nuspec }
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch all history for changelog and tagging
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: '8.0.x'
- name: Install NuGet CLI
run: choco install nuget.commandline --version 6.11.0
shell: powershell
- name: Validate .nuspec metadata
run: |
$nuspecPath = "${{ matrix.package.nuspec }}"
[xml]$nuspec = Get-Content $nuspecPath
if (-not $nuspec.package.metadata.id) { throw "Missing id in $nuspecPath" }
if (-not $nuspec.package.metadata.version) { throw "Missing version in $nuspecPath" }
if (-not $nuspec.package.metadata.authors) { throw "Missing authors in $nuspecPath" }
if (-not $nuspec.package.metadata.description) { throw "Missing description in $nuspecPath" }
if (-not $nuspec.package.metadata.license) { throw "Missing license in $nuspecPath" }
if (-not $nuspec.package.metadata.projectUrl) { throw "Missing projectUrl in $nuspecPath" }
Write-Host "Metadata validation passed for $nuspecPath"
shell: powershell
- name: Extract version from .nuspec
id: extract_version
run: |
$nuspecPath = "${{ matrix.package.nuspec }}"
[xml]$nuspec = Get-Content $nuspecPath
$version = $nuspec.package.metadata.version
echo "PACKAGE_VERSION=$version" >> $env:GITHUB_ENV
shell: powershell
- name: Extract changelog for package
id: extract_changelog
run: |
$changelog = Get-Content CHANGELOG.md -Raw
$version = "${{ env.PACKAGE_VERSION }}"
$pattern = "(?s)## \[$version\][\s\S]*?(?=## \[|$)"
$extracted = [regex]::Match($changelog, $pattern).Value
if ($extracted) {
$extracted = $extracted -replace '## \[$version\]', ''
$extracted = $extracted.Trim()
} else {
$extracted = "No changelog entries found for version $version"
}
$extracted = $extracted -replace '"', '""'
echo "CHANGELOG_CONTENT=$extracted" >> $env:GITHUB_ENV
shell: powershell
- name: Update .nuspec with changelog and README
run: |
$nuspecPath = "${{ matrix.package.nuspec }}"
[xml]$nuspec = Get-Content $nuspecPath
$releaseNotes = "${{ env.CHANGELOG_CONTENT }}"
if (-not $nuspec.package.metadata.releaseNotes) {
$releaseNotesElement = $nuspec.CreateElement("releaseNotes")
$releaseNotesElement.InnerText = $releaseNotes
$nuspec.package.metadata.AppendChild($releaseNotesElement)
} else {
$nuspec.package.metadata.releaseNotes = $releaseNotes
}
# Ensure README.md from package folder is included
$readmePath = "${{ matrix.package.folder }}/README.md"
if (Test-Path $readmePath) {
$filesNode = $nuspec.package.files
if (-not $filesNode) {
$filesNode = $nuspec.CreateElement("files")
$nuspec.package.AppendChild($filesNode)
}
$fileNode = $nuspec.CreateElement("file")
$fileNode.SetAttribute("src", $readmePath)
$fileNode.SetAttribute("target", "content")
$filesNode.AppendChild($fileNode)
}
$nuspec.Save($nuspecPath)
shell: powershell
- name: Pack NuGet package
run: |
nuget pack ${{ matrix.package.nuspec }} -OutputDirectory ./nupkg -Symbols -SymbolPackageFormat snupkg -NonInteractive
shell: cmd
- name: Publish to nuget.org
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
run: |
nuget push ./nupkg/${{ matrix.package.id }}.${{ env.PACKAGE_VERSION }}.nupkg -Source https://api.nuget.org/v3/index.json -ApiKey ${{ secrets.NUGET_API_KEY }} -SymbolSource https://api.nuget.org/v3/index.json -SymbolApiKey ${{ secrets.NUGET_API_KEY }} -NonInteractive
nuget push ./nupkg/${{ matrix.package.id }}.${{ env.PACKAGE_VERSION }}.snupkg -Source https://api.nuget.org/v3/index.json -ApiKey ${{ secrets.NUGET_API_KEY }} -NonInteractive
shell: cmd
- name: Publish to GitHub Packages
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
nuget setApiKey ${{ secrets.GITHUB_TOKEN }} -Source https://nuget.pkg.github.com/your-org/index.json -NonInteractive
nuget push ./nupkg/${{ matrix.package.id }}.${{ env.PACKAGE_VERSION }}.nupkg -Source https://nuget.pkg.github.com/your-org/index.json -NonInteractive
nuget push ./nupkg/${{ matrix.package.id }}.${{ env.PACKAGE_VERSION }}.snupkg -Source https://nuget.pkg.github.com/your-org/index.json -NonInteractive
shell: cmd
- name: Create Git tag
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
run: |
git config user.name "GitHub Actions"
git config user.email "actions@github.com"
git tag ${{ matrix.package.id }}-v${{ env.PACKAGE_VERSION }}
git push origin ${{ matrix.package.id }}-v${{ env.PACKAGE_VERSION }}
shell: bash