Skip to content

Commit

Permalink
Replace ldflags with go:embed (temporalio#2085)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexshtin authored Dec 1, 2021
1 parent 799a3c1 commit 2a47cdf
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 117 deletions.
20 changes: 0 additions & 20 deletions .github/workflows/goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,10 @@ jobs:
uses: actions/setup-go@v2
with:
go-version: 1.17.3
- name: Get build date
id: date
run: echo "::set-output name=date::$(date '+%F-%T')"
- name: Get build unix timestamp
id: timestamp
run: echo "::set-output name=timestamp::$(date '+%s')"
- name: Get git branch
id: branch
run: echo "::set-output name=branch::$(git rev-parse --abbrev-ref HEAD)"
- name: Get build platform
id: platform
run: echo "::set-output name=platform::$(go version | cut -d ' ' -f 4)"
- name: Get Go version
id: go
run: echo "::set-output name=go::$(go version | cut -d ' ' -f 3)"
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
version: latest
args: release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
BUILD_DATE: ${{ steps.date.outputs.date }}
BUILD_TS_UNIX: ${{ steps.timestamp.outputs.timestamp }}
GIT_BRANCH: ${{ steps.branch.outputs.branch }}
BUILD_PLATFORM: ${{ steps.platform.outputs.platform }}
GO_VERSION: ${{ steps.go.outputs.go }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@
# Fossa
.fossa.yml

# Build info
build/info/data.json

11 changes: 1 addition & 10 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
env:
- MODULE_ROOT=go.temporal.io/server
before:
hooks:
- go mod download
- ./develop/scripts/create_build_info_data.sh
builds:
- id: "temporal-server"
dir: cmd/server
binary: temporal-server
env:
- CGO_ENABLED=0
ldflags:
- -X {{.Env.MODULE_ROOT}}/ldflags.GitRevision={{.ShortCommit}}
- -X {{.Env.MODULE_ROOT}}/ldflags.GitBranch={{.Env.GIT_BRANCH}}
- -X {{.Env.MODULE_ROOT}}/ldflags.GitTag={{.Tag}}
- -X {{.Env.MODULE_ROOT}}/ldflags.BuildDate={{.Env.BUILD_DATE}}
- -X {{.Env.MODULE_ROOT}}/ldflags.BuildTimeUnix={{.Env.BUILD_TS_UNIX}}
- -X {{.Env.MODULE_ROOT}}/ldflags.BuildPlatform={{.Env.BUILD_PLATFORM}}
- -X {{.Env.MODULE_ROOT}}/ldflags.GoVersion={{.Env.GO_VERSION}}
goos:
- linux
- darwin
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ clean-bins:

temporal-server:
@printf $(COLOR) "Build temporal-server with OS: $(GOOS), ARCH: $(GOARCH)..."
CGO_ENABLED=$(CGO_ENABLED) go build -ldflags "$(shell ./develop/scripts/go-build-ldflags.sh $(MODULE_ROOT)/ldflags)" -o temporal-server cmd/server/main.go
@./develop/scripts/create_build_info_data.sh
CGO_ENABLED=$(CGO_ENABLED) go build -o temporal-server cmd/server/main.go

tctl:
@printf $(COLOR) "Build tctl with OS: $(GOOS), ARCH: $(GOARCH)..."
Expand Down
56 changes: 34 additions & 22 deletions ldflags/ldflags.go → build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,46 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package ldflags
package build

import (
"runtime"
"embed"
"encoding/json"
"time"
)

var (
// This variables are overridden using ldflags at compile time. Example:
// $ go build -ldflags "-X go.temporal.io/server/ldflags.GitRevision=abcdef" ...

// GitRevision is the git revision associated with this build.
GitRevision = "unknown"

// GitBranch is the git branch associated with this build.
GitBranch = "unknown"
const (
buildInfoDataFile = "info/data.json"
)

// GitTag is the git tag associated with this build.
GitTag = "unknown"
type (
Info struct {
// GitRevision is the git revision associated with this build.
GitRevision string
// BuildTimeUnix is the seconds since epoch representing the date this build was created.
BuildTimeUnix int64
}
)

// BuildDate is the date this build was created.
BuildDate = "unknown"
var (
//go:embed info
buildInfoFs embed.FS
InfoData Info
)

// BuildTimeUnix is the seconds since epoch representing the date this build was created.
BuildTimeUnix = "0"
func init() {
InfoData = Info{
GitRevision: "unknown",
BuildTimeUnix: 0,
}

// BuildPlatform is the platform used to build.
BuildPlatform = "unknown"
buildInfoDataJson, err := buildInfoFs.ReadFile(buildInfoDataFile)
if err != nil {
return
}
_ = json.Unmarshal(buildInfoDataJson, &InfoData)
}

// GoVersion is the current runtime version.
GoVersion = runtime.Version()
)
func (i Info) BuildTime() time.Time {
return time.Unix(i.BuildTimeUnix, 0)
}
Empty file added build/info/empty
Empty file.
10 changes: 10 additions & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ import (
stdlog "log"
"os"
"path"
"runtime"
"strings"

"github.com/urfave/cli/v2"

"go.temporal.io/server/build"
"go.temporal.io/server/common/authorization"
"go.temporal.io/server/common/config"
"go.temporal.io/server/common/dynamicconfig"
Expand Down Expand Up @@ -129,6 +132,13 @@ func buildCLI() *cli.App {
}

logger := log.NewZapLogger(log.BuildZapLogger(cfg.Log))
logger.Info("Build info",
tag.Timestamp(build.InfoData.BuildTime()),
tag.NewStringTag("git-revision", build.InfoData.GitRevision),
tag.NewStringTag("platform", runtime.GOARCH),
tag.NewStringTag("go-version", runtime.Version()),
tag.NewStringTag("server-version", headers.ServerVersion),
)

var dynamicConfigClient dynamicconfig.Client
if cfg.DynamicConfigClient != nil {
Expand Down
37 changes: 13 additions & 24 deletions common/metrics/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@ package metrics

import (
"runtime"
"strconv"
"sync/atomic"
"time"

"github.com/uber-go/tally/v4"

"go.temporal.io/server/build"
"go.temporal.io/server/common/headers"
"go.temporal.io/server/common/log"
"go.temporal.io/server/ldflags"
)

const (
Expand Down Expand Up @@ -64,39 +63,29 @@ func NewRuntimeMetricsReporter(
logger log.Logger,
instanceID string,
) *RuntimeMetricsReporter {
const (
base = 10
bitSize = 64
)
if len(instanceID) > 0 {
scope = scope.Tagged(map[string]string{instance: instanceID})
}
var memstats runtime.MemStats
runtime.ReadMemStats(&memstats)
rReporter := &RuntimeMetricsReporter{

return &RuntimeMetricsReporter{
scope: scope,
reportInterval: reportInterval,
logger: logger,
lastNumGC: memstats.NumGC,
quit: make(chan struct{}),
buildTime: build.InfoData.BuildTime(),
buildInfoScope: scope.Tagged(
map[string]string{
gitRevisionTag: build.InfoData.GitRevision,
buildDateTag: build.InfoData.BuildTime().Format(time.RFC3339),
buildPlatformTag: runtime.GOARCH,
goVersionTag: runtime.Version(),
buildVersionTag: headers.ServerVersion,
},
),
}
rReporter.buildInfoScope = scope.Tagged(
map[string]string{
gitRevisionTag: ldflags.GitRevision,
gitBranchTag: ldflags.GitBranch,
gitTagTag: ldflags.GitTag,
buildDateTag: ldflags.BuildDate,
buildPlatformTag: ldflags.BuildPlatform,
goVersionTag: ldflags.GoVersion,
buildVersionTag: headers.ServerVersion,
},
)
sec, err := strconv.ParseInt(ldflags.BuildTimeUnix, base, bitSize)
if err != nil || sec < 0 {
sec = 0
}
rReporter.buildTime = time.Unix(sec, 0).UTC()
return rReporter
}

// report Sends runtime metrics to the local metrics collector.
Expand Down
2 changes: 0 additions & 2 deletions common/metrics/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ import (

const (
gitRevisionTag = "git_revision"
gitBranchTag = "git_branch"
buildDateTag = "build_date"
gitTagTag = "git_tag"
buildVersionTag = "build_version"
buildPlatformTag = "build_platform"
goVersionTag = "go_version"
Expand Down
10 changes: 10 additions & 0 deletions develop/scripts/create_build_info_data.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/sh

set -eu

build_info_data_file="build/info/data.json"

git_revision=$(git rev-parse --short HEAD) # "6cbfa2a3a"
build_time_unix=$(date '+%s') # seconds since epoch

echo '{"gitRevision":"'"${git_revision}"'","buildTimeUnix":'"${build_time_unix}"'}' > "${build_info_data_file}"
23 changes: 0 additions & 23 deletions develop/scripts/go-build-ldflags.sh

This file was deleted.

16 changes: 1 addition & 15 deletions develop/scripts/goreleaser.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,7 @@

set -eu

GIT_REVISION=$(git rev-parse --short HEAD)
GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
GIT_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo unknown)
BUILD_DATE=$(date '+%F-%T') # outputs something in this format 2017-08-21-18:58:45
BUILD_TS_UNIX=$(date '+%s') # second since epoch
BUILD_PLATFORM=$(go version | cut -d ' ' -f 4) # from "go version go1.15.3 linux/amd64"
GO_VERSION=$(go version | cut -d ' ' -f 3) # from "go version go1.15.3 linux/amd64"

export GIT_REVISION
export GIT_BRANCH
export GIT_TAG
export BUILD_DATE
export BUILD_TS_UNIX
export BUILD_PLATFORM
export GO_VERSION
./develop/scripts/create_build_info_data.sh

curl -sfL https://install.goreleaser.com/github.com/goreleaser/goreleaser.sh | sh

Expand Down

0 comments on commit 2a47cdf

Please sign in to comment.