|
| 1 | +name: Publish NuGet Package |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + dry_run: |
| 7 | + description: 'Dry run (do not publish)' |
| 8 | + required: false |
| 9 | + default: 'false' |
| 10 | + type: choice |
| 11 | + options: |
| 12 | + - 'false' |
| 13 | + - 'true' |
| 14 | + |
| 15 | +jobs: |
| 16 | + build: |
| 17 | + runs-on: ubuntu-latest |
| 18 | + |
| 19 | + steps: |
| 20 | + - name: Checkout code |
| 21 | + uses: actions/checkout@v6 |
| 22 | + with: |
| 23 | + fetch-depth: 0 # depth is needed for nbgv |
| 24 | + |
| 25 | + - name: Setup .NET |
| 26 | + uses: actions/setup-dotnet@v5 |
| 27 | + with: |
| 28 | + dotnet-version: '10.0.x' |
| 29 | + |
| 30 | + - name: Cache NuGet packages |
| 31 | + uses: actions/cache@v5 |
| 32 | + with: |
| 33 | + path: ~/.nuget/packages |
| 34 | + key: ${{ runner.os }}-nuget-${{ hashFiles('**/*.csproj', '**/Directory.Packages.props') }} |
| 35 | + restore-keys: | |
| 36 | + ${{ runner.os }}-nuget- |
| 37 | +
|
| 38 | + - name: Setup versioning |
| 39 | + run: | |
| 40 | + dotnet tool install --global nbgv |
| 41 | + nbgv cloud |
| 42 | +
|
| 43 | + - name: Restore dependencies |
| 44 | + run: dotnet restore |
| 45 | + |
| 46 | + - name: Build |
| 47 | + run: dotnet build --no-restore --configuration Release |
| 48 | + |
| 49 | + - name: Run tests |
| 50 | + run: dotnet test --no-build --configuration Release |
| 51 | + |
| 52 | + - name: Pack |
| 53 | + run: dotnet pack --no-build --configuration Release --output nupkgs |
| 54 | + |
| 55 | + - name: Install SBOM tool |
| 56 | + run: dotnet tool install --global Microsoft.Sbom.DotNetTool --version 4.1.5 |
| 57 | + |
| 58 | + - name: Generate SBOM |
| 59 | + shell: pwsh |
| 60 | + run: | |
| 61 | + $packagesDirectory = Join-Path $PWD 'nupkgs' |
| 62 | + $sbomDirectory = Join-Path $PWD 'sbom' |
| 63 | + $dropDirectory = Join-Path $sbomDirectory 'drop' |
| 64 | +
|
| 65 | + New-Item -ItemType Directory -Force -Path $sbomDirectory | Out-Null |
| 66 | + New-Item -ItemType Directory -Force -Path $dropDirectory | Out-Null |
| 67 | +
|
| 68 | + $packageFiles = Get-ChildItem -Path $packagesDirectory -Filter *.nupkg |
| 69 | +
|
| 70 | + if ($packageFiles.Count -eq 0) { |
| 71 | + throw 'No NuGet packages were produced for SBOM generation.' |
| 72 | + } |
| 73 | +
|
| 74 | + foreach ($packageFile in $packageFiles) { |
| 75 | + Copy-Item -Path $packageFile.FullName -Destination $dropDirectory -Force |
| 76 | + } |
| 77 | +
|
| 78 | + $packageVersion = (dotnet msbuild ./Trellis.ServiceLevelIndicators/src/Trellis.ServiceLevelIndicators.csproj --getProperty:PackageVersion).Trim() |
| 79 | +
|
| 80 | + sbom-tool generate ` |
| 81 | + -b $dropDirectory ` |
| 82 | + -bc $PWD ` |
| 83 | + -pn 'Trellis.ServiceLevelIndicators' ` |
| 84 | + -pv $packageVersion ` |
| 85 | + -ps 'Xavier John' ` |
| 86 | + -nsb 'https://github.com/xavierjohn/ServiceLevelIndicators/sbom' ` |
| 87 | + -m $sbomDirectory ` |
| 88 | + -mi SPDX:3.0 ` |
| 89 | + -D true ` |
| 90 | + -V Information |
| 91 | +
|
| 92 | + Write-Host 'Generated SBOM manifests:' |
| 93 | + Get-ChildItem -Path $sbomDirectory -Recurse -Filter *.spdx.json | |
| 94 | + Select-Object FullName, Length | |
| 95 | + Format-Table -AutoSize |
| 96 | +
|
| 97 | + - name: List packages |
| 98 | + run: | |
| 99 | + echo "Packages to be published:" |
| 100 | + ls -lh nupkgs/*.nupkg |
| 101 | +
|
| 102 | + - name: Upload SBOMs |
| 103 | + uses: actions/upload-artifact@v6 |
| 104 | + with: |
| 105 | + name: sbom-spdx |
| 106 | + path: sbom/_manifest/** |
| 107 | + |
| 108 | + - name: Publish to NuGet |
| 109 | + if: ${{ inputs.dry_run == 'false' }} |
| 110 | + run: | |
| 111 | + dotnet nuget push nupkgs/*.nupkg \ |
| 112 | + --api-key ${{ secrets.NUGET_API_KEY }} \ |
| 113 | + --source https://api.nuget.org/v3/index.json \ |
| 114 | + --skip-duplicate |
| 115 | +
|
| 116 | + - name: Dry run summary |
| 117 | + if: ${{ inputs.dry_run == 'true' }} |
| 118 | + run: | |
| 119 | + echo "DRY RUN - Packages NOT published:" |
| 120 | + ls -lh nupkgs/*.nupkg |
| 121 | + echo "" |
| 122 | + echo "To publish for real, re-run with dry_run = false" |
0 commit comments