|
12 | 12 | - '**.md' |
13 | 13 | - '.gitignore' |
14 | 14 |
|
| 15 | +# Add permissions needed for creating and managing releases |
| 16 | +permissions: |
| 17 | + contents: write |
| 18 | + packages: read |
| 19 | + issues: read |
| 20 | + pull-requests: read |
| 21 | + |
15 | 22 | env: |
16 | 23 | DOCKER_BUILDKIT: 1 |
17 | 24 | ISO_FILENAME: Arch.iso |
@@ -175,12 +182,112 @@ jobs: |
175 | 182 | files: | |
176 | 183 | ${{ env.WORKSPACE }}/out/*.iso |
177 | 184 | ${{ env.WORKSPACE }}/out/*.sha*sum |
| 185 | + |
| 186 | + |
| 187 | + - name: Set up GitHub CLI |
| 188 | + run: | |
| 189 | + type -p curl >/dev/null || (sudo apt update && sudo apt install curl -y) |
| 190 | + curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg \ |
| 191 | + && sudo chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg \ |
| 192 | + && echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null \ |
| 193 | + && sudo apt update \ |
| 194 | + && sudo apt install gh -y |
178 | 195 |
|
179 | | - - name: Clean Up |
180 | | - if: always() |
| 196 | + - name: Delete old releases |
| 197 | + # This step runs after the GitHub CLI setup and release creation |
| 198 | + if: github.ref == 'refs/heads/main' && success() |
| 199 | + env: |
| 200 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
181 | 201 | run: | |
182 | | - if docker ps -a | grep -q arch-container; then |
183 | | - docker stop arch-container || true |
184 | | - docker rm -f arch-container || true |
| 202 | + set +e # Don't exit on error |
| 203 | + |
| 204 | + echo "::group::Release Cleanup" |
| 205 | + |
| 206 | + # Get current release tag we're creating |
| 207 | + current_tag="v${{ env.VERSION }}" |
| 208 | + echo "ℹ️ Current release tag: $current_tag" |
| 209 | + |
| 210 | + # Explicitly authenticate with GitHub CLI using the token |
| 211 | + echo "$GITHUB_TOKEN" | gh auth login --with-token |
| 212 | + |
| 213 | + if [ $? -ne 0 ]; then |
| 214 | + echo "::warning::Failed to authenticate with GitHub CLI. Skipping release cleanup." |
| 215 | + exit 0 # Continue workflow |
185 | 216 | fi |
186 | | - sudo rm -rf workdir/ out/*.iso out/*.sha*sum |
| 217 | + |
| 218 | + # List all releases |
| 219 | + echo "ℹ️ Listing existing releases..." |
| 220 | + |
| 221 | + # Use simple list format first to check if releases exist |
| 222 | + release_count=$(gh release list | wc -l) |
| 223 | + |
| 224 | + if [ $release_count -eq 0 ]; then |
| 225 | + echo "ℹ️ No existing releases found. Nothing to clean up." |
| 226 | + echo "::endgroup::" |
| 227 | + exit 0 |
| 228 | + fi |
| 229 | + |
| 230 | + echo "ℹ️ Found $release_count releases in total" |
| 231 | + |
| 232 | + # Get detailed release info with JSON |
| 233 | + releases=$(gh release list --limit 100 --json tagName,createdAt 2>/dev/null) |
| 234 | + |
| 235 | + if [ $? -ne 0 ] || [ -z "$releases" ]; then |
| 236 | + echo "::warning::Unable to get detailed release information. Skipping cleanup." |
| 237 | + echo "::endgroup::" |
| 238 | + exit 0 |
| 239 | + fi |
| 240 | + |
| 241 | + # Check if jq command is available |
| 242 | + if ! command -v jq &> /dev/null; then |
| 243 | + echo "::warning::jq command not found. Installing jq..." |
| 244 | + sudo apt-get update && sudo apt-get install -y jq |
| 245 | + fi |
| 246 | + |
| 247 | + # Parse releases, handling potential JSON parsing errors |
| 248 | + if ! old_releases=($(echo "$releases" | jq -r 'sort_by(.createdAt) | .[].tagName' 2>/dev/null)); then |
| 249 | + echo "::warning::Failed to parse release information. Skipping cleanup." |
| 250 | + echo "::endgroup::" |
| 251 | + exit 0 |
| 252 | + fi |
| 253 | + |
| 254 | + # Number of releases to keep (0 means delete all old releases) |
| 255 | + keep=0 |
| 256 | + count=0 |
| 257 | + total=${#old_releases[@]} |
| 258 | + |
| 259 | + echo "ℹ️ Found $total releases after parsing" |
| 260 | + |
| 261 | + # Delete all releases except the most recent 'keep' number and current release |
| 262 | + for tag in "${old_releases[@]}"; do |
| 263 | + # Skip the current release |
| 264 | + if [[ "$tag" == "$current_tag" ]]; then |
| 265 | + echo "ℹ️ Skipping current release: $tag" |
| 266 | + continue |
| 267 | + fi |
| 268 | + |
| 269 | + ((count++)) |
| 270 | + if ((count > keep)); then |
| 271 | + echo "🗑️ Attempting to delete release: $tag" |
| 272 | + |
| 273 | + # Try to delete the release with a timeout |
| 274 | + if timeout 30s gh release delete "$tag" --yes; then |
| 275 | + echo "✅ Successfully deleted release: $tag" |
| 276 | + else |
| 277 | + deletion_status=$? |
| 278 | + echo "::warning::Failed to delete release $tag (exit code: $deletion_status) - continuing with next release" |
| 279 | + fi |
| 280 | + |
| 281 | + # Small delay to avoid rate limiting |
| 282 | + sleep 1 |
| 283 | + else |
| 284 | + echo "🔒 Keeping release: $tag (within keep limit)" |
| 285 | + fi |
| 286 | + done |
| 287 | + |
| 288 | + echo "🏁 Release cleanup completed" |
| 289 | + echo "::endgroup::" |
| 290 | + |
| 291 | + # Always return success to avoid workflow failures |
| 292 | + exit 0 |
| 293 | +
|
0 commit comments