Skip to content

Commit 146dd3f

Browse files
Merge pull request #106 from Githubguy132010/dev
This pull request includes significant changes to the GitHub Actions workflow, primarily focusing on enhancing the release management process by integrating release cleanup directly into the build workflow and removing the separate cleanup workflow. The most important changes are detailed below: Enhancements to release management: * [`.github/workflows/build.yaml`](diffhunk://#diff-d0777657fa3fd81d23aaf7273e58aee453b04e67882517900c56daeef9b3e4c1R15-R21): Added permissions for creating and managing releases, set up the GitHub CLI, and implemented a step to delete old releases after the release creation. [[1]](diffhunk://#diff-d0777657fa3fd81d23aaf7273e58aee453b04e67882517900c56daeef9b3e4c1R15-R21) [[2]](diffhunk://#diff-d0777657fa3fd81d23aaf7273e58aee453b04e67882517900c56daeef9b3e4c1L179-R293) Removal of redundant workflow: * [`.github/workflows/cleanup-releases.yml`](diffhunk://#diff-e954dcb033c455af4eb077e1c984f17b9d1b6e98b4ddc3dd50d366d259cbb837L1-L41): Deleted the separate workflow file for cleaning up old releases, as this functionality is now integrated into the main build workflow.
2 parents 6a23a63 + 47521d7 commit 146dd3f

File tree

2 files changed

+113
-47
lines changed

2 files changed

+113
-47
lines changed

.github/workflows/build.yaml

Lines changed: 113 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ on:
1212
- '**.md'
1313
- '.gitignore'
1414

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+
1522
env:
1623
DOCKER_BUILDKIT: 1
1724
ISO_FILENAME: Arch.iso
@@ -175,12 +182,112 @@ jobs:
175182
files: |
176183
${{ env.WORKSPACE }}/out/*.iso
177184
${{ 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
178195
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 }}
181201
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
185216
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+

.github/workflows/cleanup-releases.yml

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)