|
| 1 | +--- |
| 2 | +name: 🔄 Terraform Registry Webhook Sync |
| 3 | + |
| 4 | +# Audits the Terraform Registry webhook on every matching repo in one or more |
| 5 | +# orgs and ensures it fires on tag/branch creation+deletion (not just push), |
| 6 | +# so new module tags auto-sync to registry.terraform.io without a manual resync. |
| 7 | +# |
| 8 | +# Root cause this fixes: the registry installs a webhook subscribed to "push" |
| 9 | +# only. Tag ingestion keys off the "create" event, so new tags never reach the |
| 10 | +# registry and require a manual "Resync Module" click. Adding create+delete to |
| 11 | +# the hook makes sync automatic and self-healing for newly published repos. |
| 12 | + |
| 13 | +on: |
| 14 | + workflow_call: |
| 15 | + inputs: |
| 16 | + orgs: |
| 17 | + description: 'Comma-separated GitHub orgs to audit. e.g. "clouddrove,org2,org3"' |
| 18 | + required: true |
| 19 | + type: string |
| 20 | + repo_filter: |
| 21 | + description: 'Only repos whose name matches this regex are touched.' |
| 22 | + required: false |
| 23 | + type: string |
| 24 | + default: '^terraform-' |
| 25 | + required_events: |
| 26 | + description: 'Comma-separated webhook events the registry hook must have.' |
| 27 | + required: false |
| 28 | + type: string |
| 29 | + default: 'push,create,delete' |
| 30 | + dry_run: |
| 31 | + description: 'When true, report drift only; do not modify any webhook.' |
| 32 | + required: false |
| 33 | + type: boolean |
| 34 | + default: false |
| 35 | + secrets: |
| 36 | + GH_ADMIN_TOKEN: |
| 37 | + description: 'PAT with admin:repo_hook + repo (read) scope across the orgs.' |
| 38 | + required: true |
| 39 | + |
| 40 | + workflow_dispatch: |
| 41 | + inputs: |
| 42 | + orgs: |
| 43 | + description: 'Comma-separated GitHub orgs to audit.' |
| 44 | + required: true |
| 45 | + type: string |
| 46 | + repo_filter: |
| 47 | + description: 'Repo name regex filter.' |
| 48 | + required: false |
| 49 | + type: string |
| 50 | + default: '^terraform-' |
| 51 | + required_events: |
| 52 | + description: 'Webhook events the registry hook must have.' |
| 53 | + required: false |
| 54 | + type: string |
| 55 | + default: 'push,create,delete' |
| 56 | + dry_run: |
| 57 | + description: 'Report drift only; do not modify.' |
| 58 | + required: false |
| 59 | + type: boolean |
| 60 | + default: false |
| 61 | + |
| 62 | +permissions: |
| 63 | + contents: read |
| 64 | + |
| 65 | +jobs: |
| 66 | + sync-webhooks: |
| 67 | + name: 🔍 Audit & fix registry webhooks |
| 68 | + runs-on: ubuntu-latest |
| 69 | + env: |
| 70 | + GH_TOKEN: ${{ secrets.GH_ADMIN_TOKEN || github.token }} |
| 71 | + ORGS: ${{ inputs.orgs }} |
| 72 | + REPO_FILTER: ${{ inputs.repo_filter }} |
| 73 | + REQUIRED_EVENTS: ${{ inputs.required_events }} |
| 74 | + DRY_RUN: ${{ inputs.dry_run }} |
| 75 | + steps: |
| 76 | + - name: 🔄 Reconcile webhooks |
| 77 | + shell: bash |
| 78 | + run: | |
| 79 | + set -euo pipefail |
| 80 | +
|
| 81 | + # jq array of events the registry hook must contain. |
| 82 | + mapfile -t WANT < <(echo "$REQUIRED_EVENTS" | tr ',' '\n' | sed 's/ //g' | grep -v '^$') |
| 83 | + WANT_JSON=$(printf '%s\n' "${WANT[@]}" | jq -R . | jq -sc .) |
| 84 | + echo "Required events: $WANT_JSON | dry_run=$DRY_RUN" |
| 85 | +
|
| 86 | + changed=0; ok=0; missing=0; total=0 |
| 87 | + summary="" |
| 88 | +
|
| 89 | + IFS=',' read -ra ORG_LIST <<< "$ORGS" |
| 90 | + for org in "${ORG_LIST[@]}"; do |
| 91 | + org=$(echo "$org" | xargs) # trim |
| 92 | + [ -z "$org" ] && continue |
| 93 | + echo "::group::Org $org" |
| 94 | +
|
| 95 | + # All repos in the org matching the filter (paginated). |
| 96 | + repos=$(gh api "orgs/$org/repos" --paginate \ |
| 97 | + --jq ".[] | select(.name|test(\"$REPO_FILTER\")) | .name") |
| 98 | +
|
| 99 | + for repo in $repos; do |
| 100 | + total=$((total+1)) |
| 101 | + # Find the registry's webhook on this repo, if any. |
| 102 | + hook=$(gh api "repos/$org/$repo/hooks" \ |
| 103 | + --jq '.[] | select(.config.url // "" | test("registry.terraform.io")) | {id, events}' \ |
| 104 | + 2>/dev/null | jq -sc '.[0] // empty') || hook="" |
| 105 | +
|
| 106 | + if [ -z "$hook" ]; then |
| 107 | + missing=$((missing+1)) |
| 108 | + summary+="⚠️ $org/$repo: no registry webhook (publish module on registry first)\n" |
| 109 | + continue |
| 110 | + fi |
| 111 | +
|
| 112 | + id=$(echo "$hook" | jq -r '.id') |
| 113 | + have=$(echo "$hook" | jq -c '.events') |
| 114 | + # Drift = any required event not already present. |
| 115 | + drift=$(jq -nc --argjson have "$have" --argjson want "$WANT_JSON" \ |
| 116 | + '$want - $have') |
| 117 | +
|
| 118 | + if [ "$drift" = "[]" ]; then |
| 119 | + ok=$((ok+1)) |
| 120 | + continue |
| 121 | + fi |
| 122 | +
|
| 123 | + if [ "$DRY_RUN" = "true" ]; then |
| 124 | + summary+="🔸 $org/$repo: hook $id missing $drift (would set $WANT_JSON)\n" |
| 125 | + changed=$((changed+1)) |
| 126 | + continue |
| 127 | + fi |
| 128 | +
|
| 129 | + # PATCH: set the hook's events to the full required set. |
| 130 | + args=(); for e in "${WANT[@]}"; do args+=(-f "events[]=$e"); done |
| 131 | + gh api -X PATCH "repos/$org/$repo/hooks/$id" "${args[@]}" >/dev/null |
| 132 | + summary+="✅ $org/$repo: hook $id updated -> $WANT_JSON (was missing $drift)\n" |
| 133 | + changed=$((changed+1)) |
| 134 | + done |
| 135 | + echo "::endgroup::" |
| 136 | + done |
| 137 | +
|
| 138 | + echo "## Registry webhook sync" >> "$GITHUB_STEP_SUMMARY" |
| 139 | + echo "" >> "$GITHUB_STEP_SUMMARY" |
| 140 | + echo "- Repos scanned: $total" >> "$GITHUB_STEP_SUMMARY" |
| 141 | + echo "- Already correct: $ok" >> "$GITHUB_STEP_SUMMARY" |
| 142 | + echo "- Changed${DRY_RUN:+ (dry-run)}: $changed" >> "$GITHUB_STEP_SUMMARY" |
| 143 | + echo "- No registry hook: $missing" >> "$GITHUB_STEP_SUMMARY" |
| 144 | + echo "" >> "$GITHUB_STEP_SUMMARY" |
| 145 | + echo -e "$summary" >> "$GITHUB_STEP_SUMMARY" |
| 146 | + echo -e "$summary" |
0 commit comments