From 258171464b56d1f5304fb880be44d770a21a714c Mon Sep 17 00:00:00 2001 From: Eryk Szpotanski Date: Tue, 3 Sep 2024 16:25:19 +0200 Subject: [PATCH] docker.bzl: Detect if local Docker image is used Signed-off-by: Eryk Szpotanski Signed-off-by: Grzegorz Latosinski Co-authored-by: Grzegorz Latosinski --- docker.bzl | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/docker.bzl b/docker.bzl index dee5327a..aa3d9870 100644 --- a/docker.bzl +++ b/docker.bzl @@ -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) @@ -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,