Skip to content

Commit 61f78aa

Browse files
add config option: dockerfile.extraBuildStages (#236)
Co-authored-by: Sandro <[email protected]>
1 parent 78e79a1 commit 61f78aa

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,13 @@ Since only entire packages (not single source files) can be selected for coverag
126126
dockerfile:
127127
enabled: true
128128
entrypoint: [ "/bin/bash", "--", "--arg" ]
129+
extraBuildStages:
130+
- |
131+
FROM ghcr.io/foobar/big-toolbox AS toolbox
132+
RUN toolbox-cmd
129133
extraDirectives:
130-
- 'LABEL mylabel=myvalu'
134+
- 'LABEL mylabel=myvalue'
135+
- 'COPY --from=toolbox /bin/fancytool /usr/bin/fancytool'
131136
extraIgnores:
132137
- tmp
133138
- files
@@ -148,6 +153,7 @@ As an additional smoke test, the compiled binaries are invoked with the `--versi
148153
With [go-api-declarations](https://github.com/sapcc/go-api-declarations)'s [`bininfo.HandleVersionArgument` function](https://pkg.go.dev/github.com/sapcc/go-api-declarations/bininfo#HandleVersionArgument), this can be implemented in one line. If you are using Cobra or any other library to handle arguments, the [`bininfo.Version` function](https://pkg.go.dev/github.com/sapcc/go-api-declarations/bininfo#Version) is recommended instead.
149154

150155
* `entrypoint` allows overwriting the final entrypoint.
156+
* `extraBuildStages` prepends additional build stages at the top of the Dockerfile. This is useful for bringing in precompiled assets from other images, or if a non-Go compilation step is required.
151157
* `extraDirectives` appends additional directives near the end of the Dockerfile.
152158
* `extraIgnores` appends entries in `.dockerignore` to the default ones.
153159
* `extraPackages` installs extra Alpine packages in the final Docker layer. `ca-certificates` is always installed.

internal/core/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ type RenovateConfig struct {
176176
type DockerfileConfig struct {
177177
Enabled bool `yaml:"enabled"`
178178
Entrypoint []string `yaml:"entrypoint"`
179+
ExtraBuildStages []string `yaml:"extraBuildStages"`
179180
ExtraDirectives []string `yaml:"extraDirectives"`
180181
ExtraIgnores []string `yaml:"extraIgnores"`
181182
ExtraPackages []string `yaml:"extraPackages"`

internal/dockerfile/docker.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ func RenderConfig(cfg core.Configuration) {
7777
firstBinary = false
7878
}
7979

80+
extraBuildStages := ""
81+
for _, stage := range cfg.Dockerfile.ExtraBuildStages {
82+
extraBuildStages += fmt.Sprintf("%s\n\n%s\n\n", strings.TrimSpace(stage), strings.Repeat("#", 80))
83+
}
84+
8085
extraDirectives := strings.Join(cfg.Dockerfile.ExtraDirectives, "\n")
8186
if extraDirectives != "" {
8287
extraDirectives += "\n"
@@ -98,38 +103,38 @@ func RenderConfig(cfg core.Configuration) {
98103
runCommands := strings.Join(commands, " \\\n && ")
99104

100105
dockerfile := fmt.Sprintf(
101-
`FROM golang:%[1]s-alpine%[2]s AS builder
106+
`%[1]sFROM golang:%[2]s-alpine%[3]s AS builder
102107
103108
RUN apk add --no-cache --no-progress ca-certificates gcc git make musl-dev
104109
105110
COPY . /src
106111
ARG BININFO_BUILD_DATE BININFO_COMMIT_HASH BININFO_VERSION # provided to 'make install'
107-
RUN make -C /src install PREFIX=/pkg GOTOOLCHAIN=local%[3]s
112+
RUN make -C /src install PREFIX=/pkg GOTOOLCHAIN=local%[4]s
108113
109114
################################################################################
110115
111-
FROM alpine:%[2]s
116+
FROM alpine:%[3]s
112117
113-
%[4]s# upgrade all installed packages to fix potential CVEs in advance
118+
%[5]s# upgrade all installed packages to fix potential CVEs in advance
114119
# also remove apk package manager to hopefully remove dependency on OpenSSL 🤞
115-
RUN %[5]s
120+
RUN %[6]s
116121
117122
COPY --from=builder /etc/ssl/certs/ /etc/ssl/certs/
118123
COPY --from=builder /etc/ssl/cert.pem /etc/ssl/cert.pem
119124
COPY --from=builder /pkg/ /usr/
120125
# make sure all binaries can be executed
121-
%[6]s
126+
%[7]s
122127
123128
ARG BININFO_BUILD_DATE BININFO_COMMIT_HASH BININFO_VERSION
124-
LABEL source_repository="%[7]s" \
125-
org.opencontainers.image.url="%[7]s" \
129+
LABEL source_repository="%[8]s" \
130+
org.opencontainers.image.url="%[8]s" \
126131
org.opencontainers.image.created=${BININFO_BUILD_DATE} \
127132
org.opencontainers.image.revision=${BININFO_COMMIT_HASH} \
128133
org.opencontainers.image.version=${BININFO_VERSION}
129134
130-
%[8]s%[9]sWORKDIR %[10]s
131-
ENTRYPOINT [ %[11]s ]
132-
`, core.DefaultGoVersion, core.DefaultAlpineImage, goBuildflags, addUserGroup, runCommands, runVersionArg, cfg.Metadata.URL, extraDirectives, userCommand, workingDir, entrypoint)
135+
%[9]s%[10]sWORKDIR %[11]s
136+
ENTRYPOINT [ %[12]s ]
137+
`, extraBuildStages, core.DefaultGoVersion, core.DefaultAlpineImage, goBuildflags, addUserGroup, runCommands, runVersionArg, cfg.Metadata.URL, extraDirectives, userCommand, workingDir, entrypoint)
133138

134139
must.Succeed(os.WriteFile("Dockerfile", []byte(dockerfile), 0666))
135140

0 commit comments

Comments
 (0)