Skip to content

Commit

Permalink
Merge pull request #5151 from kariya-mitsuru/add-env-replace-for-chmod
Browse files Browse the repository at this point in the history
Add environment replacement support for chmod option
  • Loading branch information
crazy-max authored Aug 6, 2024
2 parents 33c1609 + 22fbeaf commit de65ae6
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 @@ -3504,9 +3504,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 @@ -3531,6 +3535,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 @@ -3550,6 +3557,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 testCopyInvalidChmod(t *testing.T, sb integration.Sandbox) {
Expand Down Expand Up @@ -3852,9 +3863,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 @@ -3878,6 +3894,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 @@ -3887,7 +3906,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 testAddInvalidChmod(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 de65ae6

Please sign in to comment.