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

Handle custom invocation images without #PORTER_INIT #2998

4 changes: 2 additions & 2 deletions pkg/build/dockerfile-generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ func (g *DockerfileGenerator) copyMixin(mixin string) error {

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

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

substitutions := []struct {
token string
Expand Down
29 changes: 29 additions & 0 deletions pkg/build/dockerfile-generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,35 @@ COPY mybin /cnab/app/
require.NoError(t, err)
test.CompareGoldenFile(t, "testdata/custom-dockerfile-expected-output.Dockerfile", strings.Join(gotlines, "\n"))
})

t.Run("build from custom docker without PORTER_INIT supplied", func(t *testing.T) {
t.Parallel()

c := config.NewTestConfig(t)
tmpl := templates.NewTemplates(c.Config)
configTpl, err := tmpl.GetManifest()
require.Nil(t, err)
c.TestContext.AddTestFileContents(configTpl, config.Name)

m, err := manifest.LoadManifestFrom(context.Background(), c.Config, config.Name)
require.NoError(t, err, "could not load manifest")

// Use a custom dockerfile template
m.Dockerfile = "Dockerfile.template"
customFrom := `FROM ubuntu:latest
# stuff
COPY mybin /cnab/app/

`
c.TestContext.AddTestFileContents([]byte(customFrom), "Dockerfile.template")

mp := mixin.NewTestMixinProvider()
g := NewDockerfileGenerator(c.Config, m, tmpl, mp)
gotlines, err := g.buildDockerfile(context.Background())

require.NoError(t, err)
test.CompareGoldenFile(t, "testdata/custom-dockerfile-without-init-expected-output.Dockerfile", strings.Join(gotlines, "\n"))
})
}

func TestPorter_generateDockerfile(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# syntax=docker/dockerfile-upstream:1.4.0
FROM ubuntu:latest
ARG BUNDLE_DIR
ARG BUNDLE_UID=65532
ARG BUNDLE_USER=nonroot
ARG BUNDLE_GID=0
RUN useradd ${BUNDLE_USER} -m -u ${BUNDLE_UID} -g ${BUNDLE_GID} -o
# stuff
COPY mybin /cnab/app/

# exec mixin has no buildtime dependencies

RUN rm ${BUNDLE_DIR}/porter.yaml
RUN rm -fr ${BUNDLE_DIR}/.cnab
COPY --link .cnab /cnab
RUN chgrp -R ${BUNDLE_GID} /cnab && chmod -R g=u /cnab
USER ${BUNDLE_UID}
WORKDIR ${BUNDLE_DIR}
CMD ["/cnab/app/run"]
4 changes: 2 additions & 2 deletions pkg/build/testdata/missing-args-expected-output.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# syntax=docker/dockerfile-upstream:1.4.0
FROM ubuntu:latest
COPY mybin /cnab/app/

ARG BUNDLE_DIR
ARG BUNDLE_UID=65532
ARG BUNDLE_USER=nonroot
ARG BUNDLE_GID=0
RUN useradd ${BUNDLE_USER} -m -u ${BUNDLE_UID} -g ${BUNDLE_GID} -o
COPY mybin /cnab/app/

# exec mixin has no buildtime dependencies

RUN rm ${BUNDLE_DIR}/porter.yaml
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# syntax=docker/dockerfile-upstream:1.4.0
FROM ubuntu:light
ARG BUNDLE_DIR
COPY mybin /cnab/app/
ARG BUNDLE_DIR
ARG BUNDLE_UID=65532
ARG BUNDLE_USER=nonroot
ARG BUNDLE_GID=0
RUN useradd ${BUNDLE_USER} -m -u ${BUNDLE_UID} -g ${BUNDLE_GID} -o
ARG BUNDLE_DIR
kichristensen marked this conversation as resolved.
Show resolved Hide resolved
COPY mybin /cnab/app/
# exec mixin has no buildtime dependencies

RUN rm ${BUNDLE_DIR}/porter.yaml
Expand Down
Loading