1

I would like to version build artefacts with build number for CI passed to bazel via workspace_status_command. Sometimes I would like to include build number to the name of the artefact.

Is there a way how do I access ctx when writing a macro(as I was trying to use ctx.info_file)? So far it seems that I am able to access such info just in new rule when creating a new rule which in this case is a bit awkward.

I guess that having a build number or similar info is pretty common use case so I wonder if thre is a simpler way how to access such info.

jaksky
  • 3,305
  • 4
  • 35
  • 68

2 Answers2

1

No, you really need to define a custom rule to be able to consume information passed from workspace_status_command through info_file and version_file file and even then you cannot just access it's values from Starlark, you can pass the file to your tooling (wrapper) and process the inputs there. After all, (build) rules do not execute anything, they emit actions to be executed at a later phase.

Be careful though, because if you depend on info_file (STABLE_* entries), changes to the file invalidate targets depending on it. For something like CI build number, it's usually not what you want and version_file is more likely what you are after. You may want to record the id, but you usually do not want to rebuild stuff just because the build ID has changed (it's a new CI run). However, even simple inclusion of IDs could be considered problematic, if you want your results to be completely reproducible.

Having variable artifact names is a whole new problem and there would be good reasons why not to. But generally since as proposed the name would be decided during execution of actions (reading in version_file in your tool), you're past the analysis phase to decide what comes out of the action. The only way I am currently aware of (that is for out of tree source of variable input, you can of course always define a Starlark variable and load it from your BUILD file) to be able to do that is to use tree artifacts (using declare_directory in your rule.

Ondrej K.
  • 8,841
  • 11
  • 24
  • 39
  • Thx, come to the same conclusions – jaksky Sep 23 '19 at 07:32
  • One cannot even fake bazel out with `STABLE_XXX` key value pairs in the `workspace_status_command` output because so long as the values are variable (eg via build number etc) the rebuild/retest will be triggered accordingly right? It would be nice for bazel to have some escape mechanism to take in variable metadata that has no influence on the outcome. I'm thinking this information must be incorporated in some post processing step once the bazel command has exited. Maybe some kind of rule that skips the hashing step entirely, I'm not too sure entirely. – jxramos Jun 22 '22 at 04:09
  • actually it looks like there's some functionality with this so called **stamping** business: *Bazel only rebuilds an artifact if the stable stamp or one of the declared inputs changes. Otherwise you can get a cache hit, with a stale value of a volatile stamp.* https://www.aspect.dev/blog/stamping-bazel-builds-with-selective-delivery – jxramos Jun 22 '22 at 04:34
  • This other answer is worth linking here: https://stackoverflow.com/a/59012944/1330381 – jxramos Jun 22 '22 at 04:41
0

You cannot access the value at load or analysis time (e.g. in a macro).

It can be accessed at execution time.

def _my_rule_impl(ctx):
  output = ctx.actions.declare_file("%s.txt" % ctx.attr.name)
  
  ctx.actions.run_shell(
    args = [ctx.info_file.path, output.path],
    command = 'grep -oP "(?<=STABLE_BUILD_NUMBER=).*" "$1" > "$2"',
    inputs = [ctx.info_file],
    outputs = [output]
  )

  return [DefaultInfo(files = depset([output]))]

my_rule = rule(
  implementation = _my_rule_impl,
)

The workspace status variables beginning with STABLE_ and BUILD_EMBED_LABEL are in ctx.info_file and are included in the cache key. Others are in ctx.version_file, which is excluded from the cache key.


If you want access to it at analysis time, you can use a build setting.

If you want access to it at load time, you can write a Starlark file.

BUILD_NUMBER = ""
echo BUILD_NUMBER="$BUILD_NUMBER" > vars.bzl
Paul Draper
  • 78,542
  • 46
  • 206
  • 285