Codex: Update package dependencies and workflow actions #8
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: Build, Test, and Publish to GitHub Packages | |
| on: | |
| push: | |
| branches: [ master ] | |
| pull_request: | |
| branches: [ master ] | |
| permissions: | |
| contents: read | |
| packages: write | |
| jobs: | |
| build: | |
| runs-on: windows-latest | |
| steps: | |
| - name: Checkout source code | |
| uses: actions/checkout@v5 | |
| - name: Setup .NET | |
| uses: actions/setup-dotnet@v5 | |
| with: | |
| dotnet-version: '8.0.x' | |
| - name: Create nuget.config for GitHub Packages | |
| shell: pwsh | |
| run: | | |
| $lines = @() | |
| $lines += "<?xml version='1.0' encoding='utf-8'?>" | |
| $lines += '<configuration>' | |
| $lines += ' <packageSources>' | |
| $lines += " <add key='github' value='https://nuget.pkg.github.com/$env:GITHUB_REPOSITORY_OWNER/index.json' />" | |
| $lines += " <add key='nuget.org' value='https://api.nuget.org/v3/index.json' />" | |
| $lines += ' </packageSources>' | |
| $lines += ' <packageSourceCredentials>' | |
| $lines += ' <github>' | |
| $lines += " <add key='Username' value='$env:GITHUB_ACTOR' />" | |
| $lines += " <add key='ClearTextPassword' value='$env:SOMENEWKID_PACKAGES_PAT' />" | |
| $lines += ' </github>' | |
| $lines += ' </packageSourceCredentials>' | |
| $lines += '</configuration>' | |
| $lines | Out-File -FilePath "nuget.config" -Encoding utf8 | |
| env: | |
| SOMENEWKID_PACKAGES_PAT: ${{ secrets.SOMENEWKID_PACKAGES_PAT }} | |
| GITHUB_ACTOR: ${{ github.actor }} | |
| GITHUB_REPOSITORY_OWNER: ${{ github.repository_owner }} | |
| - name: Restore dependencies | |
| run: dotnet restore --configfile nuget.config | |
| - name: Build solution | |
| run: dotnet build --no-restore --configuration Release | |
| - name: Run xUnit tests (SolarSystem.UnitTests only) | |
| run: dotnet test ./SolarSystem.UnitTests/SolarSystem.UnitTests.csproj --no-build --configuration Release --verbosity normal | |
| - name: Generate version string with date/time | |
| id: version | |
| shell: bash | |
| run: | | |
| echo "VERSION=1.0.0-$(date -u +%Y%m%d-%H%M)" >> $GITHUB_ENV | |
| - name: Pack library | |
| run: dotnet pack ./SolarSystem/SolarSystem.csproj --no-build --configuration Release --output ${{ github.workspace }}/nupkg -p:PackageVersion=${{ env.VERSION }} | |
| - name: Publish to GitHub Packages | |
| if: github.event_name == 'push' | |
| run: dotnet nuget push "${{ github.workspace }}\nupkg\*.nupkg" --source "https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json" --api-key ${{ secrets.GITHUB_TOKEN }} | |
| - name: Delete older package versions | |
| if: github.event_name == 'push' | |
| uses: actions/github-script@v8 | |
| with: | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| script: | | |
| const packageType = "nuget"; | |
| const packageName = "Genova.SolarSystem"; | |
| const keep = 2; | |
| const owner = context.repo.owner; | |
| const ownerType = context.payload.repository.owner.type; | |
| let versions; | |
| if (ownerType === "Organization") { | |
| versions = await github.paginate( | |
| "GET /orgs/{org}/packages/{package_type}/{package_name}/versions", | |
| { | |
| org: owner, | |
| package_type: packageType, | |
| package_name: packageName, | |
| per_page: 100 | |
| } | |
| ); | |
| } else { | |
| versions = await github.paginate( | |
| "GET /users/{username}/packages/{package_type}/{package_name}/versions", | |
| { | |
| username: owner, | |
| package_type: packageType, | |
| package_name: packageName, | |
| per_page: 100 | |
| } | |
| ); | |
| } | |
| versions.sort((a, b) => new Date(b.created_at) - new Date(a.created_at)); | |
| core.info(`Found ${versions.length} package version(s) for ${packageName}.`); | |
| if (versions.length <= keep) { | |
| core.info(`Nothing to delete. Keeping all ${versions.length} version(s).`); | |
| return; | |
| } | |
| const toDelete = versions.slice(keep); | |
| for (const version of toDelete) { | |
| core.info(`Deleting version id=${version.id}, name=${version.name}, created_at=${version.created_at}`); | |
| if (ownerType === "Organization") { | |
| await github.request( | |
| "DELETE /orgs/{org}/packages/{package_type}/{package_name}/versions/{package_version_id}", | |
| { | |
| org: owner, | |
| package_type: packageType, | |
| package_name: packageName, | |
| package_version_id: version.id | |
| } | |
| ); | |
| } else { | |
| await github.request( | |
| "DELETE /users/{username}/packages/{package_type}/{package_name}/versions/{package_version_id}", | |
| { | |
| username: owner, | |
| package_type: packageType, | |
| package_name: packageName, | |
| package_version_id: version.id | |
| } | |
| ); | |
| } | |
| } | |
| core.info(`Kept the latest ${keep} version(s). Deleted ${toDelete.length} older version(s).`); |