Skip to content

Commit cfa0bc1

Browse files
committed
Add initial GitHub Actions workflows
1 parent 5f0c1ba commit cfa0bc1

File tree

3 files changed

+414
-0
lines changed

3 files changed

+414
-0
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
name: Cache Cleanup
2+
3+
on:
4+
schedule:
5+
# Run every Sunday at 2 AM UTC
6+
- cron: '0 2 * * 0'
7+
workflow_dispatch:
8+
inputs:
9+
max_age_days:
10+
description: 'Maximum age of caches to keep (in days)'
11+
required: false
12+
default: '30'
13+
type: string
14+
15+
jobs:
16+
cleanup:
17+
name: Clean up old caches
18+
runs-on: ubuntu-latest
19+
permissions:
20+
actions: write
21+
contents: read
22+
23+
steps:
24+
- name: Checkout code
25+
uses: actions/checkout@v4
26+
27+
- name: Get cache cleanup parameters
28+
id: params
29+
run: |
30+
MAX_AGE_DAYS="${{ github.event.inputs.max_age_days || '30' }}"
31+
echo "max_age_days=${MAX_AGE_DAYS}" >> $GITHUB_OUTPUT
32+
echo "cleanup_date=$(date -d "${MAX_AGE_DAYS} days ago" '+%Y-%m-%dT%H:%M:%SZ')" >> $GITHUB_OUTPUT
33+
34+
- name: List current caches
35+
run: |
36+
echo "Current caches in repository:"
37+
gh api \
38+
-H "Accept: application/vnd.github+json" \
39+
-H "X-GitHub-Api-Version: 2022-11-28" \
40+
"/repos/${{ github.repository }}/actions/caches" \
41+
--jq '.actions_caches[] | "\(.key) - \(.size_in_bytes) bytes - \(.created_at)"'
42+
env:
43+
GH_TOKEN: ${{ github.token }}
44+
45+
- name: Clean up old caches
46+
run: |
47+
echo "Cleaning up caches older than ${{ steps.params.outputs.cleanup_date }}"
48+
49+
# Get list of cache IDs older than specified date
50+
CACHE_IDS=$(gh api \
51+
-H "Accept: application/vnd.github+json" \
52+
-H "X-GitHub-Api-Version: 2022-11-28" \
53+
"/repos/${{ github.repository }}/actions/caches" \
54+
--jq --arg cleanup_date "${{ steps.params.outputs.cleanup_date }}" \
55+
'.actions_caches[] | select(.created_at < $cleanup_date) | .id')
56+
57+
if [ -z "$CACHE_IDS" ]; then
58+
echo "No old caches found to clean up"
59+
exit 0
60+
fi
61+
62+
echo "Found caches to delete:"
63+
echo "$CACHE_IDS"
64+
65+
# Delete each cache
66+
for cache_id in $CACHE_IDS; do
67+
echo "Deleting cache ID: $cache_id"
68+
gh api \
69+
-X DELETE \
70+
-H "Accept: application/vnd.github+json" \
71+
-H "X-GitHub-Api-Version: 2022-11-28" \
72+
"/repos/${{ github.repository }}/actions/caches/$cache_id" || true
73+
done
74+
env:
75+
GH_TOKEN: ${{ github.token }}
76+
77+
- name: Clean up caches from deleted branches
78+
run: |
79+
echo "Cleaning up caches from non-existent branches"
80+
81+
# Get all remote branches
82+
git fetch --all
83+
EXISTING_BRANCHES=$(git branch -r | sed 's/origin\///' | grep -v HEAD | tr -d ' ')
84+
85+
# Get all cache keys
86+
CACHE_KEYS=$(gh api \
87+
-H "Accept: application/vnd.github+json" \
88+
-H "X-GitHub-Api-Version: 2022-11-28" \
89+
"/repos/${{ github.repository }}/actions/caches" \
90+
--jq '.actions_caches[] | .key')
91+
92+
# Find caches that might be from deleted branches
93+
for cache_key in $CACHE_KEYS; do
94+
# Extract potential branch name from cache key (this is heuristic)
95+
# Cache keys often contain branch names or refs
96+
if [[ "$cache_key" == *"refs/heads/"* ]]; then
97+
BRANCH_IN_KEY=$(echo "$cache_key" | sed -n 's/.*refs\/heads\/\([^-]*\).*/\1/p')
98+
if [ -n "$BRANCH_IN_KEY" ] && ! echo "$EXISTING_BRANCHES" | grep -q "^$BRANCH_IN_KEY$"; then
99+
echo "Found cache from potentially deleted branch: $cache_key (branch: $BRANCH_IN_KEY)"
100+
101+
# Get cache ID and delete
102+
CACHE_ID=$(gh api \
103+
-H "Accept: application/vnd.github+json" \
104+
-H "X-GitHub-Api-Version: 2022-11-28" \
105+
"/repos/${{ github.repository }}/actions/caches" \
106+
--jq --arg key "$cache_key" \
107+
'.actions_caches[] | select(.key == $key) | .id')
108+
109+
if [ -n "$CACHE_ID" ]; then
110+
echo "Deleting cache ID: $CACHE_ID"
111+
gh api \
112+
-X DELETE \
113+
-H "Accept: application/vnd.github+json" \
114+
-H "X-GitHub-Api-Version: 2022-11-28" \
115+
"/repos/${{ github.repository }}/actions/caches/$CACHE_ID" || true
116+
fi
117+
fi
118+
fi
119+
done
120+
env:
121+
GH_TOKEN: ${{ github.token }}
122+
123+
- name: Show cache statistics after cleanup
124+
run: |
125+
echo "Cache statistics after cleanup:"
126+
CACHE_INFO=$(gh api \
127+
-H "Accept: application/vnd.github+json" \
128+
-H "X-GitHub-Api-Version: 2022-11-28" \
129+
"/repos/${{ github.repository }}/actions/caches")
130+
131+
CACHE_COUNT=$(echo "$CACHE_INFO" | jq '.actions_caches | length')
132+
TOTAL_SIZE=$(echo "$CACHE_INFO" | jq '.actions_caches | map(.size_in_bytes) | add // 0')
133+
TOTAL_SIZE_MB=$((TOTAL_SIZE / 1024 / 1024))
134+
135+
echo "Total caches: $CACHE_COUNT"
136+
echo "Total size: ${TOTAL_SIZE_MB} MB"
137+
echo "Remaining caches:"
138+
echo "$CACHE_INFO" | jq '.actions_caches[] | "\(.key) - \(.size_in_bytes) bytes - \(.created_at)"'
139+
env:
140+
GH_TOKEN: ${{ github.token }}

.github/workflows/ci.yml

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [main, develop]
6+
pull_request:
7+
branches: [main, develop]
8+
9+
concurrency:
10+
group: ${{ github.workflow }}-${{ github.ref }}
11+
cancel-in-progress: true
12+
13+
jobs:
14+
test:
15+
name: Test on ${{ matrix.os }}
16+
runs-on: ${{ matrix.os }}
17+
strategy:
18+
fail-fast: false
19+
matrix:
20+
os: [macos-15]
21+
include:
22+
- os: macos-15
23+
xcode: "16.4"
24+
25+
steps:
26+
- name: Checkout code
27+
uses: actions/checkout@v4
28+
29+
- name: Select Xcode version
30+
run: sudo xcode-select -s /Applications/Xcode_${{ matrix.xcode }}.app/Contents/Developer
31+
32+
- name: Show Swift version
33+
run: swift --version
34+
35+
- name: Cache Swift Package Manager
36+
uses: actions/cache@v4
37+
with:
38+
path: |
39+
.build
40+
~/.cache/org.swift.swiftpm
41+
key: ${{ runner.os }}-spm-${{ hashFiles('Package.swift', 'Package.resolved') }}
42+
restore-keys: |
43+
${{ runner.os }}-spm-
44+
45+
- name: Resolve dependencies
46+
run: swift package resolve
47+
48+
- name: Build debug
49+
run: swift build --configuration debug
50+
51+
- name: Build release
52+
run: swift build --configuration release
53+
54+
- name: Run tests
55+
run: swift test --configuration debug --enable-code-coverage
56+
57+
- name: Generate code coverage report
58+
run: |
59+
xcrun llvm-cov export -format="lcov" \
60+
.build/debug/SwiftDependencyAuditPackageTests.xctest/Contents/MacOS/SwiftDependencyAuditPackageTests \
61+
-instr-profile=.build/debug/codecov/default.profdata > coverage.lcov
62+
continue-on-error: true
63+
64+
- name: Upload coverage to Codecov
65+
uses: codecov/codecov-action@v4
66+
with:
67+
file: coverage.lcov
68+
fail_ci_if_error: false
69+
env:
70+
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
71+
72+
- name: Test CLI functionality
73+
run: |
74+
swift run swift-dependency-audit --help
75+
swift run swift-dependency-audit . --verbose
76+
77+
- name: Upload build artifacts
78+
uses: actions/upload-artifact@v4
79+
with:
80+
name: build-artifacts-${{ matrix.os }}
81+
path: |
82+
.build/release/swift-dependency-audit
83+
retention-days: 7
84+
85+
lint:
86+
name: Lint and Format Check
87+
runs-on: macos-15
88+
steps:
89+
- name: Checkout code
90+
uses: actions/checkout@v4
91+
92+
- name: Select Xcode version
93+
run: sudo xcode-select -s /Applications/Xcode_16.4.app/Contents/Developer
94+
95+
- name: Cache Swift Package Manager
96+
uses: actions/cache@v4
97+
with:
98+
path: |
99+
.build
100+
~/.cache/org.swift.swiftpm
101+
key: ${{ runner.os }}-spm-lint-${{ hashFiles('Package.swift', 'Package.resolved') }}
102+
restore-keys: |
103+
${{ runner.os }}-spm-lint-
104+
${{ runner.os }}-spm-
105+
106+
- name: Check Swift format
107+
run: |
108+
# Install swift-format if available
109+
if command -v swift-format &> /dev/null; then
110+
swift-format --version
111+
find Sources Tests -name "*.swift" | xargs swift-format --mode diff
112+
else
113+
echo "swift-format not available, skipping format check"
114+
fi
115+
continue-on-error: true
116+
117+
- name: Swift build warnings check
118+
run: |
119+
swift build --configuration release 2>&1 | tee build.log
120+
if grep -q "warning:" build.log; then
121+
echo "⚠️ Build warnings found:"
122+
grep "warning:" build.log
123+
exit 1
124+
fi

0 commit comments

Comments
 (0)