Skip to content
/ go-stamp Public

Stamp adds version information to a Bazel-built Go library.

License

Notifications You must be signed in to change notification settings

gebn/go-stamp

Repository files navigation

Stamp

CI Go Reference Go Report Card

Stamp exposes information about the build (user, host, time) and code version (description, commit, branch) at runtime.

Configuration

To include the library, add the following to your WORKSPACE:

load("@bazel_gazelle//:deps.bzl", "go_repository")
go_repository(
    name = "com_github_gebn_go_stamp_v2",
    tag = "v2.2.1",
    importpath = "github.com/gebn/go-stamp/v2",
)

Stamp uses a combination of workspace_status_command and x_defs to substitute values of Go vars at link time. Unfortunately, there does not currently appear to be a way to add to the workspace status from Starlark, so this must be manually configured.

First, create an executable script with the following contents somewhere in your workspace, e.g. bin/workspace_status (if you already have a workspace status script, you just need to add the three echo lines to it). N.B. due to this issue, having Bazel execute a script in the workspace root is awkward.

#!/bin/bash

set -o errexit
set -o nounset
set -o pipefail

echo "STABLE_STAMP_COMMIT $(git rev-parse HEAD)"
echo "STABLE_STAMP_BRANCH $(git rev-parse --abbrev-ref HEAD)"  # will be HEAD in detached HEAD state
echo "STABLE_STAMP_VERSION $(git describe --always --tags --dirty)"

Then add the following line to .bazelrc to execute the script during the build:

build --workspace_status_command=bin/workspace_status --stamp

You can test everything is working by calling Summary() in your project, which summarises all information gathered by this library:

package main

import (
    "fmt"

    "github.com/gebn/go-stamp/v2"
)

func main() {
    fmt.Println(stamp.Summary())
}

This should print something similar to:

v1.0.0 (709d67c5f0563c685838312cb33a2a92ab1788f5, master), built with go1.18.1 by george@dev on 2022-04-23T18:29:58+01:00

See examples for more detail. vanilla demos using this library without Bazel.

About

Stamp adds version information to a Bazel-built Go library.

Resources

License

Stars

Watchers

Forks

Packages

No packages published