When configuring `docker_image` targets, would it ...
# general
h
When configuring
docker_image
targets, would it be useful/are there any plans to include a
version
property and for this to be available as a placeholder
{version}
for tagging? This would be useful for more complex tag formats e.g. if I wanted to add build and commit info into version tags like so for every docker image that would be built:
reg.address/docker/repo:v1.2-build5-fd6s82hr
It would also be useful to be able to set the
image_tags
in
pants.toml
if the format is consistent for an entire registry. This could then use the
{version}
placeholder (+ env vars) to create tag formats like:
Copy code
[docker.registries.reg0]
image_tags = ["{version}-build{BUILD_NUM}-{GIT_COMMIT}"]
Currently to do the above I’d have to set up a plugin or set that format for every
docker_image
target AFAIK. Are there any plans for something close to this or should I look to just write a plugin for now? 🙂
c
I think you could do that with a macro around
docker_image
today. <https://www.pantsbuild.org/docs/macros> snippet:
Copy code
commit_sha = env("CI_COMMIT_SHA")
    if commit_sha:
        image_tags.append(commit_sha)
        extra_build_args.append(f"COMMIT_SHA={commit_sha}")
    if env("CI_COMMIT_SHORT_SHA"):
        image_tags.append(env("CI_COMMIT_SHORT_SHA"))
    if env("CI_COMMIT_REF_PROTECTED") == "true":
        image_tags.append(f"protected-{env('CI_COMMIT_SHA')}")
    if env("CI_COMMIT_BRANCH") == "main":
        image_tags.append("latest")
c
Regarding
image_tags
in
pants.toml
see
extra_image_tags
here: https://www.pantsbuild.org/docs/tagging-docker-images#configuring-registries
h
I assumed that the “extra” implied that the way that tags should be set primarily is in the
docker_image
target
image_tags
. So I guess my second suggestion was more around best practice.
I think you could do that with a macro around
docker_image
today. <https://www.pantsbuild.org/docs/macros>
snippet:
``` commit_sha = env("CI_COMMIT_SHA")
if commit_sha:
image_tags.append(commit_sha)
extra_build_args.append(f"COMMIT_SHA={commit_sha}")
if env("CI_COMMIT_SHORT_SHA"):
image_tags.append(env("CI_COMMIT_SHORT_SHA"))
if env("CI_COMMIT_REF_PROTECTED") == "true":
image_tags.append(f"protected-{env('CI_COMMIT_SHA')}")
if env("CI_COMMIT_BRANCH") == "main":
image_tags.append("latest")```
Interesting, I’ll try that out. It would have to depend on registry selected which I’m not sure is possible but I’ll test it out, thanks.
c
The
extra
was meant to highlight the fact that it doesn’t replace any image tags on the
docker_image
but adds to them. Perhaps
additional_image_tags
would’ve been better, but that’s getting unwieldy.. 😛
h
Ah that makes sense. What is your opinion on the idea of setting image tags like I described? Since we can already pull info for placeholders from the image target like
{name}
c
you can set your own placeholders using build args (https://www.pantsbuild.org/docs/tagging-docker-images#using-env-vars-to-include-dynamic-data-in-tags), or do the interpolation your self using env vars in the BUILD file (using
env
) — so it is not clear to me what beyond that you are suggesting (besides easier to use syntax?)
h
I was suggesting two things: 1, having another dynamic(?) placeholder (to be used in pants.toml) that comes from the
docker_image
target, in the same way that
{name}
does but for the version of the image. But currently
docker_image
doesn’t have a version property, so 2 would be adding that version property. I was wondering if this sounds like anything would be possible in future, basically. As I’m happy to write a plugin that does it for me for now if need be. :)
I was under the impression that build args/env vars wouldn’t work for this but I may be wrong. I’ll test that out
c
there is https://www.pantsbuild.org/docs/reference-docker_image#codeextra_build_argscode
Copy code
docker_image(
  extra_build_args=["VERSION=1.2"],
  image_tags=["{build_args.VERSION]"],
)
so, it’s not in a dedicated field, but it is set on the target..