Skip to content

Commit

Permalink
ci: stamp our releases with a stable key
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Eagle authored and alexeagle committed Sep 7, 2020
1 parent 58e5bd4 commit f7819fc
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 17 deletions.
8 changes: 3 additions & 5 deletions docs/stamping.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,7 @@ set -u -e -o pipefail
# Call the script with argument "pack" or "publish"
readonly NPM_COMMAND=${1:-publish}
# Don't rely on $PATH to have the right version
readonly BAZEL_BIN=./node_modules/.bin/bazel
# Use a new output_base so we get a clean build
# Bazel can't know if the git metadata changed
readonly TMP=$(mktemp -d -t bazel-release.XXXXXXX)
readonly BAZEL="$BAZEL_BIN --output_base=$TMP"
readonly BAZEL=./node_modules/.bin/bazel
# Find all the npm packages in the repo
readonly PKG_NPM_LABELS=`$BAZEL query --output=label 'kind("pkg_npm", //...)'`
# Build them in one command to maximize parallelism
Expand All @@ -95,5 +91,7 @@ done
```

See https://www.kchodorow.com/blog/2017/03/27/stamping-your-builds/ for more background.
Make sure you use a "STABLE_" status key, or else Bazel may use a cached npm artifact rather than
building a new one with your current version info.

[bazel_stamp_vars in Angular]: https://github.com/angular/angular/blob/master/tools/bazel_stamp_vars.sh
2 changes: 1 addition & 1 deletion internal/pkg_npm/pkg_npm.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def create_package(ctx, deps_files, nested_packages):
# see https://github.com/bazelbuild/rules_nodejs/issues/2158 for removal
substitutions = dict(**ctx.attr.substitutions)
if stamp and ctx.attr.replace_with_version:
substitutions[ctx.attr.replace_with_version] = "{BUILD_SCM_VERSION}"
substitutions.setdefault(ctx.attr.replace_with_version, "{BUILD_SCM_VERSION}")

args = ctx.actions.args()
inputs = ctx.files.srcs + deps_files + nested_packages
Expand Down
2 changes: 2 additions & 0 deletions scripts/current_version.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env bash
# Used with Bazel's stamping feature
# TODO: remove this one as part of #2158
echo BUILD_SCM_VERSION $(git describe --abbrev=7 --tags HEAD)
echo STABLE_BUILD_SCM_VERSION $(git describe --abbrev=7 --tags HEAD)
7 changes: 1 addition & 6 deletions scripts/publish_release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@ set -u -e -o pipefail

readonly NPM_COMMAND=${1:-publish}
readonly NPM_TAG=${2:-latest}
readonly BAZEL_BIN=./node_modules/.bin/bazel

# Use a new output_base so we get a clean build
# Bazel can't know if the git metadata changed
readonly TMP=$(mktemp -d -t bazel-release.XXXXXXX)
readonly BAZEL="$BAZEL_BIN --output_base=$TMP"
readonly BAZEL=./node_modules/.bin/bazel
readonly PKG_NPM_LABELS=`$BAZEL query --output=label 'kind("pkg_npm rule", //packages/...) - attr("tags", "\[.*do-not-publish.*\]", //packages/...)'`

$BAZEL build --config=release $PKG_NPM_LABELS
Expand Down
16 changes: 11 additions & 5 deletions tools/defaults.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,22 @@ def pkg_npm(**kwargs):
"//examples:__pkg__",
])

# Default substitutions to scrub things like skylib references
substitutions = kwargs.pop("substitutions", _COMMON_REPLACEMENTS)

pkg = native.package_name().split("/")[-1]

# Default substitutions to scrub things like skylib references
substitutions = dict(kwargs.pop("substitutions", _COMMON_REPLACEMENTS), **{
"//packages/%s" % pkg: "//@bazel/%s" % pkg,
})
stamped_substitutions = dict(substitutions, **{
"0.0.0-PLACEHOLDER": "{STABLE_BUILD_SCM_VERSION}",
})

# Finally call through to the rule with our defaults set
_pkg_npm(
deps = deps,
substitutions = dict(substitutions, **{
"//packages/%s" % pkg: "//@bazel/%s" % pkg,
substitutions = select({
"@build_bazel_rules_nodejs//internal:stamp": stamped_substitutions,
"//conditions:default": substitutions,
}),
visibility = visibility,
**kwargs
Expand Down

0 comments on commit f7819fc

Please sign in to comment.