feat(bazel): introduce a binary stamping feature (#22176)

This grabs version control metadata and makes it available in the build, eg. to put in the version field for released artifacts

PR Close #22176
This commit is contained in:
Alex Eagle
2018-02-13 08:20:35 -08:00
committed by Victor Berchet
parent a069e08354
commit bba65e0f41
5 changed files with 97 additions and 1 deletions

View File

@ -1 +1,9 @@
# Marker file indicating this folder is a Bazel package
# Executes the workspace_status_command and provides the result.
# See the section on stamping in docs/BAZEL.md
genrule(
name = "stamp_data",
outs = ["stamp_data.txt"],
cmd = "cat bazel-out/volatile-status.txt > $@",
stamp = True,
visibility = ["//:__subpackages__"],
)

View File

@ -21,6 +21,12 @@ build --symlink_prefix=dist/
# Performance: avoid stat'ing input files
build --watchfs
###############################
# Release support #
###############################
build --workspace_status_command=./tools/bazel_stamp_vars.sh
###############################
# Output #
###############################

42
tools/bazel_stamp_vars.sh Executable file
View File

@ -0,0 +1,42 @@
#!/usr/bin/env bash
# Generates the data used by the stamping feature in bazel.
# A genrule with stamp=1 can read the resulting file from bazel-out/volatile-status.txt
# See the section on stamping in docs/BAZEL.md
set -u -e -E -o pipefail
echo "Running: $0" >&2
function onError {
echo "Failed to execute: $0"
echo ""
}
# Setup crash trap
trap 'onError' ERR
echo BUILD_SCM_HASH $(git rev-parse HEAD)
if [[ "$(git tag)" == "" ]]; then
echo "No git tags found, can't stamp the build."
echo "Either fetch the tags:"
echo " git fetch git@github.com:angular/angular.git --tags"
echo "or build without stamping by giving an empty workspace_status_command:"
echo " bazel build --workspace_status_command= ..."
echo ""
fi
BUILD_SCM_VERSION_RAW=$(git describe --abbrev=7 --tags HEAD)
# Find out if there are any uncommitted local changes
# TODO(i): is it ok to use "--untracked-files=no" to ignore untracked files since they should not affect anything?
if [[ $(git status --untracked-files=no --porcelain) ]]; then LOCAL_CHANGES="true"; else LOCAL_CHANGES="false"; fi
echo BUILD_SCM_LOCAL_CHANGES ${LOCAL_CHANGES}
# Reformat `git describe` version string into a more semver-ish string
# From: 5.2.0-rc.0-57-g757f886
# To: 5.2.0-rc.0+57.sha-757f886
# Or: 5.2.0-rc.0+57.sha-757f886.with-local-changes
BUILD_SCM_VERSION="$(echo ${BUILD_SCM_VERSION_RAW} | sed -E 's/-([0-9]+)-g/+\1.sha-/g')""$( if [[ $LOCAL_CHANGES == "true" ]]; then echo ".with-local-changes"; fi)"
echo BUILD_SCM_VERSION ${BUILD_SCM_VERSION}