Skip to content

Commit f7d23b3

Browse files
committed
ci: make version input optional and validate format
1 parent ef90f82 commit f7d23b3

1 file changed

Lines changed: 23 additions & 8 deletions

File tree

.github/workflows/build.yml

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ on:
1616
inputs:
1717
version:
1818
description: 'Release version (SemVer)'
19-
required: true
19+
required: false
2020
build_mode:
2121
description: 'Build mode'
2222
required: false
@@ -56,12 +56,23 @@ jobs:
5656
echo "VERSION=$VERSION" >> $GITHUB_ENV
5757
sed -i '/#define RELEASE_VER_STR/,/TOSTRING(RELEASE_VER_FIX)/c\#define RELEASE_VER_STR "'"$VERSION"'"' src/version.h
5858
elif [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
59-
VERSION="${{ github.event.inputs.version }}"
60-
echo "VERSION=$VERSION" >> $GITHUB_ENV
61-
IFS='.' read -r MAJOR MINOR PATCH <<< "$VERSION"
62-
sed -i 's/#define RELEASE_VER_MAIN .*/#define RELEASE_VER_MAIN '"$MAJOR"'/' src/version.h
63-
sed -i 's/#define RELEASE_VER_SUB .*/#define RELEASE_VER_SUB '"$MINOR"'/' src/version.h
64-
sed -i 's/#define RELEASE_VER_FIX .*/#define RELEASE_VER_FIX '"$PATCH"'/' src/version.h
59+
INPUT_VERSION="${{ github.event.inputs.version }}"
60+
if [ -n "$INPUT_VERSION" ]; then
61+
VERSION="$INPUT_VERSION"
62+
if ! [[ "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
63+
echo "::error::Invalid version '$VERSION'. Expected SemVer like 1.2.3" >&2
64+
exit 1
65+
fi
66+
echo "VERSION=$VERSION" >> $GITHUB_ENV
67+
IFS='.' read -r MAJOR MINOR PATCH <<< "$VERSION"
68+
sed -i 's/#define RELEASE_VER_MAIN .*/#define RELEASE_VER_MAIN '"$MAJOR"'/' src/version.h
69+
sed -i 's/#define RELEASE_VER_SUB .*/#define RELEASE_VER_SUB '"$MINOR"'/' src/version.h
70+
sed -i 's/#define RELEASE_VER_FIX .*/#define RELEASE_VER_FIX '"$PATCH"'/' src/version.h
71+
else
72+
COMMIT_HASH=$(git rev-parse --short HEAD)
73+
VERSION="manual-${COMMIT_HASH}"
74+
echo "VERSION=$VERSION" >> $GITHUB_ENV
75+
fi
6576
fi
6677
6778
- name: Set Build Mode
@@ -107,7 +118,7 @@ jobs:
107118
create_pr:
108119
needs: build
109120
runs-on: ubuntu-slim
110-
if: github.event_name == 'workflow_dispatch'
121+
if: github.event_name == 'workflow_dispatch' && github.event.inputs.version != ''
111122

112123
steps:
113124
- name: Checkout repository
@@ -122,6 +133,10 @@ jobs:
122133
- name: Update version.h
123134
run: |
124135
VERSION="${{ github.event.inputs.version }}"
136+
if ! [[ "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
137+
echo "Invalid version '$VERSION'. Expected SemVer like 1.2.3" >&2
138+
exit 1
139+
fi
125140
echo "VERSION=$VERSION" >> $GITHUB_ENV
126141
IFS='.' read -r MAJOR MINOR PATCH <<< "$VERSION"
127142
sed -i 's/#define RELEASE_VER_MAIN .*/#define RELEASE_VER_MAIN '"$MAJOR"'/' src/version.h

0 commit comments

Comments
 (0)