refactor: simplify test project dependencies by removing net7.0 condi… #26
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Publish NuGet Package to GitHub Packages and NuGet.org | |
| on: | |
| push: | |
| branches: | |
| - main | |
| tags: | |
| - 'v*' | |
| workflow_dispatch: | |
| jobs: | |
| build-and-publish: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| packages: write | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Setup .NET | |
| uses: actions/setup-dotnet@v4 | |
| with: | |
| dotnet-version: '9.0.x' | |
| - name: Extract Version from .csproj | |
| id: get_version | |
| run: | | |
| $ErrorActionPreference = "Stop" | |
| Write-Host "Extract Version Step: Initializing..." | |
| Write-Host "Current directory: $(Get-Location)" | |
| Write-Host "Listing files in current directory (root of checkout):" | |
| Get-ChildItem -Path "./" | |
| $projectFilePath = "Rustify.csproj" | |
| Write-Host "Project file path: $projectFilePath" | |
| if (-not (Test-Path $projectFilePath)) { | |
| Write-Error "Error: Project file '$projectFilePath' not found in current directory '$(Get-Location)'." | |
| exit 1 | |
| } | |
| Write-Host "Project file '$projectFilePath' found." | |
| $versionValue = "" | |
| try { | |
| $projectFileContent = Get-Content -Path $projectFilePath -Raw | |
| $xmlDoc = [xml]$projectFileContent | |
| $versionNode = $xmlDoc.Project.PropertyGroup.Version | |
| if ($null -eq $versionNode) { | |
| Write-Error "Error: <Version> XML node not found in '$projectFilePath'." | |
| exit 1 | |
| } | |
| $versionValue = $versionNode.Trim() | |
| Write-Host "Raw version string from XML: '$($xmlDoc.Project.PropertyGroup.Version)'" | |
| Write-Host "Trimmed version string: '$versionValue'" | |
| } catch { | |
| Write-Error "Error: Failed to parse version from '$projectFilePath'. Exception: $($_.Exception.Message)" | |
| exit 1 | |
| } | |
| if ([string]::IsNullOrWhiteSpace($versionValue)) { | |
| Write-Error "Error: Version extracted from '$projectFilePath' is null, empty, or whitespace." | |
| exit 1 | |
| } | |
| Write-Host "Successfully extracted version: '$versionValue'" | |
| Add-Content -Path $env:GITHUB_OUTPUT -Value "VERSION=$versionValue" | |
| Write-Host "Attempted to set output VERSION to '$versionValue'" | |
| Write-Host "Extract Version Step: Finalizing." | |
| shell: pwsh | |
| - name: Create Git Tag if version changed | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| run: | | |
| $ErrorActionPreference = "Stop" # Stop on terminating errors for subsequent commands | |
| $extractedVersion = "${{ steps.get_version.outputs.VERSION }}" | |
| Write-Host "Version received from 'Extract Version' step: '$extractedVersion'" | |
| if ([string]::IsNullOrWhiteSpace($extractedVersion)) { | |
| Write-Error "Error: Extracted version is empty. Cannot create tag." | |
| exit 1 | |
| } | |
| $tagName = "v$extractedVersion" | |
| Write-Host "Attempting to create or check tag: '$tagName'" | |
| $tagExists = $false | |
| $originalErrorActionPreference = $ErrorActionPreference | |
| $ErrorActionPreference = "SilentlyContinue" | |
| Write-Host "Checking for local tag '$tagName'..." | |
| git show-ref --verify --quiet "refs/tags/$tagName" | |
| if ($LASTEXITCODE -eq 0) { | |
| $tagExists = $true | |
| Write-Host "Tag '$tagName' found locally." | |
| } else { | |
| Write-Host "Tag '$tagName' not found locally. Checking remote 'origin'..." | |
| git ls-remote --exit-code --tags origin "refs/tags/$tagName" | |
| if ($LASTEXITCODE -eq 0) { | |
| $tagExists = $true | |
| Write-Host "Tag '$tagName' found on remote 'origin'." | |
| } else { | |
| Write-Host "Tag '$tagName' not found on remote 'origin' either." | |
| } | |
| } | |
| $ErrorActionPreference = $originalErrorActionPreference | |
| if ($tagExists) { | |
| Write-Host "Tag '$tagName' already exists (locally or remotely). Skipping tag creation and push." | |
| } else { | |
| Write-Host "Tag '$tagName' does not exist. Creating tag..." | |
| $ErrorActionPreference = "Stop" # Ensure critical git commands stop script on failure | |
| git config user.name "${{ github.actor }}" | |
| git config user.email "${{ github.actor_id }}+${{ github.actor }}@users.noreply.github.com" | |
| git tag "$tagName" -m "Release version $tagName" | |
| Write-Host "Created tag '$tagName' locally." | |
| Write-Host "Pushing tag '$tagName' to origin..." | |
| git push origin "$tagName" | |
| Write-Host "Successfully pushed tag '$tagName'." | |
| } | |
| shell: pwsh | |
| - name: Restore dependencies | |
| run: dotnet restore Rustify.csproj | |
| - name: Build for .NET 8.0 | |
| run: dotnet build Rustify.csproj --configuration Release --no-restore -f net8.0 | |
| - name: Build for .NET 9.0 | |
| run: dotnet build Rustify.csproj --configuration Release --no-restore -f net9.0 | |
| - name: Build for .NET 10.0 | |
| run: dotnet build Rustify.csproj --configuration Release --no-restore -f net10.0 | |
| - name: Pack | |
| run: dotnet pack Rustify.csproj --configuration Release --no-build --output ./nupkgs | |
| - name: Publish to GitHub Packages | |
| run: dotnet nuget push ./nupkgs/*.nupkg --api-key ${{ secrets.GITHUB_TOKEN }} --source "https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json" --skip-duplicate | |
| - name: Publish to NuGet.org | |
| env: | |
| NUGET_API_KEY_ENV: ${{ secrets.NUGET_API_KEY }} | |
| if: ${{ startsWith(github.ref, 'refs/tags/v') && env.NUGET_API_KEY_ENV != '' }} | |
| run: dotnet nuget push ./nupkgs/*.nupkg --api-key ${{ secrets.NUGET_API_KEY }} --source "https://api.nuget.org/v3/index.json" --skip-duplicate |