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
1939tasks :
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.
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