Skip to content

Publish NuGet Package #54

Publish NuGet Package

Publish NuGet Package #54

name: Publish NuGet Package
on:
workflow_dispatch:
push:
tags:
- 'v*'
permissions:
contents: write
packages: write
jobs:
publish:
runs-on: ubuntu-latest
steps:
# Step 1: Checkout the repository
- name: Checkout Repository
uses: actions/checkout@v3
with:
submodules: true
fetch-depth: 0
# Step 2: Set up .NET
- name: Set up .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: '7.0'
# Step 3: Generate Version
- name: Generate Version
id: generate_version
run: |
major=1
minor=0
build=$(date +'%Y%m%d')
revision=$(date +'%H%M')
version="$major.$minor.$build.$revision"
echo "version=$version" >> $GITHUB_ENV
# Step 4: Prepare Files
- name: Prepare Files
run: |
echo "Preparing Scripts-PowerShell directory..."
mkdir -p Scripts-PowerShell
# Copy LICENSE and README.md if they exist
if [[ -f LICENSE ]]; then
cp LICENSE Scripts-PowerShell/
else
echo "LICENSE file not found."
fi
if [[ -f README.md ]]; then
cp README.md Scripts-PowerShell/
else
echo "README.md file not found."
fi
# Process all directories except excluded ones
for dir in */ ; do
dir=${dir%/} # Remove trailing slash
if [[ "$dir" != "Scripts-PowerShell" && "$dir" != ".github" && "$dir" != "SysAdmin-Tools/GroupPolicyObjects-Templates" ]]; then
echo "Processing directory: $dir"
mkdir -p "Scripts-PowerShell/$dir"
# Copy files recursively, excluding hidden files and folders
find "$dir" -type f ! -name '.*' -exec cp --parents {} "Scripts-PowerShell/" \; || echo "Skipping directory: $dir"
fi
done
echo "Files prepared in Scripts-PowerShell directory:"
find Scripts-PowerShell -type f
# Step 5: Restore Dependencies (Optional for Project Structure)
- name: Restore Dependencies
run: dotnet restore || echo "Skipping restore."
# Step 6: Build and Pack NuGet Package
- name: Build and Pack
run: |
echo "Packing the NuGet package..."
mkdir -p ./artifacts
dotnet pack nuget.package.csproj --configuration Release --output ./artifacts || exit 1
echo "Checking artifacts directory..."
ls ./artifacts
package_file=$(find ./artifacts -name "Scripts-PowerShell*.nupkg")
if [[ -z "$package_file" ]]; then
echo "Error: Package not found in ./artifacts"
exit 1
fi
echo "Package successfully created: $package_file"
# Step 7: Publish Package to GitHub Packages
- name: Publish to GitHub Packages
env:
NUGET_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
dotnet nuget push ./artifacts/Scripts-PowerShell*.nupkg --api-key $NUGET_AUTH_TOKEN --source "github" --skip-duplicate