如何动态标记container_image?

How to tag container_image dynamically?

我对 Bazel 世界有点陌生。 我的目标是标记图像并将其推送到注册表,但使用动态标记。 没有 Bazel,我曾经在我的版本后缀 git 提交 SHA(6-7 个字符),例如。 1.0.0-a68hg4 我想对 container_push 规则做同样的事情。

container_push(
    name = "publish",
    format = "Docker",
    image = ":image",
    registry = DOCKER_REGISTRY,
    repository = "app1",
    skip_unchanged_digest = True,
    tag_file = "image.json.sha256",
)

here 复制的代码。 我可以使用 SHA 使我的标签在构建之间独一无二,但我可以加入字符串来制作我想要的东西吗? IE。 1.0.0-a68h4 (-

提前致谢

您可以通过 stamping, which is supported by rules_docker 获得 git 提交。例如,将其放入 workspace_status.sh:

#!/bin/bash
echo "STABLE_GIT_COMMIT $(git rev-parse HEAD)"

然后如果你使用 --workspace_status_command=workspace_status.sh 构建,你可以写 tag = "something-{STABLE_GIT_COMMIT}"(并在 container_push 上设置 stamp = True)。如果需要,git describe 而不是 git rev-parse 可能有助于包含当前标签或分支的名称。

如果你想把它和 sha256 结合起来,我会使用 genrule 来创建这样的文件:

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 }' >> $@",
)

container_push(
    <same as before>
    tag_file = ":my_tag_file",
)

在单独的文件中编写脚本(将其放入 tools 并使用 $(location) 将其定位到 运行)将使字符串操作比像这样将其全部内联在 cmd 属性中。

如果要添加任意标识字符串作为标记的一部分,bazel 命令行上的 --embed_label 将在 stable-status.txt 中设置 BUILD_EMBED_LABEL 键。

感谢 Brain Silverman 的详细回答。

如果有人正在寻找简单的解决方案,这里就是。

build.sh(将 docker 图像推送到注册表的脚本)

version="1.0.0"

docker login -u "$USERNAME" -p "$PASSWORD" "$REGISTRY"

bazel run --workspace_status_command="echo VERSION $version-$(git rev-parse HEAD | cut -c 1-8)" //docker: publish

BUILD.bazel

container_push(
    name = "publish",
    format = "Docker",
    image = ":image",
    registry = DOCKER_REGISTRY,
    repository = "app1",
    skip_unchanged_digest = True,
    tag_file = "{VERSION}",
)

对于快照,我更推荐基于源文件 SHA256 的版本控制。使用这种方法,只有当图像内容真正发生变化时,才会发布新标签。

看看https://github.com/mgosk/bazel-scala-example/blob/master/example-bin/BUILD

# https://github.com/mgosk/bazel-scala-example/blob/master/example-bin/BUILD
git_tag_with_sha_multitargets(
    name = "version",
    targets = [
        ":image",
        "@java_base//image",
    ],
)

container_push(
    name = "image-push",
    format = "Docker",
    image = ":image",
    registry = "docker.io",
    repository = "mgosk/example-bin",
    tag_file = ":version",
)

# https://github.com/mgosk/bazel-scala-example/blob/master/tools/version.bzl
def git_tag_with_sha_multitargets(name, targets, postfix = "", **kwargs):
    super_stable_status = "//:super_stable_status"
    native.genrule(
        name = name,
        srcs = targets + [super_stable_status],
        outs = [name + ".txt"],
        cmd = """
            STABLE_RELEASE_VERSION=$$(cat $(location """ + super_stable_status + """) | grep 'STABLE_RELEASE_VERSION' | awk '{print $}' || :)
            POSTFIX=""" + postfix + """
            if [[ -z "$$STABLE_RELEASE_VERSION" ]]; then
              SHA256=$$(sha256sum $(location """ + " ) $(location ".join(targets) + """) | awk '{print $;}' | sha256sum | awk '{print $;}')
              echo $$SHA256-SNAPSHOT > $(OUTS);
            else
              echo $$STABLE_RELEASE_VERSION$$POSTFIX > $(OUTS);
            fi
            """,
        **kwargs
    )