Skip to content
This repository was archived by the owner on Mar 11, 2023. It is now read-only.

Commit b607d6d

Browse files
committed
Search repo digests for correct repo
If the same image is pushed to multiple repos, each repo may compute a different digest (seriously). So, search through the list and find the one that matches, and use *that* as the repo digest. This typically comes up when creating a repo (e.g. an ECR repo) in the same Pulumi program as pushing to it, and then `pulumi destroy && pulumi up` without changing the Docker image will result in a local Docker image that has repo digest for both the old deleted repo and the new repo.
1 parent 9dbc7a8 commit b607d6d

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

cmd/pulumi-resource-docker-buildkit/main.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ func (k *dockerBuildkitProvider) dockerBuild(
219219
}
220220
applyDefaults(inputs)
221221
name := inputs["name"].StringValue()
222+
baseName := strings.Split(name, ":")[0]
222223
context := inputs["context"].StringValue()
223224
dockerfile := inputs["dockerfile"].StringValue()
224225
registry := inputs["registry"].ObjectValue()
@@ -259,10 +260,21 @@ func (k *dockerBuildkitProvider) dockerBuild(
259260
return nil, fmt.Errorf("docker build failed: %w", err)
260261
}
261262

262-
cmd = exec.Command("docker", "inspect", name, "-f", "{{index .RepoDigests 0}}")
263-
repoDigest, err := cmd.CombinedOutput()
263+
cmd = exec.Command("docker", "inspect", name, "-f", `{{join .RepoDigests "\n"}}`)
264+
repoDigests, err := cmd.CombinedOutput()
264265
if err != nil {
265-
return nil, fmt.Errorf("docker inspect failed: %s: %s", err, string(repoDigest))
266+
return nil, fmt.Errorf("docker inspect failed: %s: %s", err, string(repoDigests))
267+
}
268+
var repoDigest string
269+
for _, line := range strings.Split(string(repoDigests), "\n") {
270+
repo := strings.Split(line, "@")[0]
271+
if repo == baseName {
272+
repoDigest = line
273+
break
274+
}
275+
}
276+
if repoDigest == "" {
277+
return nil, fmt.Errorf("failed to find repo digest in docker inspect output: %s", repoDigests)
266278
}
267279

268280
outputs := map[string]interface{}{
@@ -271,7 +283,7 @@ func (k *dockerBuildkitProvider) dockerBuild(
271283
"name": name,
272284
"platforms": platforms,
273285
"contextDigest": contextDigest,
274-
"repoDigest": strings.TrimSpace(string(repoDigest)),
286+
"repoDigest": repoDigest,
275287
"registryServer": registry["server"].StringValue(),
276288
}
277289
return plugin.MarshalProperties(

examples/aws-python/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ def get_registry_info(registry_id):
2020
name=repo.repository_url,
2121
registry=repo.registry_id.apply(get_registry_info),
2222
)
23-
pulumi.export("image", image.image_digest)
23+
pulumi.export("image", image.repo_digest)
2424

0 commit comments

Comments
 (0)