Skip to content

Commit a7e20f6

Browse files
Add docker test target which can be easily run in concourse
1 parent 0f7202e commit a7e20f6

File tree

5 files changed

+50
-5
lines changed

5 files changed

+50
-5
lines changed

internal/core/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ type DockerfileConfig struct {
242242
ExtraDirectives []string `yaml:"extraDirectives"`
243243
ExtraIgnores []string `yaml:"extraIgnores"`
244244
ExtraPackages []string `yaml:"extraPackages"`
245+
ExtraTestPackages []string `yaml:"extraTestPackages"`
245246
RunAsRoot bool `yaml:"runAsRoot"`
246247
UseBuildKit bool `yaml:"useBuildKit"`
247248
WithLinkerdAwait bool `yaml:"withLinkerdAwait"`

internal/dockerfile/Dockerfile.tmpl

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,41 @@ RUN {{ if .UseBuildKit }}--mount=type=cache,target=/go/pkg/mod \
2626
{{ end -}}
2727
################################################################################
2828

29+
# To only build the tests run: docker build . --target test
30+
# We can't do `FROM builder AS test` here, as than make prepare-static-check would not be cached during interactive use when developing
31+
# and caching all the tools, especially golangci-lint, takes a few minutes.
32+
FROM {{ .DockerHubMirror }}golang:{{ .Constants.DefaultGoVersion }}-alpine{{ .Constants.DefaultAlpineImage }} AS test
33+
34+
COPY Makefile /src/Makefile
35+
36+
# used below by USER
37+
RUN addgroup -g 4200 appgroup \
38+
&& adduser -h /home/appuser -s /sbin/nologin -G appgroup -D -u 4200 appuser
39+
40+
RUN apk add --no-cache --no-progress git make {{- range .ExtraTestPackages }} {{.}}{{ end }} \
41+
{{- if .ReuseEnabled }}
42+
&& pip3 install --break-system-packages reuse \
43+
{{- end }}
44+
&& make -C /src prepare-static-check
45+
46+
47+
# We only copy here because we want the "prepare-static-check" to be cacheable.
48+
# It is not a problem that we are overwriting the go cache from the earlier steps because we do not need to rebuild those tools.
49+
COPY --from=builder /go /go
50+
COPY --from=builder /src /src
51+
52+
RUN make -C /src static-check
53+
54+
# Some things like postgres do not like to run as root. For simplicity, just always run as a unprivileged user,
55+
# but for it to be able to read the go cache, we need to allow it.
56+
RUN chmod 777 -R /src/
57+
USER 4200:4200
58+
RUN cd /src \
59+
&& git config --global --add safe.directory /src \
60+
&& make build/cover.out
61+
62+
################################################################################
63+
2964
FROM {{ .DockerHubMirror }}alpine:{{ .Constants.DefaultAlpineImage }}
3065

3166
{{ if not $dcfg.RunAsRoot -}}

internal/dockerfile/docker.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/sapcc/go-bits/must"
1313

1414
"github.com/sapcc/go-makefile-maker/internal/core"
15+
"github.com/sapcc/go-makefile-maker/internal/golang"
1516
"github.com/sapcc/go-makefile-maker/internal/util"
1617
)
1718

@@ -22,7 +23,7 @@ var (
2223
dockerignoreTemplate string
2324
)
2425

25-
func RenderConfig(cfg core.Configuration) {
26+
func RenderConfig(cfg core.Configuration, sr golang.ScanResult) {
2627
// if there is an entrypoint configured use that otherwise fallback to the first binary name
2728
var entrypoint string
2829
if len(cfg.Dockerfile.Entrypoint) > 0 {
@@ -70,14 +71,25 @@ func RenderConfig(cfg core.Configuration) {
7071
dockerHubMirror = "keppel.eu-de-1.cloud.sap/ccloud-dockerhub-mirror/library/"
7172
}
7273

74+
var extraTestPackages []string
75+
reuseEnabled := cfg.Reuse.Enabled.UnwrapOr(true)
76+
if reuseEnabled {
77+
extraTestPackages = append(extraTestPackages, "py3-pip")
78+
}
79+
if sr.UsesPostgres {
80+
extraTestPackages = append(extraTestPackages, "postgresql")
81+
}
82+
7383
must.Succeed(util.WriteFileFromTemplate("Dockerfile", dockerfileTemplate, map[string]any{
7484
"Config": cfg,
7585
"Constants": map[string]any{
7686
"DefaultGoVersion": core.DefaultGoVersion,
7787
"DefaultAlpineImage": core.DefaultAlpineImage,
7888
},
7989
"DockerHubMirror": dockerHubMirror,
90+
"ExtraTestPackages": extraTestPackages,
8091
"Entrypoint": entrypoint,
92+
"ReuseEnabled": reuseEnabled,
8193
"RunCommands": strings.Join(commands, " \\\n && "),
8294
"RunVersionCommands": strings.Join(runVersionCommands, " \\\n && "),
8395
"UseBuildKit": cfg.Dockerfile.UseBuildKit,

internal/dockerfile/dockerignore.tmpl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,10 @@
77
# TODO: uncomment when applications no longer use git to get version information
88
#.git/
99
/.github/
10-
/.gitignore
11-
/.golangci.yaml
1210
/.goreleaser.yml
1311
/.vscode/
1412
/CONTRIBUTING.md
1513
/Dockerfile
16-
/LICENSE*
1714
/Makefile.maker.yaml
1815
/README.md
1916
/build/

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func main() {
9494
// Render Dockerfile
9595
if cfg.Dockerfile.Enabled {
9696
logg.Debug("rendering Dockerfile")
97-
dockerfile.RenderConfig(cfg)
97+
dockerfile.RenderConfig(cfg, sr)
9898
}
9999

100100
// Render golangci-lint config file

0 commit comments

Comments
 (0)