skip to Main Content

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


  1. Chosen as BEST ANSWER

    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)

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

  2. 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.

    Login or Signup to reply.
  3. 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

    # 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
        )
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search