Skip to content

Commit f0a68b3

Browse files
Handle custom invocation images without #PORTER_INIT (#2998)
* Handle custom Dockerfile without PORTER_INIT The documentation says that if the PORTER_INIT section is missing on the custom dockerfile, it will be placed right after the FROM section. This was not the case, instead it was place at the end of the file, resulting in a dockerfile not working with Porter Signed-off-by: Kim Christensen <kimworking@gmail.com> --------- Signed-off-by: Kim Christensen <kimworking@gmail.com>
1 parent 7001782 commit f0a68b3

5 files changed

+54
-6
lines changed

pkg/build/dockerfile-generator.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ func (g *DockerfileGenerator) copyMixin(mixin string) error {
254254

255255
func (g *DockerfileGenerator) getIndexOfToken(lines []string, token string) int {
256256
for lineNumber, lineContent := range lines {
257-
if token == strings.TrimSpace(lineContent) {
257+
if strings.HasPrefix(strings.TrimSpace(lineContent), token) {
258258
return lineNumber
259259
}
260260
}
@@ -269,7 +269,7 @@ func (g *DockerfileGenerator) replaceTokens(ctx context.Context, lines []string)
269269
return nil, fmt.Errorf("error generating Dockerfile content for mixins: %w", err)
270270
}
271271

272-
fromToken := g.getIndexOfToken(lines, "FROM")
272+
fromToken := g.getIndexOfToken(lines, "FROM") + 1
273273

274274
substitutions := []struct {
275275
token string

pkg/build/dockerfile-generator_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,35 @@ COPY mybin /cnab/app/
103103
require.NoError(t, err)
104104
test.CompareGoldenFile(t, "testdata/custom-dockerfile-expected-output.Dockerfile", strings.Join(gotlines, "\n"))
105105
})
106+
107+
t.Run("build from custom docker without PORTER_INIT supplied", func(t *testing.T) {
108+
t.Parallel()
109+
110+
c := config.NewTestConfig(t)
111+
tmpl := templates.NewTemplates(c.Config)
112+
configTpl, err := tmpl.GetManifest()
113+
require.Nil(t, err)
114+
c.TestContext.AddTestFileContents(configTpl, config.Name)
115+
116+
m, err := manifest.LoadManifestFrom(context.Background(), c.Config, config.Name)
117+
require.NoError(t, err, "could not load manifest")
118+
119+
// Use a custom dockerfile template
120+
m.Dockerfile = "Dockerfile.template"
121+
customFrom := `FROM ubuntu:latest
122+
# stuff
123+
COPY mybin /cnab/app/
124+
125+
`
126+
c.TestContext.AddTestFileContents([]byte(customFrom), "Dockerfile.template")
127+
128+
mp := mixin.NewTestMixinProvider()
129+
g := NewDockerfileGenerator(c.Config, m, tmpl, mp)
130+
gotlines, err := g.buildDockerfile(context.Background())
131+
132+
require.NoError(t, err)
133+
test.CompareGoldenFile(t, "testdata/custom-dockerfile-without-init-expected-output.Dockerfile", strings.Join(gotlines, "\n"))
134+
})
106135
}
107136

108137
func TestPorter_generateDockerfile(t *testing.T) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# syntax=docker/dockerfile-upstream:1.4.0
2+
FROM ubuntu:latest
3+
ARG BUNDLE_DIR
4+
ARG BUNDLE_UID=65532
5+
ARG BUNDLE_USER=nonroot
6+
ARG BUNDLE_GID=0
7+
RUN useradd ${BUNDLE_USER} -m -u ${BUNDLE_UID} -g ${BUNDLE_GID} -o
8+
# stuff
9+
COPY mybin /cnab/app/
10+
11+
# exec mixin has no buildtime dependencies
12+
13+
RUN rm ${BUNDLE_DIR}/porter.yaml
14+
RUN rm -fr ${BUNDLE_DIR}/.cnab
15+
COPY --link .cnab /cnab
16+
RUN chgrp -R ${BUNDLE_GID} /cnab && chmod -R g=u /cnab
17+
USER ${BUNDLE_UID}
18+
WORKDIR ${BUNDLE_DIR}
19+
CMD ["/cnab/app/run"]

pkg/build/testdata/missing-args-expected-output.Dockerfile

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# syntax=docker/dockerfile-upstream:1.4.0
22
FROM ubuntu:latest
3-
COPY mybin /cnab/app/
4-
53
ARG BUNDLE_DIR
64
ARG BUNDLE_UID=65532
75
ARG BUNDLE_USER=nonroot
86
ARG BUNDLE_GID=0
97
RUN useradd ${BUNDLE_USER} -m -u ${BUNDLE_UID} -g ${BUNDLE_GID} -o
8+
COPY mybin /cnab/app/
9+
1010
# exec mixin has no buildtime dependencies
1111

1212
RUN rm ${BUNDLE_DIR}/porter.yaml

pkg/build/testdata/missing-mixins-token-expected-output.Dockerfile

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# syntax=docker/dockerfile-upstream:1.4.0
22
FROM ubuntu:light
33
ARG BUNDLE_DIR
4-
COPY mybin /cnab/app/
5-
ARG BUNDLE_DIR
64
ARG BUNDLE_UID=65532
75
ARG BUNDLE_USER=nonroot
86
ARG BUNDLE_GID=0
97
RUN useradd ${BUNDLE_USER} -m -u ${BUNDLE_UID} -g ${BUNDLE_GID} -o
8+
ARG BUNDLE_DIR
9+
COPY mybin /cnab/app/
1010
# exec mixin has no buildtime dependencies
1111

1212
RUN rm ${BUNDLE_DIR}/porter.yaml

0 commit comments

Comments
 (0)