|
| 1 | +name: keep-build-cache-small |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + workflow_call: |
| 6 | + schedule: |
| 7 | + - cron: 0 6 * * * |
| 8 | + |
| 9 | +env: |
| 10 | + CACHE_THRESHOLD_GB: 6 |
| 11 | + BUILD_CACHE_IMAGE: ghcr.io/autowarefoundation/autoware-buildcache:amd64-main |
| 12 | + |
| 13 | +jobs: |
| 14 | + keep-build-cache-small: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + steps: |
| 17 | + - name: Install dependencies |
| 18 | + run: | |
| 19 | + sudo apt-get update |
| 20 | + sudo apt-get install -y skopeo jq bc |
| 21 | +
|
| 22 | + - name: Login to GitHub Container Registry |
| 23 | + uses: docker/login-action@v3 |
| 24 | + with: |
| 25 | + registry: ghcr.io |
| 26 | + username: ${{ github.repository_owner }} |
| 27 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 28 | + |
| 29 | + - name: Check if build cache image exists |
| 30 | + id: check_image |
| 31 | + run: | |
| 32 | + echo "🔍 Checking if build cache image exists..." |
| 33 | + if skopeo inspect --raw docker://${{ env.BUILD_CACHE_IMAGE }} > /dev/null 2>&1; then |
| 34 | + echo "✅ Build cache image found. Proceeding..." |
| 35 | + echo "exists=true" >> "$GITHUB_OUTPUT" |
| 36 | + else |
| 37 | + echo "⚠️ Build cache image does not exist. Skipping remaining steps." |
| 38 | + echo "exists=false" >> "$GITHUB_OUTPUT" |
| 39 | + fi |
| 40 | +
|
| 41 | + - name: Calculate cache size |
| 42 | + id: calc_size |
| 43 | + if: steps.check_image.outputs.exists == 'true' |
| 44 | + run: | |
| 45 | + echo "🔍 Inspecting build cache size for image: ${{ env.BUILD_CACHE_IMAGE }}..." |
| 46 | + SIZE_BYTES=$(skopeo inspect --raw docker://${{ env.BUILD_CACHE_IMAGE }} | jq '[.manifests[].size] | add') |
| 47 | + SIZE_GB=$(echo "scale=2; $SIZE_BYTES / (1024 * 1024 * 1024)" | bc) |
| 48 | + echo "Cache size: ${SIZE_GB} GB" |
| 49 | + echo "size_gb=${SIZE_GB}" >> "$GITHUB_OUTPUT" |
| 50 | +
|
| 51 | + - name: Compare cache size with threshold |
| 52 | + id: check_threshold |
| 53 | + if: steps.check_image.outputs.exists == 'true' |
| 54 | + run: | |
| 55 | + echo "📊 Checking cache size against threshold..." |
| 56 | + THRESHOLD_GB=${{ env.CACHE_THRESHOLD_GB }} |
| 57 | + CACHE_SIZE_GB=${{ steps.calc_size.outputs.size_gb }} |
| 58 | +
|
| 59 | + if (( $(echo "$CACHE_SIZE_GB > $THRESHOLD_GB" | bc -l) )); then |
| 60 | + echo "❌ Cache size exceeds threshold. Action needed." |
| 61 | + echo "status=exceeded" >> "$GITHUB_OUTPUT" |
| 62 | + else |
| 63 | + echo "✅ Cache size is within threshold. No action needed." |
| 64 | + echo "status=ok" >> "$GITHUB_OUTPUT" |
| 65 | + fi |
| 66 | +
|
| 67 | + - name: Delete build cache if exceeded |
| 68 | + if: steps.check_threshold.outputs.status == 'exceeded' |
| 69 | + run: | |
| 70 | + echo "🗑️ Deleting build cache image: ${{ env.BUILD_CACHE_IMAGE }}..." |
| 71 | + # skopeo delete docker://${{ env.BUILD_CACHE_IMAGE }} |
| 72 | +
|
| 73 | + - name: Verify cache deletion |
| 74 | + if: steps.check_threshold.outputs.status == 'exceeded' |
| 75 | + id: verify_deletion |
| 76 | + run: | |
| 77 | + echo "🔍 Verifying if build cache image has been deleted..." |
| 78 | +
|
| 79 | + if skopeo inspect --raw docker://${{ env.BUILD_CACHE_IMAGE }} > /dev/null 2>&1; then |
| 80 | + echo "❌ Build cache image still exists!" |
| 81 | + echo "status=failed" >> "$GITHUB_OUTPUT" |
| 82 | + exit 1 |
| 83 | + else |
| 84 | + echo "✅ Build cache image successfully deleted." |
| 85 | + echo "status=success" >> "$GITHUB_OUTPUT" |
| 86 | + fi |
| 87 | +
|
| 88 | + - name: Generate workflow summary |
| 89 | + if: always() |
| 90 | + run: | |
| 91 | + SUMMARY="# 📦 Build Cache Size Check\n" |
| 92 | + SUMMARY+="- **Image**: \`${{ env.BUILD_CACHE_IMAGE }}\`\n" |
| 93 | +
|
| 94 | + if [[ "${{ steps.check_image.outputs.exists }}" == "false" ]]; then |
| 95 | + SUMMARY+="⚠️ **Build cache image does not exist. Skipping checks.**\n" |
| 96 | + else |
| 97 | + SUMMARY+="- **Cache Size**: ${{ steps.calc_size.outputs.size_gb }} GB\n" |
| 98 | + SUMMARY+="- **Threshold**: ${{ env.CACHE_THRESHOLD_GB }} GB\n" |
| 99 | +
|
| 100 | + if [[ "${{ steps.check_threshold.outputs.status }}" == "exceeded" ]]; then |
| 101 | + if [[ "${{ steps.verify_deletion.outputs.status }}" == "failed" ]]; then |
| 102 | + SUMMARY+="❌ **Cache size exceeded, but deletion failed!**\n" |
| 103 | + else |
| 104 | + SUMMARY+="🗑️✅ **Cache size exceeded. Image deleted successfully!**\n" |
| 105 | + fi |
| 106 | + else |
| 107 | + SUMMARY+="👌✅ **Cache size is within acceptable limits.**\n" |
| 108 | + fi |
| 109 | + fi |
| 110 | +
|
| 111 | + echo -e "$SUMMARY" |
| 112 | + echo -e "$SUMMARY" >> $GITHUB_STEP_SUMMARY |
0 commit comments