Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions go/cmd/orchestrator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,29 @@ import (
_ "github.com/mattn/go-sqlite3"
)

// AppVersion and GitCommit are normally injected via -ldflags by build.sh / script/build.
// When building with plain `go build` they are empty; defaultAppVersion (kept in sync with
// RELEASE_VERSION) is used so `orchestrator -version` is never blank (#109).
var AppVersion, GitCommit string

// defaultAppVersion must match the contents of the repo-root RELEASE_VERSION file.
const defaultAppVersion = "4.30.0"

func resolvedAppVersion() string {
if AppVersion != "" {
return AppVersion
}
return defaultAppVersion
}

func versionOutput() string {
if AppVersion != "" {
return AppVersion
}
// Plain `go build` without -ldflags: still report the tree version, marked devel.
return defaultAppVersion + " (devel)"
}

// main is the application's entry point. It will either spawn a CLI or HTTP interfaces.
func main() {
configFile := flag.String("config", "", "config file name")
Expand Down Expand Up @@ -95,15 +116,19 @@ func main() {
log.SetPrintStackTrace(*stack)
}
if *config.RuntimeCLIFlags.Version {
fmt.Println(AppVersion)
fmt.Println(GitCommit)
fmt.Println(versionOutput())
if GitCommit != "" {
fmt.Println(GitCommit)
}
return
}

startText := "starting orchestrator"
if AppVersion != "" {
startText += ", version: " + AppVersion
// Ensure runtime always has a non-empty version (API/status, backend deploy checks).
if AppVersion == "" {
AppVersion = resolvedAppVersion()
}

startText := "starting orchestrator, version: " + AppVersion
if GitCommit != "" {
startText += ", git commit: " + GitCommit
}
Expand Down
67 changes: 67 additions & 0 deletions go/cmd/orchestrator/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
Copyright 2014 Outbrain Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"os"
"path/filepath"
"strings"
"testing"
)

func TestDefaultAppVersionMatchesReleaseFile(t *testing.T) {
// Walk up from this test file's package dir to the repo root.
dir, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
var release string
for {
b, err := os.ReadFile(filepath.Join(dir, "RELEASE_VERSION"))
if err == nil {
release = strings.TrimSpace(string(b))
break
}
parent := filepath.Dir(dir)
if parent == dir {
t.Fatal("RELEASE_VERSION not found")
}
dir = parent
}
if release != defaultAppVersion {
t.Fatalf("defaultAppVersion %q must match RELEASE_VERSION %q", defaultAppVersion, release)
}
}

func TestVersionOutputNeverEmpty(t *testing.T) {
saved := AppVersion
defer func() { AppVersion = saved }()

AppVersion = ""
out := versionOutput()
if strings.TrimSpace(out) == "" {
t.Fatal("versionOutput empty when AppVersion unset")
}
if !strings.Contains(out, defaultAppVersion) {
t.Fatalf("versionOutput %q missing defaultAppVersion", out)
}

AppVersion = "9.9.9"
if versionOutput() != "9.9.9" {
t.Fatalf("versionOutput should prefer ldflags AppVersion, got %q", versionOutput())
}
}
10 changes: 7 additions & 3 deletions script/build
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ cd "$(dirname "$0")/.."
mkdir -p bin
bindir="$PWD"/bin

version=$(git rev-parse HEAD)
describe=$(git describe --tags --always --dirty)
commit=$(git rev-parse HEAD)
if [ -f RELEASE_VERSION ]; then
version=$(tr -d '[:space:]' < RELEASE_VERSION)
else
version=$(git describe --tags --always --dirty)
fi

go build -o "$bindir/orchestrator" -ldflags "-X main.AppVersion=${version} -X main.BuildDescribe=${describe}" ./go/cmd/orchestrator/main.go
go build -o "$bindir/orchestrator" -ldflags "-X main.AppVersion=${version} -X main.GitCommit=${commit}" ./go/cmd/orchestrator/main.go

rsync -qa ./resources $bindir/
Loading