Skip to content

Commit f1255bd

Browse files
authored
extract version from main.go
1 parent 7f7e587 commit f1255bd

File tree

1 file changed

+41
-38
lines changed

1 file changed

+41
-38
lines changed

.github/workflows/build-release.yaml

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,45 @@ on:
55
push:
66

77
jobs:
8+
version:
9+
runs-on: ubuntu-latest
10+
outputs:
11+
version: ${{ steps.get_version.outputs.version }}
12+
steps:
13+
- name: Checkout codebase
14+
uses: actions/checkout@v4
15+
16+
- name: Extract version from main.go
17+
id: get_version
18+
run: |
19+
set -e
20+
VERSION=$(grep -Eo 'version[[:space:]]*=[[:space:]]*"[^"]+"' main.go | head -n1 | sed -E 's/.*"([^"]+)"/\1/')
21+
if [ -z "$VERSION" ]; then
22+
echo "::error::Failed to detect version constant in source code"
23+
exit 1
24+
fi
25+
echo "Detected version: $VERSION"
26+
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
27+
28+
- name: Create git tag if needed
29+
if: github.ref == 'refs/heads/main'
30+
run: |
31+
git config --local user.name "github-actions"
32+
git config --local user.email "github-actions@users.noreply.github.com"
33+
git fetch --tags --quiet
34+
if git rev-parse "refs/tags/v${VERSION}" >/dev/null 2>&1; then
35+
echo "Tag v${VERSION} already exists"
36+
else
37+
git tag -a "v${VERSION}" -m "v${VERSION}"
38+
git push origin "v${VERSION}"
39+
fi
40+
env:
41+
VERSION: ${{ steps.get_version.outputs.version }}
42+
843
build:
44+
needs: version
945
permissions:
10-
contents: write
46+
contents: read
1147
strategy:
1248
fail-fast: false
1349
matrix:
@@ -40,43 +76,13 @@ jobs:
4076
GOARM: ${{ matrix.goarm }}
4177
CGO_ENABLED: 0
4278
steps:
43-
- name: Checkout codebase
44-
uses: actions/checkout@v4
45-
4679
- name: Show workflow information
4780
run: |
4881
export _NAME=$GOOS-$GOARCH$GOARM$GOMIPS
4982
echo "GOOS: $GOOS, GOARCH: $GOARCH, GOARM: $GOARM, GOMIPS: $GOMIPS, RELEASE_NAME: $_NAME"
5083
echo "ASSET_NAME=$_NAME" >> $GITHUB_ENV
5184
echo "REF=${GITHUB_SHA::6}" >> $GITHUB_ENV
5285
53-
- name: Extract version from main.go
54-
id: version
55-
run: |
56-
set -e
57-
VERSION=$(grep -Eo 'version[[:space:]]*=[[:space:]]*"[^"]+"' main.go | head -n1 | sed -E 's/.*"([^"]+)"/\1/')
58-
if [ -z "$VERSION" ]; then
59-
echo "::error::Failed to detect version constant in source code"
60-
exit 1
61-
fi
62-
echo "Detected version: $VERSION"
63-
echo "VERSION=$VERSION" >> $GITHUB_ENV
64-
# expose as step output
65-
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
66-
67-
- name: Create git tag if needed
68-
if: github.ref == 'refs/heads/main'
69-
run: |
70-
git config --local user.name "github-actions"
71-
git config --local user.email "github-actions@users.noreply.github.com"
72-
git fetch --tags --quiet
73-
if git rev-parse "refs/tags/v${VERSION}" >/dev/null 2>&1; then
74-
echo "Tag v${VERSION} already exists"
75-
else
76-
git tag -a "v${VERSION}" -m "v${VERSION}"
77-
git push origin "v${VERSION}"
78-
fi
79-
8086
- name: Set up Go
8187
uses: actions/setup-go@v5
8288
with:
@@ -86,20 +92,17 @@ jobs:
8692

8793
- name: Build heybabe
8894
run: |
89-
go build -v -o bin/heybabe_${{ env.ASSET_NAME }} -trimpath -ldflags "-s -w -buildid= -X main.version=${VERSION}" .
95+
go build -v -o bin/heybabe_${{ env.ASSET_NAME }} -trimpath -ldflags "-s -w -buildid= -X main.version=${{ needs.version.outputs.version }}" .
9096
9197
- name: Upload files to Artifacts
9298
uses: actions/upload-artifact@v4
9399
with:
94100
name: heybabe_${{ env.ASSET_NAME }}_${{ env.REF }}
95101
path: ./bin/heybabe_${{ env.ASSET_NAME }}
96102

97-
outputs:
98-
version: ${{ steps.version.outputs.version }}
99-
100103
release:
101104
name: Publish Release
102-
needs: build
105+
needs: [version, build]
103106
runs-on: ubuntu-latest
104107
permissions:
105108
contents: write
@@ -112,8 +115,8 @@ jobs:
112115
- name: Create / Update GitHub Release and upload binaries
113116
uses: softprops/action-gh-release@v2
114117
with:
115-
tag_name: v${{ needs.build.outputs.version }}
116-
name: v${{ needs.build.outputs.version }}
118+
tag_name: v${{ needs.version.outputs.version }}
119+
name: v${{ needs.version.outputs.version }}
117120
draft: false
118121
prerelease: false
119122
files: |

0 commit comments

Comments
 (0)