Skip to content

v1.0.30

v1.0.30 #77

# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json
name: publish
on:
workflow_dispatch: # Allow running the workflow manually from the GitHub UI
push:
branches:
- 'net8' # Run the workflow when pushing to net8 branch
- 'net9' # Run the workflow when pushing to net9 branch
- 'net10' # Run the workflow when pushing to net10 branch
pull_request:
branches:
- '*' # Run the workflow for all pull requests
release:
types:
- published # Run the workflow when a new GitHub release is published
env:
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 1
DOTNET_NOLOGO: true
NuGetDirectory: ${{ github.workspace}}/nuget
defaults:
run:
shell: pwsh
jobs:
create_nuget:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Get all history to allow automatic versioning using MinVer
# Determine target framework based on branch or release version
- name: Determine Target Framework
id: target_framework
run: |
$branch = "${{ github.ref_name }}"
$targetFramework = "net9.0"
$dotnetVersion = "9.0.101"
# Check if it's a release event
if ("${{ github.event_name }}" -eq "release") {
$version = "${{ github.event.release.tag_name }}"
Write-Host "Release version: $version"
# Determine framework based on version pattern
if ($version -match "^v?1\.0\.") {
$targetFramework = "net8.0"
$dotnetVersion = "8.0.x"
Write-Host "Version 1.0.x detected - targeting .NET 8"
}
elseif ($version -match "^v?1\.1\.") {
$targetFramework = "net9.0"
$dotnetVersion = "9.0.101"
Write-Host "Version 1.1.x detected - targeting .NET 9"
}
elseif ($version -match "^v?1\.2\.") {
$targetFramework = "net10.0"
$dotnetVersion = "10.0.x"
Write-Host "Version 1.2.x detected - targeting .NET 10"
}
}
else {
# Determine framework based on branch name
Write-Host "Branch: $branch"
if ($branch -eq "net8" -or $branch -eq "refs/heads/net8") {
$targetFramework = "net8.0"
$dotnetVersion = "8.0.x"
Write-Host "net8 branch detected - targeting .NET 8"
}
elseif ($branch -eq "net9" -or $branch -eq "refs/heads/net9") {
$targetFramework = "net9.0"
$dotnetVersion = "9.0.101"
Write-Host "net9 branch detected - targeting .NET 9"
}
elseif ($branch -eq "net10" -or $branch -eq "refs/heads/net10") {
$targetFramework = "net10.0"
$dotnetVersion = "10.0.x"
Write-Host "net10 branch detected - targeting .NET 10"
}
}
Write-Host "Target Framework: $targetFramework"
Write-Host "Dotnet Version: $dotnetVersion"
"target_framework=$targetFramework" >> $env:GITHUB_OUTPUT
"dotnet_version=$dotnetVersion" >> $env:GITHUB_OUTPUT
# Install the .NET SDK based on detected version
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: ${{ steps.target_framework.outputs.dotnet_version }}
# Update TargetFramework in csproj
- name: Update Target Framework
run: |
$csprojPath = "Compila.Net.Utils/Compila.Net.Utils.csproj"
$targetFramework = "${{ steps.target_framework.outputs.target_framework }}"
Write-Host "Updating TargetFramework to $targetFramework in $csprojPath"
$content = Get-Content $csprojPath -Raw
$content = $content -replace '<TargetFramework>net\d+\.\d+</TargetFramework>', "<TargetFramework>$targetFramework</TargetFramework>"
Set-Content $csprojPath $content
Write-Host "Updated content:"
Get-Content $csprojPath | Select-String "TargetFramework"
# Create the NuGet package in the folder from the environment variable NuGetDirectory
- run: dotnet pack --configuration Release --output ${{ env.NuGetDirectory }}
# Publish the NuGet package as an artifact, so they can be used in the following jobs
- uses: actions/upload-artifact@v4
with:
name: nuget
if-no-files-found: error
retention-days: 7
path: ${{ env.NuGetDirectory }}/*.nupkg
validate_nuget:
runs-on: ubuntu-latest
needs: [ create_nuget ]
steps:
# Install multiple .NET SDK versions to support all target frameworks
- name: Setup .NET 8
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x
continue-on-error: true
- name: Setup .NET 9
uses: actions/setup-dotnet@v4
with:
dotnet-version: 9.0.x
continue-on-error: true
- name: Setup .NET 10
uses: actions/setup-dotnet@v4
with:
dotnet-version: 10.0.x
continue-on-error: true
# Download the NuGet package created in the previous job
- uses: actions/download-artifact@v4
with:
name: nuget
path: ${{ env.NuGetDirectory }}
- name: Install nuget validator
run: dotnet tool update Meziantou.Framework.NuGetPackageValidation.Tool --global
# Validate metadata and content of the NuGet package
# https://www.nuget.org/packages/Meziantou.Framework.NuGetPackageValidation.Tool#readme-body-tab
# If some rules are not applicable, you can disable them
# using the --excluded-rules or --excluded-rule-ids option
- name: Validate package
run: meziantou.validate-nuget-package (Get-ChildItem "${{ env.NuGetDirectory }}/*.nupkg") --excluded-rules Symbols
run_test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
# Install multiple .NET SDK versions to support all target frameworks
- name: Setup .NET 8
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x
continue-on-error: true
- name: Setup .NET 9
uses: actions/setup-dotnet@v4
with:
dotnet-version: 9.0.x
continue-on-error: true
- name: Setup .NET 10
uses: actions/setup-dotnet@v4
with:
dotnet-version: 10.0.x
continue-on-error: true
- name: Run tests
run: dotnet test --configuration Release
deploy:
# Publish only when creating a GitHub Release
# https://docs.github.com/en/repositories/releasing-projects-on-github/managing-releases-in-a-repository
# You can update this logic if you want to manage releases differently
if: github.event_name == 'release'
runs-on: ubuntu-latest
needs: [ validate_nuget, run_test ]
steps:
# Download the NuGet package created in the previous job
- uses: actions/download-artifact@v4
with:
name: nuget
path: ${{ env.NuGetDirectory }}
# Install multiple .NET SDK versions to support all target frameworks
- name: Setup .NET 8
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x
continue-on-error: true
- name: Setup .NET 9
uses: actions/setup-dotnet@v4
with:
dotnet-version: 9.0.x
continue-on-error: true
- name: Setup .NET 10
uses: actions/setup-dotnet@v4
with:
dotnet-version: 10.0.x
continue-on-error: true
# Publish all NuGet packages to NuGet.org
# Use --skip-duplicate to prevent errors if a package with the same version already exists.
# If you retry a failed workflow, already published packages will be skipped without error.
- name: Publish NuGet package
run: |
foreach($file in (Get-ChildItem "${{ env.NuGetDirectory }}" -Recurse -Include *.nupkg)) {
dotnet nuget push $file --api-key "${{ secrets.NUGET_APIKEY }}" --source https://api.nuget.org/v3/index.json --skip-duplicate
}