Skip to content

Commit 9394f7e

Browse files
authored
chore: cleanup old github prerelease pacakge versions (#120)
### 🗒️ Description remove old github prerelease nuget pacakges ### ✅ Checklist <!--- Please include the following in your Pull Request when applicable --> - [ ] Linked to any related issues in the PR description - [ ] Added or updated documentation - [ ] Tests for new functionality and regression tests for bug fixes - [ ] Breaking changes were made
1 parent db17f38 commit 9394f7e

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

.github/workflows/_dotnet-build-test-pack-publish-nuget.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ on:
2222
required: false
2323
type: string
2424
default: 9.0.x
25+
num-github-prerelease-packages-to-keep:
26+
description: "We cleanup github prerelease nuget packages. Configure how many to keep"
27+
type: number
28+
required: false
29+
default: 50
2530
codecov-slug:
2631
description: "Slug to upload code coverage results for Codecov. e.g. saan800/saansoft-correlationId"
2732
required: false
@@ -109,6 +114,7 @@ jobs:
109114
os: ${{ inputs.os }}
110115
dotnet-version: ${{ inputs.dotnet-version }}
111116
package-artifact-name: "packages"
117+
num-github-prerelease-packages-to-keep: ${{ inputs.num-github-prerelease-packages-to-keep }}
112118
upload-to-github: ${{ inputs.is-release-branch == false && needs.check-release-eligibility.outputs.should-release == 'false' }}
113119
upload-to-nuget: ${{ needs.check-release-eligibility.outputs.should-release == 'true' }}
114120
secrets:

.github/workflows/_dotnet-publish-nuget.yml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ on:
2121
required: false
2222
type: string
2323
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
2429
upload-to-github:
2530
description: "Upload pacakge to github package registry"
2631
type: boolean
@@ -41,6 +46,7 @@ on:
4146

4247
permissions:
4348
contents: read
49+
packages: write
4450

4551
jobs:
4652
publish-nuget-package:
@@ -101,3 +107,63 @@ jobs:
101107
dotnet nuget push build-packages/*.nupkg
102108
--source https://api.nuget.org/v3/index.json
103109
--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

Comments
 (0)