Skip to content

Commit e672e34

Browse files
committed
feat: add some error handling
1 parent 41e0f6e commit e672e34

File tree

3 files changed

+112
-121
lines changed

3 files changed

+112
-121
lines changed

.github/workflows/trunk-upgrade.yaml

Lines changed: 5 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -19,93 +19,10 @@ jobs:
1919
- name: Checkout
2020
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
2121

22-
- name: Create Token for MasterpointBot App
23-
uses: tibdex/github-app-token@3beb63f4bd073e61482598c45c71c1019b59b73a #v2.1.0
24-
id: generate-token
22+
- name: Run Trunk Upgrade
23+
uses: masterpointio/github-action-trunk-upgrade@v0.1.0
2524
with:
26-
app_id: ${{ secrets.MP_BOT_APP_ID }}
27-
private_key: ${{ secrets.MP_BOT_APP_PRIVATE_KEY }}
28-
29-
- name: Upgrade
30-
id: trunk-upgrade
31-
uses: trunk-io/trunk-action/upgrade@4d5ecc89b2691705fd08c747c78652d2fc806a94 # v1.1.19
32-
with:
33-
github-token: ${{ steps.generate-token.outputs.token }}
25+
app-id: ${{ secrets.MP_BOT_APP_ID }}
26+
app-private-key: ${{ secrets.MP_BOT_APP_PRIVATE_KEY }}
27+
github-token: ${{ secrets.MASTERPOINT_TEAM_PAT }}
3428
reviewers: "@masterpointio/masterpoint-internal"
35-
prefix: "chore: "
36-
37-
- name: Wait for checks to pass + Merge PR
38-
if: steps.trunk-upgrade.outputs.pull-request-number != ''
39-
env:
40-
GH_TOKEN: ${{ secrets.MASTERPOINT_TEAM_PAT }}
41-
PR_NUMBER: ${{ steps.trunk-upgrade.outputs.pull-request-number }}
42-
run: |
43-
echo "Waiting for status checks to pass on PR #$PR_NUMBER..."
44-
45-
# Wait a bit for checks to start
46-
echo "Waiting 30 seconds for checks to initialize..."
47-
sleep 30
48-
49-
# Try to get all checks first to see if any exist
50-
ALL_CHECKS_JSON=$(gh pr checks "$PR_NUMBER" --json state,bucket || echo "[]")
51-
echo "All checks: $ALL_CHECKS_JSON"
52-
53-
# Get required checks
54-
REQUIRED_CHECKS_JSON=$(gh pr checks "$PR_NUMBER" --required --json state,bucket || echo "[]")
55-
echo "Required checks: $REQUIRED_CHECKS_JSON"
56-
57-
# Check if we have any required checks
58-
REQUIRED_CHECKS_COUNT=$(echo "$REQUIRED_CHECKS_JSON" | jq '. | length')
59-
ALL_CHECKS_COUNT=$(echo "$ALL_CHECKS_JSON" | jq '. | length')
60-
61-
if [ "$REQUIRED_CHECKS_COUNT" -eq 0 ] && [ "$ALL_CHECKS_COUNT" -eq 0 ]; then
62-
echo "No status checks found. This might be expected if no checks are configured."
63-
echo "Proceeding with auto-approval and merge..."
64-
65-
# Auto-approve the PR
66-
gh pr review "$PR_NUMBER" --approve --body "Auto-approved by trunk upgrade workflow (no status checks configured)"
67-
68-
# Merge the PR
69-
gh pr merge "$PR_NUMBER" --squash --delete-branch --admin
70-
exit 0
71-
fi
72-
73-
# If we have required checks, wait for them. Otherwise, wait for all checks.
74-
if [ "$REQUIRED_CHECKS_COUNT" -gt 0 ]; then
75-
echo "Waiting for $REQUIRED_CHECKS_COUNT required status checks..."
76-
CHECKS_TO_MONITOR="required"
77-
else
78-
echo "No required checks configured. Waiting for all $ALL_CHECKS_COUNT status checks..."
79-
CHECKS_TO_MONITOR="all"
80-
fi
81-
82-
# Wait for checks to complete
83-
while true; do
84-
if [ "$CHECKS_TO_MONITOR" = "required" ]; then
85-
CHECKS_JSON=$(gh pr checks "$PR_NUMBER" --required --json state,bucket)
86-
else
87-
CHECKS_JSON=$(gh pr checks "$PR_NUMBER" --json state,bucket)
88-
fi
89-
90-
echo "Current checks status: $CHECKS_JSON"
91-
92-
if echo "$CHECKS_JSON" | jq -e '.[] | select(.bucket=="fail")' > /dev/null; then
93-
echo "One or more checks have failed. Exiting..."
94-
exit 1
95-
fi
96-
97-
FAILED_OR_PENDING_CHECKS=$(echo "$CHECKS_JSON" | jq '[.[] | select(.state!="SUCCESS" or .bucket!="pass")] | length')
98-
if [ "$FAILED_OR_PENDING_CHECKS" -eq 0 ]; then
99-
echo "All checks passed. Auto-approving and merging PR https://github.com/${{ github.repository }}/pull/$PR_NUMBER..."
100-
101-
# Auto-approve the PR
102-
gh pr review "$PR_NUMBER" --approve --body "Auto-approved by trunk upgrade workflow"
103-
104-
# Merge the PR
105-
gh pr merge "$PR_NUMBER" --squash --delete-branch --admin
106-
break
107-
else
108-
echo "Some checks are still running or pending. Retrying in 30s..."
109-
sleep 30
110-
fi
111-
done

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
# Therefore, configs specific to their coding practices are their responsibilty to judiciously manage.
55
.claude/*
66

7+
.tmp-template-sync/

lib/os-modules/Taskfile.yaml

Lines changed: 106 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,33 @@ vars:
1414
terraform-spacelift-aws-integrations \
1515
terraform-spacelift-events-collector-audit-trail \
1616
terraform-spacelift-policies
17+
18+
# Configuration constants
1719
SYNC_BRANCH: chore/sync-with-template
1820
SHARED_TMP_DIR: .tmp-template-sync
21+
TEMPLATE_REPO: https://github.com/masterpointio/terraform-module-template.git
22+
TEMPLATE_OWNER: masterpointio
23+
SYNC_MESSAGE: "chore: sync with latest template state"
24+
25+
# Files to synchronize
26+
SYNC_FILES: >-
27+
.checkov.yaml
28+
.coderabbit.yaml
29+
.editorconfig
30+
.gitignore
31+
.github
32+
.markdownlint.yaml
33+
.terraform-docs.yaml
34+
.tflint.hcl
35+
.trunk
36+
.yamllint.yaml
37+
LICENSE
38+
aqua.yaml
1939
tasks:
40+
# ============================================================================
41+
# File Synchronization Tasks
42+
# ============================================================================
43+
2044
sync:
2145
desc: |
2246
Sync files from `terraform-module-template` to specified Terraform open-source module repos.
@@ -31,33 +55,24 @@ tasks:
3155
3256
vars:
3357
MODULES: "{{if .CLI_ARGS}}{{.CLI_ARGS}}{{else}}{{.DEFAULT_MODULES}}{{end}}"
34-
FILES: >-
35-
.checkov.yaml
36-
.coderabbit.yaml
37-
.editorconfig
38-
.gitignore
39-
.github
40-
.markdownlint.yaml
41-
.terraform-docs.yaml
42-
.tflint.hcl
43-
.trunk
44-
.yamllint.yaml
45-
LICENSE
46-
aqua.yaml
4758
cmds:
4859
- |
4960
# Convert newlines to spaces and remove backslashes
5061
modules=$(echo "{{.MODULES}}" | tr '\n' ' ' | sed 's/\\//g')
5162
for module in $modules
5263
do
5364
echo "Syncing files to ../$module ..."
54-
for file in {{.FILES}}
65+
for file in {{.SYNC_FILES}}
5566
do
5667
echo " Syncing $file"
5768
rsync -av --delete {{.SHARED_TMP_DIR}}/$file ../$module/
5869
done
5970
done
6071
72+
# ============================================================================
73+
# Git Branch Management Tasks
74+
# ============================================================================
75+
6176
pull-and-branch:
6277
desc: |
6378
Pull main branch and create a sync branch for specified Terraform open-source module repos.
@@ -81,7 +96,7 @@ tasks:
8196
echo -e "\n\n🚀 Processing ---------------- $module \n"
8297
if [ ! -d ../$module ]; then
8398
echo "🧲 Cloning repository..."
84-
git clone "git@github.com:masterpointio/$module.git" ../$module
99+
git clone "git@github.com:{{.TEMPLATE_OWNER}}/$module.git" ../$module
85100
fi
86101
cd ../$module
87102
@@ -117,6 +132,10 @@ tasks:
117132
cd -
118133
done
119134
135+
# ============================================================================
136+
# Commit and Push Tasks
137+
# ============================================================================
138+
120139
push:
121140
desc: |
122141
Commit and push changes with the specified commit message.
@@ -140,14 +159,25 @@ tasks:
140159
cd ../$module
141160
echo "🔄 Checking out {{.SYNC_BRANCH}} branch..."
142161
git checkout {{.SYNC_BRANCH}}
143-
echo "📝 Committing changes..."
162+
echo "📝 Staging changes..."
144163
git add .
145-
git commit -m "chore: update with the latest template state"
146-
echo "⬆️ Pushing changes..."
147-
git push origin {{.SYNC_BRANCH}}
164+
165+
# Check if there are any changes to commit
166+
if git diff --staged --quiet; then
167+
echo "⚠️ No changes to commit in $module, skipping..."
168+
else
169+
echo "📝 Committing changes..."
170+
git commit -m "{{.SYNC_MESSAGE}}"
171+
echo "⬆️ Pushing changes..."
172+
git push origin {{.SYNC_BRANCH}}
173+
fi
148174
cd -
149175
done
150176
177+
# ============================================================================
178+
# Pull Request Management Tasks
179+
# ============================================================================
180+
151181
pr:
152182
desc: |
153183
Create pull requests for the changes pushed to SYNC_BRANCH.
@@ -161,18 +191,24 @@ tasks:
161191
162192
vars:
163193
MODULES: "{{if .CLI_ARGS}}{{.CLI_ARGS}}{{else}}{{.DEFAULT_MODULES}}{{end}}"
164-
PR_TITLE: "chore: sync with latest template state"
165-
PR_BODY: |
166-
This PR syncs the repository with the latest state from `terraform-module-template`.
167-
168-
**Changes include:**
169-
- Updated configuration files (.checkov.yaml, .markdownlint.yaml, etc.)
170-
- Updated GitHub workflows and templates
171-
- Updated linting and formatting configurations
172-
- Updated documentation templates
173-
174194
cmds:
175195
- |
196+
# Get the latest commit SHA from terraform-module-template
197+
echo "🔍 Fetching latest commit SHA from terraform-module-template..."
198+
template_sha=$(git ls-remote {{.TEMPLATE_REPO}} HEAD | cut -f1)
199+
template_short_sha=$(echo $template_sha | cut -c1-7)
200+
201+
# Try to get the latest tag from terraform-module-template
202+
echo "🏷️ Fetching latest tag from terraform-module-template..."
203+
template_tag=$(git ls-remote --tags --sort=-version:refname {{.TEMPLATE_REPO}} | head -n1 | sed 's/.*refs\/tags\///' | sed 's/\^{}//')
204+
205+
# If no tag found, use "no tags"
206+
if [ -z "$template_tag" ]; then
207+
template_tag="no tags"
208+
fi
209+
210+
echo "📝 Template info: Tag: $template_tag, SHA: $template_short_sha"
211+
176212
# Convert newlines to spaces and remove backslashes
177213
modules=$(echo "{{.MODULES}}" | tr '\n' ' ' | sed 's/\\//g')
178214
for module in $modules
@@ -187,35 +223,72 @@ tasks:
187223
git checkout {{.SYNC_BRANCH}}
188224
fi
189225
226+
# Check if there are commits ahead of main
190227
commits_ahead=$(git rev-list --count main..{{.SYNC_BRANCH}})
191228
if [ "$commits_ahead" -eq 0 ]; then
192229
echo "⏭️ No commits ahead of main, skipping PR creation for $module"
193230
cd -
194231
continue
195232
fi
196233
234+
# Ensure remote branch exists
235+
if ! git ls-remote --heads origin {{.SYNC_BRANCH}} | grep -q {{.SYNC_BRANCH}}; then
236+
echo "📤 Remote branch doesn't exist, pushing {{.SYNC_BRANCH}} to origin..."
237+
git push origin {{.SYNC_BRANCH}}
238+
fi
239+
240+
# Check if PR already exists
241+
existing_pr=$(gh pr list --head {{.SYNC_BRANCH}} --base main --json number --jq '.[0].number' 2>/dev/null || echo "")
242+
if [ -n "$existing_pr" ]; then
243+
echo "📋 PR already exists (#$existing_pr), skipping PR creation for $module"
244+
cd -
245+
continue
246+
fi
247+
248+
# Create PR body with template version info
197249
echo "📋 Creating pull request..."
250+
251+
# Create PR body using printf to properly handle multi-line formatting
252+
pr_body=$(printf "%s\n\n%s\n%s\n%s\n\n%s\n%s\n%s\n%s\n%s" \
253+
"This PR syncs the repository with the latest state from [terraform-module-template]({{.TEMPLATE_REPO}})." \
254+
"**Template Version:**" \
255+
"- **Tag:** [$template_tag]({{.TEMPLATE_REPO}}/releases/tag/$template_tag)" \
256+
"- **Commit SHA:** $template_short_sha" \
257+
"**Changes include:**" \
258+
"- Updated configuration files (.checkov.yaml, .markdownlint.yaml, etc.)" \
259+
"- Updated GitHub workflows and templates" \
260+
"- Updated linting and formatting configurations" \
261+
"- Updated documentation templates")
262+
198263
gh pr create \
199-
--title "{{.PR_TITLE}}" \
200-
--body "{{.PR_BODY}}" \
264+
--title "{{.SYNC_MESSAGE}}" \
265+
--body "$pr_body" \
201266
--base main \
202267
--head {{.SYNC_BRANCH}} \
203-
--repo "masterpointio/$module"
268+
--repo "{{.TEMPLATE_OWNER}}/$module"
204269
205270
cd -
206271
done
207272
273+
# ============================================================================
274+
# Template Management Tasks
275+
# ============================================================================
276+
208277
setup-template:
209278
desc: Set up the template repository in a shared temporary directory
210279
cmds:
211280
- task: cleanup-template
212-
- git clone --depth 1 https://github.com/masterpointio/terraform-module-template.git {{.SHARED_TMP_DIR}}
281+
- git clone --depth 1 {{.TEMPLATE_REPO}} {{.SHARED_TMP_DIR}}
213282

214283
cleanup-template:
215284
desc: Clean up the shared temporary directory
216285
cmds:
217286
- rm -rf {{.SHARED_TMP_DIR}}
218287

288+
# ============================================================================
289+
# Composite Workflow Tasks
290+
# ============================================================================
291+
219292
sync-all:
220293
desc: |
221294
Pull main branch, create a sync branch, and sync with template for specified Terraform open-source module repos.

0 commit comments

Comments
 (0)