I am a bit new to the Bazel world.
My goal is to tag and push images to the registry but with a dynamic tag.
Without Bazel, I used to suffix my version with git commit SHA(6-7 chars), ex. 1.0.0-a68hg4
I want to do the same with the container_push rule.
container_push(
name = "publish",
format = "Docker",
image = ":image",
registry = DOCKER_REGISTRY,
repository = "app1",
skip_unchanged_digest = True,
tag_file = "image.json.sha256",
)
code copied from here.
I can use SHA which makes my tag unique between builds, but can I join strings to make something as I want. I.e. 1.0.0-a68h4
(<a_const_str>-<SHA256_6_char>
Thanks in advance
3
Answers
Thanks to
Brain Silverman
for the detailed answer.If anyone is looking for an easy solution, here it is.
build.sh
(script to push docker images to registry)BUILD.bazel
You can get the git commit via stamping, which is supported by rules_docker. For example, put this in
workspace_status.sh
:Then if you build with
--workspace_status_command=workspace_status.sh
you can writetag = "something-{STABLE_GIT_COMMIT}"
(and setstamp = True
on thecontainer_push
).git describe
instead ofgit rev-parse
could be useful to include the name of the current tag or branch if you want that.If you want to combine that and the sha256, I’d use a genrule to create a file like this:
Writing a script in a separate file (put it in
tools
and use$(location)
to get the location of it to run) will make the string manipulation easier to read than putting it all inline in thecmd
attribute like this.If you want to add an arbitrary identifying string as part of the tag, –embed_label on the bazel command line will set the
BUILD_EMBED_LABEL
key in stable-status.txt.For snapshots I rather recommend versioning based on SHA256 of source files. With this approach new tag will be published only when content of image really changes.
Take a look on https://github.com/mgosk/bazel-scala-example/blob/master/example-bin/BUILD