Hello - I am interested in dynamically creating do...
# general
r
Hello - I am interested in dynamically creating docker tags. I can generate them using git commands .. something like
"$(git log -1 --date=unix --format=%cd)-$(git rev-parse --short HEAD)"
. I see there is a note about this requiring custom plugin implementation (https://www.pantsbuild.org/docs/tagging-docker-images#tagging-images). Is there any existing examples or other places I should look before attempting to implement something? Thanks!
h
@curved-television-6568 you're the expert here... 🙂
r
Sorry should have done a better search before asking 🙂 I will catch up on this thread too. https://pantsbuild.slack.com/archives/C046T6T9U/p1637936640282100
That thread looks like an easy solution for me. I'll try out the build arg solution and post back if I run into issues.
👌 2
Works perfectly
❤️ 1
👍 1
c
Great. Should lift the gist of that thread to the docs before it is lost in space :)
👍 1
e
I've been doing this by adding the following to my `pants.toml`:
Copy code
[docker]
build_args = [
    # To set when packaging:
    #   COMMIT_SHA="$(git rev-parse --short HEAD)"
    #   export PANTS_DOCKER_BUILD_ARGS="['COMMIT_SHA=$COMMIT_SHA']"
    "COMMIT_SHA=SETME"
]
macros.py
(or individually for each
docker_image
target):
Copy code
def tagged_docker_image(**kwargs):
   ...
   kwargs["image_tags"] = [
        # Pant"s doesn't expose the commit sha and we need to set PANTS_DOCKER_BUILD_ARGS="['COMMIT_SHA=$(git rev-parse --short HEAD)']" when this is packaged.
        # <https://github.com/pantsbuild/pants/issues/13724>
        "{build_args.COMMIT_SHA}",
    ]
    docker_image(**kwargs)
And then in when packaging (in a GHA for us):
Copy code
COMMIT_SHA="$(git rev-parse --short HEAD)"
export PANTS_DOCKER_BUILD_ARGS="['COMMIT_SHA=$COMMIT_SHA']"

...
./pants package $target
👍 1
r
Cool, similarly I am using
Copy code
DOCKER_TAG=$(./build-support/docker-tag.sh) ./pants publish docker/geminus-ui
Where the
docker-tag.sh
script constructs and echos the tag based on the git sha.
macros.py
is a nice touch to remove duplication 👍