Skip to content

Commit e6c5e0c

Browse files
authored
Merge pull request #26 from sarus-suite/fc-version-metadata-injection
Releases now print correct metadata for version, commit, and build data
2 parents 6652f0e + f1b8d2f commit e6c5e0c

File tree

5 files changed

+37
-6
lines changed

5 files changed

+37
-6
lines changed

.github/workflows/release-opensuse156.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,13 @@ jobs:
5959
GOFLAGS: "-buildvcs=false"
6060
GO_LDFLAGS: "-linkmode external"
6161
CGO_LDFLAGS: "-g -O2"
62+
VERSION_LDFLAGS: "-X parallax/common.Version=${{ github.ref_name }} \
63+
-X parallax/common.Commit=${{ github.sha }} \
64+
-X parallax/common.BuildDate=$(date -u +%Y-%m-%dT%H:%M:%SZ)"
6265
run: |
6366
mkdir -p dist
6467
go build -v -x \
65-
-ldflags "-X 'github.com/sarus-suite/parallax/version.Version=${{ github.ref_name }}'" \
68+
-ldflags "$GO_LDFLAGS $VERSION_LDFLAGS" \
6669
-o dist/parallax-${{ github.ref_name }}-opensuse-15.6-${{ matrix.arch }} \
6770
.
6871

.github/workflows/release.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,13 @@ jobs:
5959
GOFLAGS: "-buildvcs=false"
6060
GO_LDFLAGS: "-linkmode external"
6161
CGO_LDFLAGS: "-g -O2"
62+
VERSION_LDFLAGS: "-X parallax/common.Version=${{ github.ref_name }} \
63+
-X parallax/common.Commit=${{ github.sha }} \
64+
-X parallax/common.BuildDate=$(date -u +%Y-%m-%dT%H:%M:%SZ)"
6265
run: |
6366
mkdir -p dist
6467
go build -v -x \
65-
-ldflags "-X 'github.com/sarus-suite/parallax/version.Version=${{ github.ref_name }}'" \
68+
-ldflags "$GO_LDFLAGS $VERSION_LDFLAGS" \
6669
-o dist/parallax-${{ github.ref_name }}-opensuse-15.5-${{ matrix.arch }} \
6770
.
6871

.github/workflows/unified-vs-test.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ jobs:
3737
-w "$PWD" \
3838
--env TMP_DIR="$TMP_DIR" \
3939
--env GITHUB_REF_NAME="$GITHUB_REF_NAME" \
40+
--env GITHUB_SHA="$GITHUB_SHA" \
4041
ghcr.io/sarus-suite/parallax/ci-runner:latest \
4142
bash -euxc '
4243
mkdir -p $TMP_DIR/dist
@@ -57,8 +58,13 @@ jobs:
5758
export CGO_LDFLAGS="-g -O2"
5859
export GOARCH="$HOST_ARCH"
5960
export GOOS=linux
61+
export VERSION_LDFLAGS="-X parallax/common.Version=${GITHUB_REF_NAME} \
62+
-X parallax/common.Commit=${GITHUB_SHA} \
63+
-X parallax/common.BuildDate=$(date -u +%Y-%m-%dT%H:%M:%SZ)"
64+
6065
6166
go build -v \
67+
-ldflags "$GO_LDFLAGS $VERSION_LDFLAGS" \
6268
-o "$TMP_DIR/dist/$OUT" \
6369
.
6470
echo Built binary: "$TMP_DIR/dist/$OUT"

common/cli.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ import (
1010
"github.com/mattn/go-shellwords"
1111
)
1212

13-
// TODO: auto gen version field
14-
const Version = "1.0.0"
15-
1613
func usage_banner() {
1714
out := flag.CommandLine.Output()
1815

@@ -115,7 +112,7 @@ func ParseAndValidateFlags(fs *flag.FlagSet, args []string) (*CLI, error) {
115112

116113
// Fast version exit
117114
if *versionF {
118-
fmt.Fprintf(os.Stdout, "Parallax version %s\n", Version)
115+
VersionPrint()
119116
os.Exit(0)
120117
}
121118

common/version.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package common
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
var (
8+
// These to get overriden at build time via flags
9+
Version = "unversioned"
10+
Commit = "unknown"
11+
BuildDate = "unknown"
12+
)
13+
14+
func VersionPrint() {
15+
fmt.Printf("Parallax version: %s\n", Version)
16+
if Commit != "" && Commit != "unknown" {
17+
fmt.Printf("Commit: %s\n", Commit)
18+
}
19+
if BuildDate != "" && BuildDate != "unknown" {
20+
fmt.Printf("Build date: %s\n", BuildDate)
21+
}
22+
}

0 commit comments

Comments
 (0)