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

Set labels when building image from Dockerfile #1097

Merged
merged 3 commits into from
Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions repo2docker/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,14 @@ def get_argparser():

argparser.add_argument("--appendix", type=str, help=Repo2Docker.appendix.help)

argparser.add_argument(
"--label",
dest="labels",
action="append",
help="Extra label to set on the image, in form name=value",
default=[],
)

argparser.add_argument("--subdir", type=str, help=Repo2Docker.subdir.help)

argparser.add_argument(
Expand Down Expand Up @@ -246,6 +254,13 @@ def make_r2d(argv=None):
if args.appendix:
r2d.appendix = args.appendix

for l in args.labels:
if "=" in l:
Copy link
Member

Choose a reason for hiding this comment

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

FWIW, this is a perfect application of str.partition:

key, _, value = l.partition("=")

key, val = l.split("=", 1)
r2d.labels[key] = val
else:
r2d.labels[l] = ""

r2d.repo = args.repo
r2d.ref = args.ref

Expand Down
13 changes: 13 additions & 0 deletions repo2docker/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,17 @@ def _user_name_default(self):
""",
)

labels = Dict(
{},
help="""
Extra labels to set on the final image.

Each Label is a key-value pair, with the key being the name of the label
and the value its value.
""",
config=True,
)

json_logs = Bool(
False,
help="""
Expand Down Expand Up @@ -747,6 +758,8 @@ def build(self):
picked_buildpack.labels["repo2docker.repo"] = repo_label
picked_buildpack.labels["repo2docker.ref"] = self.ref

picked_buildpack.labels.update(self.labels)

if self.dry_run:
print(picked_buildpack.render())
else:
Expand Down
1 change: 1 addition & 0 deletions repo2docker/buildpacks/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def build(
buildargs=build_args,
container_limits=limits,
cache_from=cache_from,
labels=self.get_labels(),
)

build_kwargs.update(extra_build_kwargs)
Expand Down
2 changes: 2 additions & 0 deletions repo2docker/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def build(
dockerfile="",
fileobj=None,
path="",
labels=None,
**kwargs,
):
return self._apiclient.build(
Expand All @@ -87,6 +88,7 @@ def build(
dockerfile=dockerfile,
fileobj=fileobj,
path=path,
labels=labels,
**kwargs,
)

Expand Down
3 changes: 3 additions & 0 deletions repo2docker/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ def build(
dockerfile="",
fileobj=None,
path="",
labels=None,
**kwargs,
):
"""
Expand Down Expand Up @@ -204,6 +205,8 @@ def build(
A tar file-like object containing the build context
path : str
path to the Dockerfile
labels : dict
Dictionary of labels to set on the image

Returns
-------
Expand Down