Merge pull request #1395 from lugeng/fix-load-metadata

dockerfile build:  fix not exit when meet error in load config metadata
v0.8
Tõnis Tiigi 2020-11-17 00:30:06 -08:00 committed by GitHub
commit 2aa3e0bd42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 30 deletions

View File

@ -249,33 +249,34 @@ func Dockerfile2LLB(ctx context.Context, dt []byte, opt ConvertOpt) (*llb.State,
ResolveMode: opt.ImageResolveMode.String(), ResolveMode: opt.ImageResolveMode.String(),
LogName: fmt.Sprintf("%s load metadata for %s", prefix, d.stage.BaseName), LogName: fmt.Sprintf("%s load metadata for %s", prefix, d.stage.BaseName),
}) })
if err == nil { // handle the error while builder is actually running if err != nil {
var img Image return err
if err := json.Unmarshal(dt, &img); err != nil { }
var img Image
if err := json.Unmarshal(dt, &img); err != nil {
return err
}
img.Created = nil
// if there is no explicit target platform, try to match based on image config
if d.platform == nil && platformOpt.implicitTarget {
p := autoDetectPlatform(img, *platform, platformOpt.buildPlatforms)
platform = &p
}
d.image = img
if dgst != "" {
ref, err = reference.WithDigest(ref, dgst)
if err != nil {
return err return err
} }
img.Created = nil }
// if there is no explicit target platform, try to match based on image config d.stage.BaseName = ref.String()
if d.platform == nil && platformOpt.implicitTarget { if len(img.RootFS.DiffIDs) == 0 {
p := autoDetectPlatform(img, *platform, platformOpt.buildPlatforms) isScratch = true
platform = &p // schema1 images can't return diffIDs so double check :(
} for _, h := range img.History {
d.image = img if !h.EmptyLayer {
if dgst != "" { isScratch = false
ref, err = reference.WithDigest(ref, dgst) break
if err != nil {
return err
}
}
d.stage.BaseName = ref.String()
if len(img.RootFS.DiffIDs) == 0 {
isScratch = true
// schema1 images can't return diffIDs so double check :(
for _, h := range img.History {
if !h.EmptyLayer {
isScratch = false
break
}
} }
} }
} }

View File

@ -26,7 +26,7 @@ func toEnvMap(args []instructions.KeyValuePairOptional, env []string) map[string
func TestDockerfileParsing(t *testing.T) { func TestDockerfileParsing(t *testing.T) {
t.Parallel() t.Parallel()
df := `FROM busybox df := `FROM scratch
ENV FOO bar ENV FOO bar
COPY f1 f2 /sub/ COPY f1 f2 /sub/
RUN ls -l RUN ls -l
@ -34,7 +34,7 @@ RUN ls -l
_, _, err := Dockerfile2LLB(appcontext.Context(), []byte(df), ConvertOpt{}) _, _, err := Dockerfile2LLB(appcontext.Context(), []byte(df), ConvertOpt{})
assert.NoError(t, err) assert.NoError(t, err)
df = `FROM busybox AS foo df = `FROM scratch AS foo
ENV FOO bar ENV FOO bar
FROM foo FROM foo
COPY --from=foo f1 / COPY --from=foo f1 /
@ -43,7 +43,7 @@ COPY --from=0 f2 /
_, _, err = Dockerfile2LLB(appcontext.Context(), []byte(df), ConvertOpt{}) _, _, err = Dockerfile2LLB(appcontext.Context(), []byte(df), ConvertOpt{})
assert.NoError(t, err) assert.NoError(t, err)
df = `FROM busybox AS foo df = `FROM scratch AS foo
ENV FOO bar ENV FOO bar
FROM foo FROM foo
COPY --from=foo f1 / COPY --from=foo f1 /
@ -59,13 +59,13 @@ COPY --from=0 f2 /
}) })
assert.Error(t, err) assert.Error(t, err)
df = `FROM busybox df = `FROM scratch
ADD http://github.com/moby/buildkit/blob/master/README.md / ADD http://github.com/moby/buildkit/blob/master/README.md /
` `
_, _, err = Dockerfile2LLB(appcontext.Context(), []byte(df), ConvertOpt{}) _, _, err = Dockerfile2LLB(appcontext.Context(), []byte(df), ConvertOpt{})
assert.NoError(t, err) assert.NoError(t, err)
df = `FROM busybox df = `FROM scratch
COPY http://github.com/moby/buildkit/blob/master/README.md / COPY http://github.com/moby/buildkit/blob/master/README.md /
` `
_, _, err = Dockerfile2LLB(appcontext.Context(), []byte(df), ConvertOpt{}) _, _, err = Dockerfile2LLB(appcontext.Context(), []byte(df), ConvertOpt{})