Skip to content

Commit cc7e03b

Browse files
Copilotmbg
andcommitted
Add error handling and validation to Go version workflow
Co-authored-by: mbg <278086+mbg@users.noreply.github.com>
1 parent 1cbd423 commit cc7e03b

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

.github/workflows/go-version-update.yml

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ jobs:
3030
id: fetch-version
3131
run: |
3232
LATEST_GO_VERSION=$(curl -s https://go.dev/dl/\?mode\=json | jq -r '.[0].version')
33+
34+
if [ -z "$LATEST_GO_VERSION" ] || [ "$LATEST_GO_VERSION" = "null" ]; then
35+
echo "Error: Failed to fetch latest Go version from go.dev"
36+
exit 1
37+
fi
38+
3339
echo "Latest Go version from go.dev: $LATEST_GO_VERSION"
3440
echo "version=$LATEST_GO_VERSION" >> $GITHUB_OUTPUT
3541
@@ -45,6 +51,12 @@ jobs:
4551
id: current-version
4652
run: |
4753
CURRENT_VERSION=$(sed -n 's/.*go_sdk\.download(version = \"\([^\"]*\)\".*/\1/p' MODULE.bazel)
54+
55+
if [ -z "$CURRENT_VERSION" ]; then
56+
echo "Error: Could not extract Go version from MODULE.bazel"
57+
exit 1
58+
fi
59+
4860
echo "Current Go version in MODULE.bazel: $CURRENT_VERSION"
4961
echo "version=$CURRENT_VERSION" >> $GITHUB_OUTPUT
5062
@@ -79,18 +91,34 @@ jobs:
7991
8092
echo "Updating from $CURRENT_VERSION to $LATEST_VERSION_NUM"
8193
94+
# Escape dots in version strings for use in sed patterns
95+
CURRENT_VERSION_ESCAPED=$(echo "$CURRENT_VERSION" | sed 's/\./\\./g')
96+
LATEST_VERSION_NUM_ESCAPED=$(echo "$LATEST_VERSION_NUM" | sed 's/\./\\./g')
97+
CURRENT_MAJOR_MINOR_ESCAPED=$(echo "$CURRENT_MAJOR_MINOR" | sed 's/\./\\./g')
98+
LATEST_MAJOR_MINOR_ESCAPED=$(echo "$LATEST_MAJOR_MINOR" | sed 's/\./\\./g')
99+
82100
# Update MODULE.bazel
83-
sed -i "s/go_sdk.download(version = \"$CURRENT_VERSION\")/go_sdk.download(version = \"$LATEST_VERSION_NUM\")/" MODULE.bazel
101+
if ! sed -i "s/go_sdk\.download(version = \"$CURRENT_VERSION_ESCAPED\")/go_sdk.download(version = \"$LATEST_VERSION_NUM\")/" MODULE.bazel; then
102+
echo "Warning: Failed to update MODULE.bazel"
103+
fi
84104
85105
# Update go/extractor/go.mod
86-
sed -i "s/^go $CURRENT_MAJOR_MINOR$/go $LATEST_MAJOR_MINOR/" go/extractor/go.mod
87-
sed -i "s/^toolchain go$CURRENT_VERSION$/toolchain go$LATEST_VERSION_NUM/" go/extractor/go.mod
106+
if ! sed -i "s/^go $CURRENT_MAJOR_MINOR_ESCAPED\$/go $LATEST_MAJOR_MINOR/" go/extractor/go.mod; then
107+
echo "Warning: Failed to update go directive in go.mod"
108+
fi
109+
if ! sed -i "s/^toolchain go$CURRENT_VERSION_ESCAPED\$/toolchain go$LATEST_VERSION_NUM/" go/extractor/go.mod; then
110+
echo "Warning: Failed to update toolchain in go.mod"
111+
fi
88112
89113
# Update go/extractor/autobuilder/build-environment.go
90-
sed -i "s/var maxGoVersion = util.NewSemVer(\"$CURRENT_MAJOR_MINOR\")/var maxGoVersion = util.NewSemVer(\"$LATEST_MAJOR_MINOR\")/" go/extractor/autobuilder/build-environment.go
114+
if ! sed -i "s/var maxGoVersion = util\.NewSemVer(\"$CURRENT_MAJOR_MINOR_ESCAPED\")/var maxGoVersion = util.NewSemVer(\"$LATEST_MAJOR_MINOR\")/" go/extractor/autobuilder/build-environment.go; then
115+
echo "Warning: Failed to update build-environment.go"
116+
fi
91117
92118
# Update go/actions/test/action.yml
93-
sed -i "s/default: \"~$CURRENT_VERSION\"/default: \"~$LATEST_VERSION_NUM\"/" go/actions/test/action.yml
119+
if ! sed -i "s/default: \"~$CURRENT_VERSION_ESCAPED\"/default: \"~$LATEST_VERSION_NUM\"/" go/actions/test/action.yml; then
120+
echo "Warning: Failed to update action.yml"
121+
fi
94122
95123
# Show what changed
96124
git diff
@@ -162,7 +190,7 @@ jobs:
162190
- \`go/extractor/autobuilder/build-environment.go\` - maxGoVersion
163191
- \`go/actions/test/action.yml\` - default go-test-version
164192
165-
This PR was automatically created by the [Go version update workflow](.github/workflows/go-version-update.yml).
193+
This PR was automatically created by the [Go version update workflow](https://github.com/${{ github.repository }}/blob/main/.github/workflows/go-version-update.yml).
166194
EOF
167195
)
168196

0 commit comments

Comments
 (0)