Skip to content

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

Update publish-all-nuget-packages.yml

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

name: Publish NuGet Packages
on:
push:
branches: [main] # For testing
release:
types: [published, prerelease]
permissions:
contents: write
packages: write
jobs:
publish-nuget:
runs-on: ubuntu-latest
strategy:
matrix:
package:
- BlueTeam-Tools
- Core-ScriptLibrary
- ITSM-Templates-SVR
- ITSM-Templates-WKS
- SysAdmin-Tool
fail-fast: false # Continue with other packages if one fails
steps:
- name: 🧾 Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
submodules: recursive
- name: 🛠️ Debug workflow start
run: |
echo "Workflow started for package ${{ matrix.package }}"
echo "Event: ${{ github.event_name }}"
echo "Repository: ${{ github.repository }}"
echo "Workflow file: ${{ github.workflow }}"
echo "Listing repository contents:"
ls -R
echo "Checking for .nuspec files in root:"
ls -l *.nuspec || echo "No .nuspec files found in root"
echo "Checking .NET SDK version:"
dotnet --version
echo "Checking for mono and nuget availability:"
which mono || echo "mono not found"
which nuget || echo "nuget not found"
- name: 📦 Install Mono and NuGet CLI
run: |
echo "Installing mono-complete"
sudo apt-get update
sudo apt-get install -y mono-complete
echo "Checking mono version:"
mono --version
echo "Installing nuget.exe"
wget -O nuget.exe https://dist.nuget.org/win-x86-commandline/latest/nuget.exe
chmod +x nuget.exe
echo "Moving nuget.exe to /usr/local/bin"
sudo mv nuget.exe /usr/local/bin/nuget
echo "Verifying nuget command:"
which nuget
mono /usr/local/bin/nuget help | head -n 1
- name: 📁 Prepare and stage files
id: prepare
run: |
pkg="${{ matrix.package }}"
echo "Preparing package $pkg"
if [ ! -f "$pkg.nuspec" ]; then
echo "⚠️ Warning: NuSpec file $pkg.nuspec missing in root, skipping package"
echo "skip=true" >> "$GITHUB_OUTPUT"
exit 0
fi
mkdir -p ReadmeCopies nupkg-out "temp-$pkg"
if [ -d "$pkg" ]; then
echo "Copying package directory $pkg to temp-$pkg"
cp -r "$pkg/." "temp-$pkg/"
else
echo "⚠️ Warning: Folder $pkg missing, proceeding with .nuspec only"
fi
cp "$pkg.nuspec" "temp-$pkg/$pkg.nuspec"
cp "$pkg/README.md" "ReadmeCopies/$pkg-README.md" || echo "⚠️ Warning: README.md not found for $pkg"
echo "Listing temp-$pkg contents:"
ls -l "temp-$pkg"
echo "Checking .nuspec file contents:"
cat "$pkg.nuspec"
echo "Validating .nuspec file paths:"
grep '<file src="' "$pkg.nuspec" | while read -r line ; do
src=$(echo "$line" | sed -E 's/.*src="([^"]+)".*/\1/')
resolved_src=$(echo "$src" | sed "s|SysAdmin-Tools|SysAdmin-Tool|g")
if [[ "$src" != "$resolved_src" ]]; then
echo "⚠️ Warning: Replacing SysAdmin-Tools with SysAdmin-Tool in $pkg.nuspec"
sed -i "s|$src|$resolved_src|g" "temp-$pkg/$pkg.nuspec"
fi
if [ -n "$src" ] && ! ls "temp-$pkg/$src" 2>/dev/null; then
echo "⚠️ Warning: Path $src in $pkg.nuspec does not exist in temp-$pkg"
fi
done
echo "skip=false" >> "$GITHUB_OUTPUT"
- name: 📖 Extract version and changelog
id: changelog
if: steps.prepare.outputs.skip != 'true'
run: |
pkg="${{ matrix.package }}"
if [ ! -f "CHANGELOG.md" ]; then
echo "⚠️ Warning: CHANGELOG.md missing, using default notes"
echo "notes=No changelog available" >> "$GITHUB_OUTPUT"
echo "version=0.0.0" >> "$GITHUB_OUTPUT"
exit 0
fi
version=$(grep -m1 '<version>' "$pkg.nuspec" | sed -E 's/.*<version>(.+)<\/version>.*/\1/' || echo "0.0.0")
if [ -z "$version" ]; then
echo "⚠️ Warning: Version not found in $pkg.nuspec, using default"
version="0.0.0"
fi
echo "version=$version" >> "$GITHUB_OUTPUT"
changelog=$(awk "/^## \\[$version\\]/,/^## \\[/" CHANGELOG.md | sed '1d;$d' || echo "No changelog found for version $version")
changelog_sanitized=$(printf "%s" "$changelog" | sed ':a;N;$!ba;s/\n/\\n/g')
echo "notes=$changelog_sanitized" >> "$GITHUB_OUTPUT"
- name: 📦 Pack NuGet package
if: steps.prepare.outputs.skip != 'true'
run: |
pkg="${{ matrix.package }}"
if [ ! -d "temp-$pkg" ]; then
echo "❌ Error: Directory temp-$pkg missing, skipping package"
exit 0
fi
pushd "temp-$pkg"
echo "Running nuget pack for $pkg.nuspec"
mono /usr/local/bin/nuget pack "$pkg.nuspec" \
-OutputDirectory ../nupkg-out \
-Properties "ReadMeFile=../ReadmeCopies/$pkg-README.md;SymbolPackageFormat=snupkg" \
|| { echo "❌ Error: Failed to pack $pkg.nuspec"; exit 0; }
popd
- name: 🚀 Push to GitHub Packages
if: steps.prepare.outputs.skip != 'true'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
for pkg_path in nupkg-out/*.nupkg nupkg-out/*.snupkg; do
if [ -f "$pkg_path" ]; then
echo "Pushing $pkg_path to GitHub Packages"
dotnet nuget push "$pkg_path" \
--source "https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json" \
--api-key "$GITHUB_TOKEN" \
--skip-duplicate
fi
done
- name: 🚀 Push to NuGet.org
if: steps.prepare.outputs.skip != 'true'
env:
NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }}
run: |
for pkg_path in nupkg-out/*.nupkg nupkg-out/*.snupkg; do
if [ -f "$pkg_path" ]; then
echo "Pushing $pkg_path to NuGet.org"
dotnet nuget push "$pkg_path" \
--source "https://api.nuget.org/v3/index.json" \
--api-key "$NUGET_API_KEY" \
--skip-duplicate
fi
done
- name: 🧹 Clean up
if: always()
run: |
rm -rf "temp-${{ matrix.package }}" ReadmeCopies nupkg-out || true
create-release-tag:
runs-on: ubuntu-latest
needs: publish-nuget
if: github.event_name == 'release'
steps:
- name: 🧾 Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: 🏷️ Create Git tag
run: |
version=$(grep -m1 '<version>' *.nuspec | head -n 1 | sed -E 's/.*<version>(.+)<\/version>.*/\1/' || echo "0.0.0")
tag="v$version"
if git tag --list | grep -q "^$tag$"; then
echo "Tag $tag already exists, skipping creation"
else
git config user.name "github-actions"
git config user.email "github-actions@github.com"
git tag "$tag"
git push origin "$tag"
fi