diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6376137..5527601 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,16 +1,13 @@ -# If this file is renamed, the incrementing run attempt number will be reset. - name: CI +# Pull requests only: pushes to `dev` and `main` are built, tested, and published by `publish.yml`. on: - push: - branches: [ "dev", "main" ] pull_request: branches: [ "dev", "main" ] - + env: - CI_BUILD_NUMBER_BASE: ${{ github.run_number }} - CI_TARGET_BRANCH: ${{ github.head_ref || github.ref_name }} + CI_BUILD_NUMBER: ${{ github.run_number }} + CI_TARGET_BRANCH: ${{ github.head_ref }} jobs: build: @@ -18,7 +15,7 @@ jobs: runs-on: ubuntu-24.04 permissions: - contents: write + contents: read steps: - uses: actions/checkout@v4 @@ -26,14 +23,9 @@ jobs: uses: actions/setup-dotnet@v4 with: dotnet-version: 10.0.x - - name: Compute build number - run: | - echo "CI_BUILD_NUMBER=$(($CI_BUILD_NUMBER_BASE+200))" >> $GITHUB_ENV - - name: Build and Publish + - name: Build env: DOTNET_CLI_TELEMETRY_OPTOUT: true - NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }} - GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} shell: pwsh run: | ./Build.ps1 diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..e87bdff --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,65 @@ +# Publishes packages to NuGet.org using trusted publishing. The NuGet.org trusted publishing policy +# identifies this workflow by file name; if this file is renamed, the policy must be updated, and the +# incrementing run number used to version pre-release packages will be reset. + +name: Publish + +on: + push: + branches: [ "dev", "main" ] + +env: + CI_BUILD_NUMBER_BASE: ${{ github.run_number }} + CI_TARGET_BRANCH: ${{ github.ref_name }} + +jobs: + publish: + + runs-on: ubuntu-24.04 + + permissions: + # Required for NuGet.org trusted publishing. + id-token: write + # Required for creating GitHub releases. + contents: write + + steps: + - uses: actions/checkout@v4 + - name: Setup + uses: actions/setup-dotnet@v4 + with: + dotnet-version: 10.0.x + - name: Compute build number + shell: bash + run: | + echo "CI_BUILD_NUMBER=$(($CI_BUILD_NUMBER_BASE+500))" >> $GITHUB_ENV + - name: Build + env: + DOTNET_CLI_TELEMETRY_OPTOUT: true + shell: pwsh + run: | + ./Build.ps1 + - name: Log in to NuGet + uses: NuGet/login@v1 + id: login + with: + user: ${{ secrets.NUGET_USER }} + - name: Push to NuGet + shell: pwsh + run: | + $packages = @(Get-ChildItem ./artifacts/*.nupkg) + if ($packages.Count -eq 0) { throw "No packages found in ./artifacts" } + foreach ($nupkg in $packages) { + dotnet nuget push "$nupkg" --api-key "${{ steps.login.outputs.NUGET_API_KEY }}" --source https://api.nuget.org/v3/index.json + if ($LASTEXITCODE -ne 0) { throw "Publishing failed" } + } + - name: Create GitHub release + # Builds from `main` are versioned without a pre-release suffix; see `Build.ps1`. + if: github.ref_name == 'main' + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + shell: pwsh + run: | + $version = ([Xml] (Get-Content ./Directory.Build.props)).Project.PropertyGroup.VersionPrefix + gh release create "v$version" --title "v$version" --target "${{ github.sha }}" --generate-notes (Get-Item ./artifacts/*.nupkg, ./artifacts/*.snupkg) + if ($LASTEXITCODE -ne 0) { throw "Release creation failed" } diff --git a/Build.ps1 b/Build.ps1 index 952dce0..5c26f6c 100644 --- a/Build.ps1 +++ b/Build.ps1 @@ -57,21 +57,3 @@ foreach ($test in Get-ChildItem test/*.Tests) { } Pop-Location - -if ($env:NUGET_API_KEY) { - # GitHub Actions will only supply this to branch builds and not PRs. We publish - # builds from any branch this action targets (i.e. main and dev). - - Write-Output "build: Publishing NuGet packages" - - foreach ($nupkg in Get-ChildItem artifacts/*.nupkg) { - & dotnet nuget push -k $env:NUGET_API_KEY -s https://api.nuget.org/v3/index.json "$nupkg" - if($LASTEXITCODE -ne 0) { throw "Publishing failed" } - } - - if (!($suffix)) { - Write-Output "build: Creating release for version $versionPrefix" - - iex "gh release create v$versionPrefix --title v$versionPrefix --generate-notes $(get-item ./artifacts/*.nupkg) $(get-item ./artifacts/*.snupkg)" - } -}