【问题标题】:How to tag container_image dynamically?如何动态标记 container_image?
【发布时间】:2022-01-05 23:21:23
【问题描述】:

我对 Bazel 的世界有点陌生。 我的目标是标记图像并将其推送到注册表,但使用动态标记。 没有 Bazel,我曾经用 git commit SHA(6-7 chars) 为我的版本添加后缀,例如。 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 (-

提前致谢

【问题讨论】:

    标签: docker bazel bazel-rules


    【解决方案1】:

    你可以通过stamping得到git commit,也就是supported by rules_docker。比如把这个放在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 $2}' >> $@",
    )
    
    container_push(
        <same as before>
        tag_file = ":my_tag_file",
    )
    

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

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

    【讨论】:

      【解决方案2】:

      感谢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}",
      )
      

      【讨论】:

        【解决方案3】:

        对于快照,我更推荐基于源文件的 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 $$2}' || :)
                    POSTFIX=""" + postfix + """
                    if [[ -z "$$STABLE_RELEASE_VERSION" ]]; then
                      SHA256=$$(sha256sum $(location """ + " ) $(location ".join(targets) + """) | awk '{print $$1;}' | sha256sum | awk '{print $$1;}')
                      echo $$SHA256-SNAPSHOT > $(OUTS);
                    else
                      echo $$STABLE_RELEASE_VERSION$$POSTFIX > $(OUTS);
                    fi
                    """,
                **kwargs
            )
        

        【讨论】:

          猜你喜欢
          • 2018-03-30
          • 1970-01-01
          • 2019-06-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-12-28
          • 1970-01-01
          • 2017-10-12
          相关资源
          最近更新 更多