Skip to content

Commit b56adc5

Browse files
committed
fix: simplify version bump implementation to single script
- Modify version-bump.sh to support both update and check modes - Add --check flag and 'check' command for validation without changes - Combine version-check and version-bump workflows into single workflow - Remove separate version-check.yaml workflow (was overcomplicated) - Update CONTRIBUTING.md to document simplified approach - Script now exits with proper codes: 0 for success, 1 for needs update - Workflow runs check job on all PRs, bump job only on labels - Much cleaner and simpler implementation as requested
1 parent bb4e39f commit b56adc5

File tree

4 files changed

+132
-82
lines changed

4 files changed

+132
-82
lines changed

.github/scripts/version-bump.sh

Lines changed: 91 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,24 @@
22

33
# Version Bump Script
44
# Extracts version bump logic from GitHub Actions workflow
5-
# Usage: ./version-bump.sh <bump_type> [base_ref]
6-
# bump_type: patch, minor, or major
5+
# Usage: ./version-bump.sh <bump_type> [base_ref] [--check]
6+
# bump_type: patch, minor, major, or check
77
# base_ref: base reference for diff (default: origin/main)
8+
# --check: only check if versions need updating, don't modify files
89

910
set -euo pipefail
1011

1112
# Function to print usage
1213
usage() {
13-
echo "Usage: $0 <bump_type> [base_ref]"
14-
echo " bump_type: patch, minor, or major"
14+
echo "Usage: $0 <bump_type> [base_ref] [--check]"
15+
echo " bump_type: patch, minor, major, or check"
1516
echo " base_ref: base reference for diff (default: origin/main)"
17+
echo " --check: only check if versions need updating, don't modify files"
18+
echo ""
19+
echo "Examples:"
20+
echo " $0 patch # Update versions with patch bump"
21+
echo " $0 check # Check if versions need updating"
22+
echo " $0 patch origin/main --check # Check what patch bump would do"
1623
exit 1
1724
}
1825

@@ -62,6 +69,7 @@ update_readme_version() {
6269
local namespace="$2"
6370
local module_name="$3"
6471
local new_version="$4"
72+
local check_mode="$5"
6573

6674
if [ ! -f "$readme_path" ]; then
6775
return 1
@@ -70,24 +78,28 @@ update_readme_version() {
7078
# Check if README contains version references for this specific module
7179
local module_source="registry.coder.com/${namespace}/${module_name}/coder"
7280
if grep -q "source.*${module_source}" "$readme_path"; then
73-
echo "Updating version references for $namespace/$module_name in $readme_path"
74-
# Use awk to only update versions that follow the specific module source
75-
awk -v module_source="$module_source" -v new_version="$new_version" '
76-
/source.*=.*/ {
77-
if ($0 ~ module_source) {
78-
in_target_module = 1
79-
} else {
80-
in_target_module = 0
81-
}
82-
}
83-
/version.*=.*"/ {
84-
if (in_target_module) {
85-
gsub(/version[[:space:]]*=[[:space:]]*"[^"]*"/, "version = \"" new_version "\"")
86-
in_target_module = 0
87-
}
88-
}
89-
{ print }
90-
' "$readme_path" > "${readme_path}.tmp" && mv "${readme_path}.tmp" "$readme_path"
81+
if [ "$check_mode" = "true" ]; then
82+
echo "Would update version references for $namespace/$module_name in $readme_path"
83+
else
84+
echo "Updating version references for $namespace/$module_name in $readme_path"
85+
# Use awk to only update versions that follow the specific module source
86+
awk -v module_source="$module_source" -v new_version="$new_version" '
87+
/source.*=.*/ {
88+
if ($0 ~ module_source) {
89+
in_target_module = 1
90+
} else {
91+
in_target_module = 0
92+
}
93+
}
94+
/version.*=.*"/ {
95+
if (in_target_module) {
96+
gsub(/version[[:space:]]*=[[:space:]]*"[^"]*"/, "version = \"" new_version "\"")
97+
in_target_module = 0
98+
}
99+
}
100+
{ print }
101+
' "$readme_path" > "${readme_path}.tmp" && mv "${readme_path}.tmp" "$readme_path"
102+
fi
91103
return 0
92104
elif grep -q 'version\s*=\s*"' "$readme_path"; then
93105
echo "⚠️ Found version references but no module source match for $namespace/$module_name"
@@ -100,19 +112,40 @@ update_readme_version() {
100112
# Main function
101113
main() {
102114
# Parse arguments
103-
if [ $# -lt 1 ] || [ $# -gt 2 ]; then
115+
if [ $# -lt 1 ] || [ $# -gt 3 ]; then
104116
usage
105117
fi
106118

107119
local bump_type="$1"
108-
local base_ref="${2:-origin/main}"
120+
local base_ref="origin/main"
121+
local check_mode=false
122+
123+
# Parse remaining arguments
124+
shift
125+
while [ $# -gt 0 ]; do
126+
case "$1" in
127+
--check)
128+
check_mode=true
129+
;;
130+
*)
131+
base_ref="$1"
132+
;;
133+
esac
134+
shift
135+
done
136+
137+
# Handle "check" as bump type
138+
if [ "$bump_type" = "check" ]; then
139+
check_mode=true
140+
bump_type="patch" # Use patch as default for check mode
141+
fi
109142

110143
# Validate bump type
111144
case "$bump_type" in
112145
"patch"|"minor"|"major")
113146
;;
114147
*)
115-
echo "❌ Invalid bump type: '$bump_type'. Expected patch, minor, or major." >&2
148+
echo "❌ Invalid bump type: '$bump_type'. Expected patch, minor, major, or check." >&2
116149
exit 1
117150
;;
118151
esac
@@ -196,7 +229,7 @@ main() {
196229
echo "New version: $new_version"
197230

198231
# Update README if applicable
199-
if update_readme_version "$readme_path" "$namespace" "$module_name" "$new_version"; then
232+
if update_readme_version "$readme_path" "$namespace" "$module_name" "$new_version" "$check_mode"; then
200233
updated_readmes="$updated_readmes\n- $namespace/$module_name"
201234
has_changes=true
202235
fi
@@ -228,19 +261,39 @@ main() {
228261
fi
229262

230263
# Check for changes and provide guidance
231-
if [ "$has_changes" = true ]; then
232-
echo "✅ Version bump completed successfully!"
233-
echo "📝 README files have been updated with new versions."
234-
echo ""
235-
echo "Next steps:"
236-
echo "1. Review the changes: git diff"
237-
echo "2. Commit the changes: git add . && git commit -m 'chore: bump module versions ($bump_type)'"
238-
echo "3. Push the changes: git push"
239-
exit 0
264+
if [ "$check_mode" = "true" ]; then
265+
if [ "$has_changes" = true ]; then
266+
echo "❌ Module versions need to be updated!"
267+
echo "📝 The following modules would have their README files updated:"
268+
echo -e "$updated_readmes"
269+
echo ""
270+
echo "To fix this, run one of:"
271+
echo " ./.github/scripts/version-bump.sh patch"
272+
echo " ./.github/scripts/version-bump.sh minor"
273+
echo " ./.github/scripts/version-bump.sh major"
274+
echo ""
275+
echo "Or add a version label to your PR: version:patch, version:minor, or version:major"
276+
exit 1
277+
else
278+
echo "✅ Module versions are up to date!"
279+
echo "ℹ️ No version updates needed."
280+
exit 0
281+
fi
240282
else
241-
echo "ℹ️ No README files were updated (no version references found matching module sources)."
242-
echo "Version calculations completed, but no files were modified."
243-
exit 0
283+
if [ "$has_changes" = true ]; then
284+
echo "✅ Version bump completed successfully!"
285+
echo "📝 README files have been updated with new versions."
286+
echo ""
287+
echo "Next steps:"
288+
echo "1. Review the changes: git diff"
289+
echo "2. Commit the changes: git add . && git commit -m 'chore: bump module versions ($bump_type)'"
290+
echo "3. Push the changes: git push"
291+
exit 0
292+
else
293+
echo "ℹ️ No README files were updated (no version references found matching module sources)."
294+
echo "Version calculations completed, but no files were modified."
295+
exit 0
296+
fi
244297
fi
245298
}
246299

.github/workflows/version-bump.yaml

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,35 @@ name: Version Bump
22

33
on:
44
pull_request:
5-
types: [labeled]
5+
types: [opened, synchronize, labeled]
6+
paths:
7+
- 'registry/**/modules/**'
68

79
concurrency:
810
group: ${{ github.workflow }}-${{ github.ref }}
911
cancel-in-progress: true
1012

1113
jobs:
14+
version-check:
15+
runs-on: ubuntu-latest
16+
steps:
17+
- name: Checkout code
18+
uses: actions/checkout@v4
19+
with:
20+
fetch-depth: 0
21+
22+
- name: Check if version bump is needed
23+
run: |
24+
echo "🔍 Checking if module versions need to be updated..."
25+
26+
# Run the script in check mode
27+
if ./.github/scripts/version-bump.sh check origin/main; then
28+
echo "✅ Module versions are up to date"
29+
else
30+
echo "❌ Module versions need to be updated"
31+
exit 1
32+
fi
33+
1234
version-bump:
1335
if: github.event.label.name == 'version:patch' || github.event.label.name == 'version:minor' || github.event.label.name == 'version:major'
1436
runs-on: ubuntu-latest
@@ -49,7 +71,7 @@ jobs:
4971
- name: Run version bump script
5072
id: version-bump
5173
run: |
52-
# Capture script output for later use in PR comment
74+
# Run the script to update versions
5375
output_file=$(mktemp)
5476
if ./.github/scripts/version-bump.sh "${{ steps.bump-type.outputs.type }}" origin/main > "$output_file" 2>&1; then
5577
echo "success=true" >> $GITHUB_OUTPUT
@@ -71,16 +93,15 @@ jobs:
7193
# Show output
7294
cat "$output_file"
7395
74-
- name: Commit changes
96+
- name: Check for changes and commit
7597
if: steps.version-bump.outputs.success == 'true'
7698
run: |
77-
# Check if any README files were modified
78-
if git diff --quiet 'registry/*/modules/*/README.md'; then
79-
echo "No README changes to commit"
99+
# Check if any files were modified
100+
if git diff --quiet; then
101+
echo "No changes to commit"
80102
else
81-
echo "Committing README changes..."
82-
git diff --name-only 'registry/*/modules/*/README.md'
83-
git add registry/*/modules/*/README.md
103+
echo "Committing version bump changes..."
104+
git add .
84105
git commit -m "chore: bump module versions (${{ steps.bump-type.outputs.type }})"
85106
git push
86107
fi

.github/workflows/version-check.yaml

Lines changed: 0 additions & 34 deletions
This file was deleted.

CONTRIBUTING.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,9 @@ Use the version bump script to automatically update module versions:
277277

278278
# For breaking changes
279279
./.github/scripts/version-bump.sh major
280+
281+
# To check if versions need updating (without making changes)
282+
./.github/scripts/version-bump.sh check
280283
```
281284

282285
The script will:
@@ -294,7 +297,14 @@ You can also add a label to your PR instead of running the script manually:
294297

295298
The version bump will happen automatically when the label is added.
296299

297-
**Important**: Always update module versions when making changes. The CI will check that versions are properly updated.
300+
### CI Validation
301+
302+
The CI will automatically check that module versions are properly updated:
303+
- **Version Check**: Runs on all PRs that modify modules
304+
- **Fails if versions need updating** but haven't been bumped
305+
- **Passes if versions are up to date** or no modules were modified
306+
307+
**Important**: Always update module versions when making changes. The CI will enforce this requirement.
298308

299309
---
300310

0 commit comments

Comments
 (0)