From e34c926de992921dee30001736f240b5ed6eb0ff Mon Sep 17 00:00:00 2001 From: Nick Peacock Date: Wed, 2 Jul 2025 19:43:09 +0100 Subject: [PATCH 1/4] github action example for polling quota and sending alert for thresholds --- .../.github/workflows/quota-alert.yml | 51 +++++++++++++++++++ .../README.md | 32 ++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 github-actions-cloudsmith-quota-alert/.github/workflows/quota-alert.yml create mode 100644 github-actions-cloudsmith-quota-alert/README.md 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..4e0dab7 --- /dev/null +++ b/github-actions-cloudsmith-quota-alert/.github/workflows/quota-alert.yml @@ -0,0 +1,51 @@ +name: Check Cloudsmith Quota + +on: + schedule: + - cron: '0 * * * *' # every hour + workflow_dispatch: + +jobs: + check-quota: + runs-on: ubuntu-latest + + steps: + - name: Set up Cloudsmith CLI + uses: cloudsmith-io/cloudsmith-cli-action@v0.15.0 + with: + api-key: ${{ secrets.CLOUDSMITH_API_KEY }} + + - name: Get quota info + id: quota + run: | + OUTPUT=$(cloudsmith quotas list --format json) + echo "$OUTPUT" > quota.json + + USED_PERCENT=$(jq -r '.[0].quota_used_normalised' quota.json) + echo "Used percent: $USED_PERCENT" + echo "used_percent=$USED_PERCENT" >> $GITHUB_OUTPUT + + - name: Send Slack alert if threshold is hit + if: steps.quota.outputs.used_percent != '' + env: + SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} + run: | + USAGE=$(printf "%.0f" "${{ steps.quota.outputs.used_percent }}") + + send_slack() { + curl -X POST -H "Content-Type: application/json" \ + -d "{ + \"text\": \":warning: Cloudsmith quota usage is at ${USAGE}%.\nCheck usage or increase limits if needed.\" + }" \ + \"$SLACK_WEBHOOK_URL\" + } + + case "$USAGE" in + 50|75|90) + echo "Threshold hit: $USAGE%" + send_slack + ;; + *) + echo "Quota within normal range" + ;; + esac diff --git a/github-actions-cloudsmith-quota-alert/README.md b/github-actions-cloudsmith-quota-alert/README.md new file mode 100644 index 0000000..9caa76e --- /dev/null +++ b/github-actions-cloudsmith-quota-alert/README.md @@ -0,0 +1,32 @@ +## GitHub Action - Cloudsmith Quota Alert + +This workflow checks your Cloudsmith quota usage. + +It runs every hour or on manual trigger. + +If usage hits 50%, 75%, or 90%, it sends a Slack alert. + +--- + +### Setup + +1. Store your Cloudsmith API key as a GitHub secret: + - `CLOUDSMITH_API_KEY` + +2. Create a Slack webhook and store it as a secret: + - `SLACK_WEBHOOK_URL` + +--- + +### How it works + +- Uses the Cloudsmith CLI to get quota info +- Extracts the quota percentage from the JSON output +- Sends a Slack message if usage hits one of the thresholds + +--- + +### Files + +- `.github/workflows/quota-alert.yml` + The GitHub Action workflow From 174bf2a975a80263bcb7886ad151ffa31028de13 Mon Sep 17 00:00:00 2001 From: Nick Peacock Date: Wed, 2 Jul 2025 19:46:10 +0100 Subject: [PATCH 2/4] Update README.md --- github-actions-cloudsmith-quota-alert/README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/github-actions-cloudsmith-quota-alert/README.md b/github-actions-cloudsmith-quota-alert/README.md index 9caa76e..591b91b 100644 --- a/github-actions-cloudsmith-quota-alert/README.md +++ b/github-actions-cloudsmith-quota-alert/README.md @@ -29,4 +29,3 @@ If usage hits 50%, 75%, or 90%, it sends a Slack alert. ### Files - `.github/workflows/quota-alert.yml` - The GitHub Action workflow From 4d3e2e85ed2c1ac65d563697b3ebef1b8d75cf08 Mon Sep 17 00:00:00 2001 From: Nick Peacock Date: Wed, 2 Jul 2025 19:57:37 +0100 Subject: [PATCH 3/4] Update quota-alert.yml Switched to API rather than CLI --- .../.github/workflows/quota-alert.yml | 73 ++++++++++++------- 1 file changed, 46 insertions(+), 27 deletions(-) diff --git a/github-actions-cloudsmith-quota-alert/.github/workflows/quota-alert.yml b/github-actions-cloudsmith-quota-alert/.github/workflows/quota-alert.yml index 4e0dab7..8a111ab 100644 --- a/github-actions-cloudsmith-quota-alert/.github/workflows/quota-alert.yml +++ b/github-actions-cloudsmith-quota-alert/.github/workflows/quota-alert.yml @@ -1,4 +1,4 @@ -name: Check Cloudsmith Quota +name: Check Cloudsmith Quota (via API) on: schedule: @@ -10,42 +10,61 @@ jobs: runs-on: ubuntu-latest steps: - - name: Set up Cloudsmith CLI - uses: cloudsmith-io/cloudsmith-cli-action@v0.15.0 - with: - api-key: ${{ secrets.CLOUDSMITH_API_KEY }} - - - name: Get quota info + - 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: | - OUTPUT=$(cloudsmith quotas list --format json) - echo "$OUTPUT" > quota.json + 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) - USED_PERCENT=$(jq -r '.[0].quota_used_normalised' quota.json) - echo "Used percent: $USED_PERCENT" - echo "used_percent=$USED_PERCENT" >> $GITHUB_OUTPUT + 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) - - name: Send Slack alert if threshold is hit - if: steps.quota.outputs.used_percent != '' + 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: | - USAGE=$(printf "%.0f" "${{ steps.quota.outputs.used_percent }}") + alert() { + local type="$1" + local used="$2" + local limit="$3" + local percent="$4" - send_slack() { curl -X POST -H "Content-Type: application/json" \ -d "{ - \"text\": \":warning: Cloudsmith quota usage is at ${USAGE}%.\nCheck usage or increase limits if needed.\" + \"text\": \":warning: Cloudsmith $type usage is at ${percent}% (${used} of ${limit}).\nConsider taking action.\" }" \ - \"$SLACK_WEBHOOK_URL\" + "$SLACK_WEBHOOK_URL" } - case "$USAGE" in - 50|75|90) - echo "Threshold hit: $USAGE%" - send_slack - ;; - *) - echo "Quota within normal range" - ;; - esac + 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 From 165e029e33e1e8033d4f7a358e22fb4bd163ff01 Mon Sep 17 00:00:00 2001 From: Nick Peacock Date: Wed, 2 Jul 2025 19:59:25 +0100 Subject: [PATCH 4/4] Update README.md updated readme to align with api solution --- .../README.md | 35 +++++++++++++------ 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/github-actions-cloudsmith-quota-alert/README.md b/github-actions-cloudsmith-quota-alert/README.md index 591b91b..6152494 100644 --- a/github-actions-cloudsmith-quota-alert/README.md +++ b/github-actions-cloudsmith-quota-alert/README.md @@ -1,31 +1,44 @@ ## GitHub Action - Cloudsmith Quota Alert -This workflow checks your Cloudsmith quota usage. +This workflow checks Cloudsmith quota usage via the API. -It runs every hour or on manual trigger. +It sends separate alerts for: -If usage hits 50%, 75%, or 90%, it sends a Slack alert. +- Storage usage +- Bandwidth (downloaded) usage --- ### Setup -1. Store your Cloudsmith API key as a GitHub secret: - - `CLOUDSMITH_API_KEY` +1. Add your Cloudsmith API key as a secret: + `CLOUDSMITH_API_KEY` -2. Create a Slack webhook and store it as a secret: - - `SLACK_WEBHOOK_URL` +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 -- Uses the Cloudsmith CLI to get quota info -- Extracts the quota percentage from the JSON output -- Sends a Slack message if usage hits one of the thresholds +- 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` +- `.github/workflows/quota-alert.yml`