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
>Solution :
You can get the git commit via stamping, which is supported by rules_docker. For example, put this in workspace_status.sh:
#!/bin/bash
echo "STABLE_GIT_COMMIT $(git rev-parse HEAD)"
Then if you build with --workspace_status_command=workspace_status.sh you can write tag = "something-{STABLE_GIT_COMMIT}" (and set stamp = True on the container_push). git describe instead of git 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:
genrule(
name = "create_tag_file",
srcs = [
"image.json.sha256",
],
stamp = True,
outs = [ "my_tag_file" ],
cmd = "cat $(location image.json.sha256) > $@ && cat bazel-out/volatile-status.txt | grep STABLE_GIT_COMMIT | awk '{print $2}' >> $@",
)
container_push(
<same as before>
tag_file = ":my_tag_file",
)
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 the cmd 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.