Skip to content

Publish NuGet Package #45

Publish NuGet Package

Publish NuGet Package #45

name: Publish NuGet Package
on:
workflow_dispatch: # Allows manual trigger of the workflow
push:
tags:
- 'v*' # Trigger on tags starting with 'v'
permissions:
contents: write
packages: write
jobs:
publish:
runs-on: ubuntu-latest
steps:
# Step 1: Check out 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: |
# Generate a valid version in the format Major.Minor.Build.Revision
major=1
minor=0
build=$(date +'%Y%m%d') # Date as build number
revision=$(date +'%H%M') # Hour and minute as revision
version="$major.$minor.$build.$revision"
echo "version=$version" >> $GITHUB_ENV
echo "Generated version: $version"
# Step 4: Prepare Files for Packaging
- name: Prepare Files
run: |
echo "Preparing files for packaging..."
mkdir -p Scripts-PowerShell
# Copy LICENSE and README.md to the main folder
cp LICENSE README.md Scripts-PowerShell/ || echo "LICENSE or README.md not found, skipping."
# Copy all directories except .github and configuration files
for dir in */; do
dir=${dir%/}
if [[ "$dir" != "Scripts-PowerShell" && "$dir" != ".github" ]]; then
echo "Processing directory: $dir"
mkdir -p Scripts-PowerShell/"$dir"
cp -r "$dir"/* Scripts-PowerShell/"$dir"/ || echo "Skipping $dir"
fi
done
# Step 5: Create Project File for NuGet
- name: Create Project File
run: |
echo "Creating nuget.package.csproj..."
cat > nuget.package.csproj <<- EOF
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<PackageId>Scripts-PowerShell</PackageId>
<Authors>Luiz Hamilton Silva</Authors>
<Company>Brazilianscriptguy</Company>
<Product>Scripts-PowerShell</Product>
<Description>
Comprehensive suite of PowerShell and VBScript tools automates Active Directory tasks, advances forensic analysis,
and simplifies script creation. Designed for managing Windows Servers and workstations, these tools ensure accuracy,
efficiency, scalability, and adaptability.
</Description>
<PackageTags>PowerShell;Automation;SysAdmin;ActiveDirectory;Forensics</PackageTags>
<RepositoryUrl>https://github.com/brazilianscriptguy/Windows-SysAdmin-ProSuite</RepositoryUrl>
<Version>${{ env.version }}</Version>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<NoBuild>true</NoBuild>
<PackageOutputPath>./artifacts</PackageOutputPath>
<NoWarn>NU5110;NU5111;NU5123</NoWarn> <!-- Suppress warnings -->
</PropertyGroup>
<ItemGroup>
<Content Include="Scripts-PowerShell/**" PackagePath="content/" />
<Content Remove="Scripts-PowerShell/SysAdmin-Tools/GroupPolicyObjects-Templates/**" />
</ItemGroup>
</Project>
EOF
echo "nuget.package.csproj created."
# Step 6: Build and Pack the NuGet Package
- name: Build and Pack
run: |
echo "Packing the NuGet package..."
mkdir -p ./artifacts # Ensure the artifacts directory exists
dotnet pack nuget.package.csproj --configuration Release --output ./artifacts || {
echo "Error: Failed to pack NuGet package.";
exit 1;
}
# Verify the package exists
package_file=$(find ./artifacts -name "Scripts-PowerShell*.nupkg")
if [[ -z "$package_file" ]]; then
echo "Error: NuGet package not found in ./artifacts";
ls ./artifacts
exit 1;
fi
echo "Package successfully created: $package_file"
# Step 7: Publish to GitHub Packages
- name: Publish to GitHub Packages
env:
NUGET_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
echo "Publishing package to GitHub Packages..."
dotnet nuget push ./artifacts/Scripts-PowerShell*.nupkg --api-key $NUGET_AUTH_TOKEN --source "github" --skip-duplicate