Skip to content

Commit 9d5e497

Browse files
committed
feat: add dry-run mode for nightly TFC report
Dry run fetches 1 workspace, runs full pipeline (including Claude analysis), but skips Slack posting and uploads artifacts instead.
1 parent 2442132 commit 9d5e497

File tree

1 file changed

+40
-10
lines changed

1 file changed

+40
-10
lines changed

.github/workflows/nightly-tfc-report.yml

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ name: Nightly TFC Status Report
33
on:
44
schedule:
55
- cron: "0 5 * * 1-5" # Midnight ET (UTC-5) weekdays
6-
workflow_dispatch: {}
6+
workflow_dispatch:
7+
inputs:
8+
dry_run:
9+
description: "Run full pipeline on 1 workspace but skip Slack posting"
10+
type: boolean
11+
default: true
712

813
permissions:
914
id-token: write
@@ -42,22 +47,32 @@ jobs:
4247
env:
4348
TFC_TOKEN: ${{ steps.tfc-token.outputs.secret }}
4449
TFC_ORG: workleap
50+
DRY_RUN: ${{ inputs.dry_run }}
4551
run: |
4652
set -euo pipefail
4753
mkdir -p tfc-data
4854
4955
PROBLEM_STATUSES="errored|plan_errored|apply_errored|planned|policy_checked|cost_estimated"
5056
51-
# Paginated fetch of all workspaces with their current run
57+
# In dry-run mode, fetch only 1 workspace to validate the pipeline
58+
if [ "$DRY_RUN" = "true" ]; then
59+
PAGE_SIZE=1
60+
MAX_PAGES=1
61+
else
62+
PAGE_SIZE=100
63+
MAX_PAGES=999
64+
fi
65+
66+
# Paginated fetch of workspaces with their current run
5267
page=1
5368
all_workspaces="[]"
5469
all_included="[]"
5570
56-
while true; do
71+
while [ "$page" -le "$MAX_PAGES" ]; do
5772
response=$(curl -sf \
5873
--header "Authorization: Bearer $TFC_TOKEN" \
5974
--header "Content-Type: application/vnd.api+json" \
60-
"https://app.terraform.io/api/v2/organizations/$TFC_ORG/workspaces?include=current-run&page%5Bsize%5D=100&page%5Bnumber%5D=$page")
75+
"https://app.terraform.io/api/v2/organizations/$TFC_ORG/workspaces?include=current-run&page%5Bsize%5D=$PAGE_SIZE&page%5Bnumber%5D=$page")
6176
6277
page_data=$(echo "$response" | jq '.data // []')
6378
page_included=$(echo "$response" | jq '.included // []')
@@ -133,16 +148,17 @@ jobs:
133148
fi
134149
done
135150
136-
- name: Analyze and report to Slack
151+
- name: Analyze failures
137152
if: steps.collect-tfc.outputs.found != '0'
138153
uses: anthropics/claude-code-action@v1
139154
env:
140155
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL_IDP_DEV_ALERTS }}
156+
DRY_RUN: ${{ inputs.dry_run }}
141157
with:
142158
anthropic_api_key: ${{ steps.claude-key.outputs.secret }}
143159
github_token: ${{ steps.gh-pat.outputs.secret }}
144160
claude_args: |
145-
--allowedTools "Read,Glob,Bash(curl:*)"
161+
--allowedTools "Read,Glob,Bash(curl:*),Write"
146162
prompt: |
147163
You are a Terraform infrastructure analyst. Your job is to read the collected
148164
TFC workspace data and compose a Slack message summarizing the current state.
@@ -171,7 +187,7 @@ jobs:
171187
For each pending workspace:
172188
- Note what it's waiting for (manual apply approval, policy override, etc.)
173189
174-
## Step 4: Compose and send Slack message
190+
## Step 4: Compose Slack message
175191
176192
Build a JSON payload using Slack Block Kit format. The message should be
177193
scannable in under 30 seconds. Structure:
@@ -186,11 +202,18 @@ jobs:
186202
187203
Use mrkdwn format in section blocks. Keep it concise.
188204
189-
Write the JSON payload to /tmp/slack-payload.json, then post it:
205+
Write the JSON payload to `tfc-data/slack-payload.json`.
206+
207+
## Step 5: Post to Slack (only if not dry run)
208+
209+
Check the DRY_RUN environment variable. If it is "true", do NOT post to Slack.
210+
Just print "Dry run — skipping Slack post" and stop.
211+
212+
Otherwise, post the message:
190213
```
191214
curl -sf -X POST "$SLACK_WEBHOOK_URL" \
192215
-H "Content-Type: application/json" \
193-
-d @/tmp/slack-payload.json
216+
-d @tfc-data/slack-payload.json
194217
```
195218
196219
Important:
@@ -199,10 +222,17 @@ jobs:
199222
- Keep each workspace summary to 1-2 lines max.
200223
201224
- name: Post all-clear to Slack
202-
if: steps.collect-tfc.outputs.found == '0'
225+
if: steps.collect-tfc.outputs.found == '0' && inputs.dry_run != true
203226
env:
204227
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL_IDP_DEV_ALERTS }}
205228
run: |
206229
curl -sf -X POST "$SLACK_WEBHOOK_URL" \
207230
-H "Content-Type: application/json" \
208231
-d '{"blocks":[{"type":"header","text":{"type":"plain_text","text":":terraform: TFC Nightly Report"}},{"type":"section","text":{"type":"mrkdwn","text":":white_check_mark: All workspaces healthy. No failed or pending runs."}},{"type":"context","elements":[{"type":"mrkdwn","text":"<https://app.terraform.io/app/workleap/workspaces|View TFC Dashboard>"}]}]}'
232+
233+
- name: Upload artifacts (dry run)
234+
if: inputs.dry_run == true
235+
uses: actions/upload-artifact@v4
236+
with:
237+
name: tfc-report-dry-run
238+
path: tfc-data/

0 commit comments

Comments
 (0)