diff --git a/github-actions-cloudsmith-quota-alert/.github/workflows/quota-alert.yml b/github-actions-cloudsmith-quota-alert/.github/workflows/quota-alert.yml new file mode 100644 index 0000000..8a111ab --- /dev/null +++ b/github-actions-cloudsmith-quota-alert/.github/workflows/quota-alert.yml @@ -0,0 +1,70 @@ +name: Check Cloudsmith Quota (via API) + +on: + schedule: + - cron: '0 * * * *' # every hour + workflow_dispatch: + +jobs: + check-quota: + runs-on: ubuntu-latest + + steps: + - name: Get quota info via API + id: quota + env: + OWNER: your-org-name # <-- replace with your Cloudsmith org + API_KEY: ${{ secrets.CLOUDSMITH_API_KEY }} + run: | + RESPONSE=$(curl -s -H "Authorization: Bearer $API_KEY" \ + "https://api.cloudsmith.io/v1/quota/history/$OWNER/") + + echo "$RESPONSE" > quota.json + + STORAGE_USED=$(jq -r '.history[0].display.storage_used.used' quota.json) + STORAGE_LIMIT=$(jq -r '.history[0].display.storage_used.limit' quota.json) + STORAGE_PERCENT=$(jq -r '.history[0].raw.storage_used.percentage' quota.json) + + DOWNLOAD_USED=$(jq -r '.history[0].display.downloaded.used' quota.json) + DOWNLOAD_LIMIT=$(jq -r '.history[0].display.downloaded.limit' quota.json) + DOWNLOAD_PERCENT=$(jq -r '.history[0].raw.downloaded.percentage' quota.json) + + echo "storage_percent=$STORAGE_PERCENT" >> $GITHUB_OUTPUT + echo "download_percent=$DOWNLOAD_PERCENT" >> $GITHUB_OUTPUT + + echo "STORAGE_USED=$STORAGE_USED" >> $GITHUB_ENV + echo "STORAGE_LIMIT=$STORAGE_LIMIT" >> $GITHUB_ENV + echo "DOWNLOAD_USED=$DOWNLOAD_USED" >> $GITHUB_ENV + echo "DOWNLOAD_LIMIT=$DOWNLOAD_LIMIT" >> $GITHUB_ENV + + - name: Send Slack alert if thresholds are hit + if: | + steps.quota.outputs.storage_percent != '' || steps.quota.outputs.download_percent != '' + env: + SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} + run: | + alert() { + local type="$1" + local used="$2" + local limit="$3" + local percent="$4" + + curl -X POST -H "Content-Type: application/json" \ + -d "{ + \"text\": \":warning: Cloudsmith $type usage is at ${percent}% (${used} of ${limit}).\nConsider taking action.\" + }" \ + "$SLACK_WEBHOOK_URL" + } + + STORAGE=$(printf "%.0f" "${{ steps.quota.outputs.storage_percent }}") + DOWNLOAD=$(printf "%.0f" "${{ steps.quota.outputs.download_percent }}") + + for THRESHOLD in 50 75 90; do + if [ "$STORAGE" -eq "$THRESHOLD" ]; then + alert "storage" "$STORAGE_USED" "$STORAGE_LIMIT" "$STORAGE" + fi + + if [ "$DOWNLOAD" -eq "$THRESHOLD" ]; then + alert "bandwidth" "$DOWNLOAD_USED" "$DOWNLOAD_LIMIT" "$DOWNLOAD" + fi + done diff --git a/github-actions-cloudsmith-quota-alert/README.md b/github-actions-cloudsmith-quota-alert/README.md new file mode 100644 index 0000000..6152494 --- /dev/null +++ b/github-actions-cloudsmith-quota-alert/README.md @@ -0,0 +1,44 @@ +## GitHub Action - Cloudsmith Quota Alert + +This workflow checks Cloudsmith quota usage via the API. + +It sends separate alerts for: + +- Storage usage +- Bandwidth (downloaded) usage + +--- + +### Setup + +1. Add your Cloudsmith API key as a secret: + `CLOUDSMITH_API_KEY` + +2. Add your Slack webhook URL as a secret: + `SLACK_WEBHOOK_URL` + +3. In the workflow file, replace `your-org-name` with your Cloudsmith org name. + +--- + +### How it works + +- Runs hourly or on manual trigger +- Calls the quota history API +- Parses the latest storage and bandwidth usage +- Compares against 50%, 75%, and 90% thresholds +- Sends Slack alerts using display values (e.g. "62.6 GB of 1000 GB") + +--- + +### Example Alert + +> ⚠️ Cloudsmith **storage** usage is at 75% (750 GB of 1000 GB) + +> ⚠️ Cloudsmith **bandwidth** usage is at 90% (17.6 TB of 19.5 TB) + +--- + +### Files + +- `.github/workflows/quota-alert.yml`