Skip to content

Commit

Permalink
docker.bzl: Detect if local Docker image is used
Browse files Browse the repository at this point in the history
Signed-off-by: Eryk Szpotanski <[email protected]>
Signed-off-by: Grzegorz Latosinski <[email protected]>
Co-authored-by: Grzegorz Latosinski <[email protected]>
  • Loading branch information
eszpotanski and glatosinski committed Sep 10, 2024
1 parent 9408cde commit 2581714
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion docker.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "patch")

def _impl(repository_ctx):
image = "{}@sha256:{}".format(repository_ctx.attr.image, repository_ctx.attr.sha256)
if repository_ctx.attr.sha256 != "":
image = "{}@sha256:{}".format(repository_ctx.attr.image, repository_ctx.attr.sha256)
else:
image = repository_ctx.attr.image

python_name = "python3"
python = repository_ctx.which(python_name)
Expand All @@ -14,6 +17,26 @@ def _impl(repository_ctx):
if not docker:
fail("Failed to find {}.".format(docker_name))

if repository_ctx.attr.sha256 == "":
inspect = repository_ctx.execute([
docker,
"inspect",
"--type=image",
image,
])
if inspect.return_code != 0:
fail("Local image {} does not exist.".format(image, inspect.stderr), inspect.return_code)
repository_ctx.report_progress("Using local {}.".format(repository_ctx.attr.image))
else:
pull = repository_ctx.execute([
docker,
"pull",
image,
])
if pull.return_code != 0:
fail("Image {} cannot be pulled: {}".format(image, pull.stderr), pull.return_code)
repository_ctx.report_progress("Pulled {}.".format(repository_ctx.attr.image))

created = repository_ctx.execute(
[
docker,
Expand Down

0 comments on commit 2581714

Please sign in to comment.