Skip to content

Commit 9cafda5

Browse files
committed
Update set-version script to accept version argument and modify Docker workflow to pass tag
1 parent 7a0e6c5 commit 9cafda5

3 files changed

Lines changed: 29 additions & 20 deletions

File tree

.github/workflows/publish-docker-image.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ jobs:
3939
ref: ${{ github.event.inputs.commit }}
4040

4141
- name: Set tag version
42-
run: chmod +x ./scripts/set-version.sh && ./scripts/set-version.sh
42+
run: chmod +x ./scripts/set-version.sh && ./scripts/set-version.sh v${{ github.event.inputs.tag }}
4343

4444
- name: Login to Docker Hub
4545
uses: docker/login-action@v3

Taskfile.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ tasks:
6060
desc: Set the version from latest git tag in the relevant files
6161
cmds:
6262
- chmod +x ./scripts/set-version.sh
63-
- ./scripts/set-version.sh
63+
- ./scripts/set-version.sh {{.CLI_ARGS}}
6464

6565
serve:
6666
desc: Serve the built project

scripts/set-version.sh

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,53 @@ set -euo pipefail
44
# A script to update version strings in specified files based on the latest Git tag.
55
# Used in CI/CD pipelines to ensure version consistency.
66

7-
main() {
8-
local version
9-
if ! version=$(git describe --tags --abbrev=0 2>/dev/null); then
10-
echo "Error: no git tags found." >&2
11-
exit 1
12-
fi
13-
[[ $version == v* ]] || version="v$version"
14-
15-
local file="./internal/config/version.go"
16-
local search='const Version = "v0.0.0-dev"'
17-
local replacement="const Version = \"$version\""
7+
replace_version() {
8+
local file=$1
9+
local search=$2
10+
local template=$3
11+
local version=$4
1812

1913
if [[ ! -f $file ]]; then
20-
echo "Set version error: file not found: $file" >&2
21-
exit 1
14+
echo "Error: file not found: $file" >&2
15+
return 1
2216
fi
2317
if ! grep -Fq "$search" "$file"; then
24-
echo "Set version error: target text not found in $file" >&2
25-
exit 1
18+
echo "Error: target text not found in $file" >&2
19+
return 1
2620
fi
2721

2822
local temp
2923
temp=$(mktemp)
30-
trap 'rm -f "$temp"' EXIT
24+
trap 'rm -f "$temp"' RETURN
3125

3226
local replaced=0
3327
while IFS= read -r line; do
3428
if (( replaced == 0 )) && [[ $line == "$search" ]]; then
35-
printf '%s\n' "$replacement" >>"$temp"
29+
printf '%s\n' "${template//%/$version}" >>"$temp"
3630
replaced=1
3731
else
3832
printf '%s\n' "$line" >>"$temp"
3933
fi
4034
done <"$file"
4135

4236
mv "$temp" "$file"
43-
trap - EXIT
37+
trap - RETURN
4438
echo "Updated $file to $version"
4539
}
4640

41+
main() {
42+
if [[ $# -ne 1 ]]; then
43+
echo "Usage: ${0##*/} <version>" >&2
44+
exit 1
45+
fi
46+
47+
local version=$1
48+
if [[ $version != v* ]]; then
49+
echo "Error: version must start with 'v'." >&2
50+
exit 1
51+
fi
52+
53+
replace_version "./internal/config/version.go" 'const Version = "v0.0.0-dev"' 'const Version = "%"' "$version"
54+
}
55+
4756
main "$@"

0 commit comments

Comments
 (0)