|
21 | 21 | required: false
|
22 | 22 | type: string
|
23 | 23 | default: "packages"
|
| 24 | + num-github-prerelease-packages-to-keep: |
| 25 | + description: "We cleanup github prerelease nuget packages. Configure how many to keep" |
| 26 | + type: number |
| 27 | + required: false |
| 28 | + default: 50 |
24 | 29 | upload-to-github:
|
25 | 30 | description: "Upload pacakge to github package registry"
|
26 | 31 | type: boolean
|
|
41 | 46 |
|
42 | 47 | permissions:
|
43 | 48 | contents: read
|
| 49 | + packages: write |
44 | 50 |
|
45 | 51 | jobs:
|
46 | 52 | publish-nuget-package:
|
@@ -101,3 +107,63 @@ jobs:
|
101 | 107 | dotnet nuget push build-packages/*.nupkg
|
102 | 108 | --source https://api.nuget.org/v3/index.json
|
103 | 109 | --api-key ${{ secrets.NUGET_API_KEY }}
|
| 110 | +
|
| 111 | + discover-packages: |
| 112 | + runs-on: ubuntu-latest |
| 113 | + outputs: |
| 114 | + matrix: ${{ steps.set-matrix.outputs.matrix }} |
| 115 | + steps: |
| 116 | + - name: Harden Runner |
| 117 | + uses: step-security/harden-runner@f4a75cfd619ee5ce8d5b864b0d183aff3c69b55a # v2.13.1 |
| 118 | + with: |
| 119 | + disable-sudo: true |
| 120 | + egress-policy: block |
| 121 | + allowed-endpoints: > |
| 122 | + nuget.pkg.github.com:443 |
| 123 | +
|
| 124 | + # Download the artifact generated in the build job |
| 125 | + - name: Download artifact |
| 126 | + uses: actions/download-artifact@634f93cb2916e3fdff6788551b99b062d0335ce0 # 5.0.0 |
| 127 | + with: |
| 128 | + name: ${{ inputs.package-artifact-name }} |
| 129 | + path: ${{ inputs.working-directory }}/build-packages # Path to download the artifact |
| 130 | + |
| 131 | + - name: Find package IDs from .nupkg files |
| 132 | + id: set-matrix |
| 133 | + working-directory: ${{ inputs.working-directory }} |
| 134 | + run: | |
| 135 | + # Find all .nupkg files in build-packages |
| 136 | + nupkgs=$(find build-packages -type f -name '*.nupkg') |
| 137 | +
|
| 138 | + if [ -z "$nupkgs" ]; then |
| 139 | + echo "No .nupkg files found in build-packages/" |
| 140 | + echo "matrix={\"package\":[]}" >> $GITHUB_OUTPUT |
| 141 | + exit 0 |
| 142 | + fi |
| 143 | +
|
| 144 | + # Extract package IDs by removing version/prerelease suffixes |
| 145 | + packages=$(for f in $nupkgs; do |
| 146 | + name=$(basename "$f" .nupkg) |
| 147 | + # Strip ".<digits>..." version suffix, optionally with -prerelease |
| 148 | + echo "$name" | sed -E 's/\.[0-9]+(\.[0-9]+)*(-[A-Za-z0-9.]+)?$//' |
| 149 | + done | sort -u) |
| 150 | +
|
| 151 | + # Convert to compact JSON array for matrix |
| 152 | + json=$(printf '%s\n' $packages | jq -R . | jq -cs '{package: .}') |
| 153 | +
|
| 154 | + echo "matrix=$json" >> $GITHUB_OUTPUT |
| 155 | + echo "$json" |
| 156 | +
|
| 157 | + cleanup: |
| 158 | + needs: discover-packages |
| 159 | + runs-on: ubuntu-latest |
| 160 | + strategy: |
| 161 | + matrix: ${{ fromJson(needs.discover-packages.outputs.matrix) }} |
| 162 | + steps: |
| 163 | + - name: Delete old versions for ${{ matrix.package }} |
| 164 | + uses: actions/delete-package-versions@v5 |
| 165 | + with: |
| 166 | + package-name: ${{ matrix.package }} |
| 167 | + package-type: nuget |
| 168 | + min-versions-to-keep: ${{ inputs.num-github-prerelease-packages-to-keep }} |
| 169 | + delete-only-pre-release-versions: true |
0 commit comments