Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docker image version tag #13097

Merged
merged 6 commits into from
Oct 5, 2021
Merged

Docker image version tag #13097

merged 6 commits into from
Oct 5, 2021

Conversation

kaos
Copy link
Member

@kaos kaos commented Oct 4, 2021

In order to keep the BUILD files DRY, support picking up the Docker image version to be built, from other sources rather than require a hard coded value in the BUILD file.

First up is support for looking at the Dockerfile FROM commands to glean possible version tags from them.

Example:

docker_image(name="myimage", version="{baseimage.tag}")
FROM upstream:1.2-feat
...

This would then produce myimage:1.2-feat.

As more format strings are introduced, so is the potential for users to shot them selves in the foot. So I've added some improved error feedback in case the version or image name template format strings blow up, that should help pinpoint the mistake and present possible solutions.

docker_image(
    name="test-example",
    version="1.2.5-{typo}",
    dependencies=["testprojects/src/python/hello/main"],
)
$ ./pants --no-print-stacktrace package testprojects/src/python/docker:test-example
12:09:17.29 [INFO] Initialization options changed: reinitializing scheduler...
12:09:17.99 [INFO] Scheduler initialized.
12:09:22.38 [ERROR] Exception caught: (pants.engine.internals.scheduler.ExecutionError)
[...]
pants.engine.internals.scheduler.ExecutionError: 1 Exception encountered:

  DockerBuildError: Error in testprojects/src/python/docker:test-example: Invalid format string for the `docker_image(version)` field: '1.2.5-{typo}'.

The key 'typo' is unknown. Try with one of: baseimage, stage0.

kaos added 3 commits October 4, 2021 11:50
…ions.

# Rust tests and lints will be skipped. Delete if not intended.
[ci skip-rust]

# Building wheels and fs_util will be skipped. Delete if not intended.
[ci skip-build-wheels]
# Rust tests and lints will be skipped. Delete if not intended.
[ci skip-rust]

# Building wheels and fs_util will be skipped. Delete if not intended.
[ci skip-build-wheels]
# Rust tests and lints will be skipped. Delete if not intended.
[ci skip-rust]

# Building wheels and fs_util will be skipped. Delete if not intended.
[ci skip-build-wheels]
@kaos kaos mentioned this pull request Oct 4, 2021
@kaos kaos requested a review from benjyw October 4, 2021 14:37
Copy link
Contributor

@benjyw benjyw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is neat! Not sure how generally useful it is to use the same version as a base image, but I can see uses for it.

)
except (KeyError, ValueError) as e:
msg = (
"Invalid format string for the `docker_image(version)` field: "
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

People might not know what you mean by "docker_image(version) field". I'd go with "for the version field of the docker_image target at {self.address.spec}`

Copy link
Member Author

@kaos kaos Oct 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have the address at that point, it is added further up the call stack. So the message becomes something like
"Error in {address}: Invalid format string for the version field of the docker_image target."

OK? Otherwise I can pass the address to image_names() for the sake of the error message..

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  DockerBuildError: Error in testprojects/src/python/docker:test-example: Invalid format string for the `version` field of the docker_image target: '1.2.5-{typo}'.

The key 'typo' is unknown. Try with one of: baseimage, stage0.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doh. Dummy, of course I have the address... 🤦🏽

@kaos
Copy link
Member Author

kaos commented Oct 4, 2021

Not sure how generally useful it is to use the same version as a base image

That is likely most useful only when mirroring images from one registry to another.. but then it helps avoiding typos! :D

Plan is to add support for fishing out the version value of an embedded python_artifact etc..

kaos added 3 commits October 4, 2021 18:45
# Rust tests and lints will be skipped. Delete if not intended.
[ci skip-rust]

# Building wheels and fs_util will be skipped. Delete if not intended.
[ci skip-build-wheels]
# Rust tests and lints will be skipped. Delete if not intended.
[ci skip-rust]

# Building wheels and fs_util will be skipped. Delete if not intended.
[ci skip-build-wheels]
@kaos kaos merged commit abb022b into pantsbuild:main Oct 5, 2021
@kaos kaos deleted the docker_image_version branch October 5, 2021 10:40
@kaos kaos changed the title Docker image version Docker image version tag Oct 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants