Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add environment replacement support for chmod option #5151

Merged
merged 1 commit into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions frontend/dockerfile/dockerfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3466,9 +3466,13 @@ COPY --chmod=0644 foo /
COPY --chmod=777 bar /baz
COPY --chmod=0 foo /foobis

ARG mode
COPY --chmod=${mode} foo /footer

RUN stat -c "%04a" /foo > /out/fooperm
RUN stat -c "%04a" /baz > /out/barperm
RUN stat -c "%04a" /foobis > /out/foobisperm
RUN stat -c "%04a" /footer > /out/footerperm
FROM scratch
COPY --from=base /out /
`)
Expand All @@ -3493,6 +3497,9 @@ COPY --from=base /out /
OutputDir: destDir,
},
},
FrontendAttrs: map[string]string{
"build-arg:mode": "755",
},
LocalMounts: map[string]fsutil.FS{
dockerui.DefaultLocalNameDockerfile: dir,
dockerui.DefaultLocalNameContext: dir,
Expand All @@ -3512,6 +3519,10 @@ COPY --from=base /out /
dt, err = os.ReadFile(filepath.Join(destDir, "foobisperm"))
require.NoError(t, err)
require.Equal(t, "0000\n", string(dt))

dt, err = os.ReadFile(filepath.Join(destDir, "footerperm"))
require.NoError(t, err)
require.Equal(t, "0755\n", string(dt))
}

func testCopyOverrideFiles(t *testing.T, sb integration.Sandbox) {
Expand Down Expand Up @@ -3763,9 +3774,14 @@ FROM busybox AS build
ADD --chmod=644 %[1]s /tmp/foo1
ADD --chmod=755 %[1]s /tmp/foo2
ADD --chmod=0413 %[1]s /tmp/foo3

ARG mode
ADD --chmod=${mode} %[1]s /tmp/foo4

RUN stat -c "%%04a" /tmp/foo1 >> /dest && \
stat -c "%%04a" /tmp/foo2 >> /dest && \
stat -c "%%04a" /tmp/foo3 >> /dest
stat -c "%%04a" /tmp/foo3 >> /dest && \
stat -c "%%04a" /tmp/foo4 >> /dest

FROM scratch
COPY --from=build /dest /dest
Expand All @@ -3789,6 +3805,9 @@ COPY --from=build /dest /dest
OutputDir: destDir,
},
},
FrontendAttrs: map[string]string{
"build-arg:mode": "400",
},
LocalMounts: map[string]fsutil.FS{
dockerui.DefaultLocalNameDockerfile: dir,
dockerui.DefaultLocalNameContext: dir,
Expand All @@ -3798,7 +3817,7 @@ COPY --from=build /dest /dest

dt, err := os.ReadFile(filepath.Join(destDir, "dest"))
require.NoError(t, err)
require.Equal(t, []byte("0644\n0755\n0413\n"), dt)
require.Equal(t, []byte("0644\n0755\n0413\n0400\n"), dt)
}

func testDockerfileFromGit(t *testing.T, sb integration.Sandbox) {
Expand Down
12 changes: 12 additions & 0 deletions frontend/dockerfile/instructions/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,12 @@ func (c *AddCommand) Expand(expander SingleWordExpander) error {
}
c.Chown = expandedChown

expandedChmod, err := expander(c.Chmod)
if err != nil {
return err
}
c.Chmod = expandedChmod

expandedChecksum, err := expander(c.Checksum)
if err != nil {
return err
Expand Down Expand Up @@ -287,6 +293,12 @@ func (c *CopyCommand) Expand(expander SingleWordExpander) error {
}
c.Chown = expandedChown

expandedChmod, err := expander(c.Chmod)
if err != nil {
return err
}
c.Chmod = expandedChmod

return c.SourcesAndDest.Expand(expander)
}

Expand Down
Loading