Skip to content

Commit

Permalink
build: return imageID when loading without docker driver
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <[email protected]>
  • Loading branch information
crazy-max committed Mar 1, 2022
1 parent f62c023 commit 0028c49
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ func Build(ctx context.Context, drivers []DriverInfo, opt map[string]Options, do
resp[k] = res[0]
respMu.Unlock()
if len(res) == 1 {
digest := res[0].ExporterResponse["containerimage.digest"]
digest := res[0].ExporterResponse[exptypes.ExporterImageDigestKey]
if opt.ImageIDFile != "" {
return ioutil.WriteFile(opt.ImageIDFile, []byte(digest), 0644)
}
Expand All @@ -774,7 +774,7 @@ func Build(ctx context.Context, drivers []DriverInfo, opt map[string]Options, do
descs := make([]specs.Descriptor, 0, len(res))

for _, r := range res {
s, ok := r.ExporterResponse["containerimage.digest"]
s, ok := r.ExporterResponse[exptypes.ExporterImageDigestKey]
if ok {
descs = append(descs, specs.Descriptor{
Digest: digest.Digest(s),
Expand Down Expand Up @@ -921,8 +921,9 @@ func Build(ctx context.Context, drivers []DriverInfo, opt map[string]Options, do
res[i] = rr

d := drivers[dp.driverIndex].Driver
if d.IsMobyDriver() {
for _, e := range so.Exports {

for _, e := range so.Exports {
if d.IsMobyDriver() {
if e.Type == "moby" && e.Attrs["push"] != "" {
if ok, _ := strconv.ParseBool(e.Attrs["push"]); ok {
pushNames = e.Attrs["name"]
Expand All @@ -939,6 +940,14 @@ func Build(ctx context.Context, drivers []DriverInfo, opt map[string]Options, do
}
}
}
} else if names, ok := e.Attrs["name"]; ok && e.Type == "docker" {
nameList := strings.Split(names, ",")
imageID, err := dockerImageID(ctx, d, nameList[0])
if err == nil && imageID != "" {
rr.ExporterResponse[exptypes.ExporterImageDigestKey] = imageID
} else if err != nil {
return err
}
}
}
return nil
Expand Down Expand Up @@ -1041,6 +1050,18 @@ func pushWithMoby(ctx context.Context, d driver.Driver, name string, l progress.
return nil
}

func dockerImageID(ctx context.Context, d driver.Driver, name string) (string, error) {
api := d.Config().DockerAPI
if api == nil {
return "", errors.Errorf("invalid empty Docker API reference") // should never happen
}
image, _, err := api.ImageInspectWithRaw(ctx, name)
if err != nil {
return "", err
}
return image.ID, nil
}

func createTempDockerfile(r io.Reader) (string, error) {
dir, err := ioutil.TempDir("", "dockerfile")
if err != nil {
Expand Down

0 comments on commit 0028c49

Please sign in to comment.