Skip to content

Commit

Permalink
Add environment replacement support for chmod option
Browse files Browse the repository at this point in the history
Add environment replacement support for chmod option argument for the
ADD and COPY commands.

Signed-off-by: Mitsuru Kariya <[email protected]>
  • Loading branch information
kariya-mitsuru committed Jul 24, 2024
1 parent 5a91bd3 commit 22fbeaf
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
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

0 comments on commit 22fbeaf

Please sign in to comment.