|
| 1 | +name: Release to NuGet |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + tags: |
| 6 | + - 'v[0-9]+.[0-9]+.[0-9]+' # e.g., v1.0.0, v2.3.1 |
| 7 | + |
| 8 | +permissions: |
| 9 | + contents: write # Needed for GitHub Release creation |
| 10 | + id-token: write # Needed for GitHub OIDC token issuance |
| 11 | + |
| 12 | +env: |
| 13 | + DOTNET_CLI_TELEMETRY_OPTOUT: 1 |
| 14 | + DOTNET_NOLOGO: 1 |
| 15 | + libraryPath: './src/DurationMancer/DurationMancer.csproj' |
| 16 | + testsPath: './tests/DurationMancer.Tests/DurationMancer.Tests.csproj' |
| 17 | + buildConfiguration: 'Release' |
| 18 | + |
| 19 | +jobs: |
| 20 | + release: |
| 21 | + name: Pack & Publish |
| 22 | + runs-on: ubuntu-latest |
| 23 | + environment: nuget # GitHub Environment with protection rules (recommended) |
| 24 | + steps: |
| 25 | + |
| 26 | + # --- Checkout --- |
| 27 | + - name: Code checkout |
| 28 | + uses: actions/checkout@v4 |
| 29 | + with: |
| 30 | + fetch-depth: 0 |
| 31 | + |
| 32 | + # --- GitVersion --- |
| 33 | + - name: Install GitVersion |
| 34 | + uses: gittools/actions/gitversion/setup@v4.2.0 |
| 35 | + with: |
| 36 | + versionSpec: '6.5.x' |
| 37 | + - name: Determine Version |
| 38 | + id: version_step # step id used as reference for output values |
| 39 | + uses: gittools/actions/gitversion/command@v4.2.0 |
| 40 | + with: |
| 41 | + arguments: '/output buildserver /l console /config GitVersion.yaml /updateprojectfiles' # have to self-define all arguments; /updateprojectfiles not impl. in execute task |
| 42 | + |
| 43 | + # --- .NET Setup --- |
| 44 | + - name: Setup .NET |
| 45 | + uses: actions/setup-dotnet@v4 |
| 46 | + with: |
| 47 | + dotnet-version: 10.0.x |
| 48 | + |
| 49 | + # --- Restore --- |
| 50 | + - name: Restore library |
| 51 | + run: dotnet restore ${{ env.libraryPath }} |
| 52 | + - name: Restore tests |
| 53 | + run: dotnet restore ${{ env.testsPath }} |
| 54 | + |
| 55 | + # --- Build --- |
| 56 | + - name: Build library ${{ env.GitVersion_FullSemVer }} |
| 57 | + run: > |
| 58 | + dotnet build ${{ env.libraryPath }} |
| 59 | + --no-restore |
| 60 | + -c ${{ env.buildConfiguration }} |
| 61 | + - name: Build tests |
| 62 | + run: > |
| 63 | + dotnet build ${{ env.testsPath }} |
| 64 | + --no-restore |
| 65 | + -c ${{ env.buildConfiguration }} |
| 66 | +
|
| 67 | + # --- Test (safety net before publish) --- |
| 68 | + - name: Test |
| 69 | + run: > |
| 70 | + dotnet test ${{ env.testsPath }} |
| 71 | + --no-build |
| 72 | + -c ${{ env.buildConfiguration }} |
| 73 | + --logger "GitHubActions;summary.includePassedTests=true;summary.includeSkippedTests=true" |
| 74 | +
|
| 75 | + # --- Pack --- |
| 76 | + - name: Pack NuGet package |
| 77 | + run: > |
| 78 | + dotnet pack ${{ env.libraryPath }} |
| 79 | + --no-build |
| 80 | + -c ${{ env.buildConfiguration }} |
| 81 | + -o ./nupkg |
| 82 | + /p:PackageVersion=${{ env.GitVersion_FullSemVer }} |
| 83 | +
|
| 84 | + # --- Validate package --- |
| 85 | + - name: Validate NuGet package |
| 86 | + run: dotnet nuget verify ./nupkg/*.nupkg --all || true # 'verify' checks signatures; use 'true' fallback if unsigned |
| 87 | + |
| 88 | + # --- Trusted Publishing: OIDC login |
| 89 | + - name: NuGet login (OIDC → temp API key) |
| 90 | + env: |
| 91 | + NUGET_USER: ${{ secrets.NUGET_USER }} |
| 92 | + uses: NuGet/login@v1 |
| 93 | + id: nuget_login |
| 94 | + with: |
| 95 | + user: $NUGET_USER |
| 96 | + |
| 97 | + # --- Push to nuget.org --- |
| 98 | + - name: Push to NuGet.org |
| 99 | + env: |
| 100 | + NUGET_API_KEY: ${{ steps.nuget_login.outputs.NUGET_API_KEY }} |
| 101 | + run: > |
| 102 | + dotnet nuget push ./nupkg/*.nupkg |
| 103 | + --api-key $NUGET_API_KEY |
| 104 | + --source https://api.nuget.org/v3/index.json |
| 105 | + --skip-duplicate |
| 106 | +
|
| 107 | + # --- GitHub Release --- |
| 108 | + - name: Create GitHub Release |
| 109 | + uses: softprops/action-gh-release@v2 |
| 110 | + with: |
| 111 | + generate_release_notes: true |
| 112 | + files: ./nupkg/*.nupkg |
0 commit comments