-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathDockerfile
More file actions
232 lines (212 loc) · 6.22 KB
/
Copy pathDockerfile
File metadata and controls
232 lines (212 loc) · 6.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# syntax=docker/dockerfile:1
# This comment is used to simplify checking local copies of the Dockerfile.
# Bump this number every time a significant change is made to this Dockerfile.
#
# AdGuard-Project-Version: 11
# Dockerfile guidelines:
#
# 1. Make sure that Docker correctly caches layers, on a second build attempt it
# must not run lint / test second time when it's not required.
#
# 2. Use BuildKit to improve the build performance (--mount=type=cache, etc).
#
# 3. Prefer using ARG instead of ENV when appropriate, as ARG does not create a
# layer in the final image. However, be careful with what you use ARG for.
# Also, prefer to give ARGs sensible default values.
#
# 4. Use --output and the export stage if you need to get any output on the host
# machine.
#
# NOTE: Only use --output with FROM scratch.
#
# 5. Use .dockerignore to prevent unnecessary files from being sent to the
# Docker daemon, which can invalidate the cache.
#
# 6. Add a CACHE_BUSTER argument to stages to be able to rerun the stages if
# needed. Keep it in sync with the files in .github/workflows/.
ARG BASE_IMAGE=adguard/go-builder:1.26.4--1
# The dependencies stage is needed to install packages and tool dependencies.
# This is also where binaries like osslsigncode, which may be required for tests
# in some projects, must be installed.
#
# Use fake BRANCH and REVISION values to both prevent git calls and also not
# ruin the caching with ARGs.
#
# NOTE: Only ADD the files required to install the dependencies.
FROM "$BASE_IMAGE" AS dependencies
ADD Makefile go.mod go.sum /app/
ADD internal/dnsserver/go.mod internal/dnsserver/go.sum /app/internal/dnsserver/
ADD scripts /app/scripts
WORKDIR /app
RUN \
--mount=type=cache,id=gocache,target=/root/.cache/go-build \
--mount=type=cache,id=gopath,target=/go \
<<-'EOF'
set -e -f -o 'pipefail' -u -x
make \
BRANCH='master' \
REVISION='0000000000000000000000000000000000000000' \
VERBOSE=1 \
go-env \
go-deps \
;
EOF
# The linter stage is separated from the tester stage to make catching test
# failures easier.
#
# Use fake BRANCH and REVISION values to both prevent git calls and also not
# ruin the caching with ARGs. IGNORE_NON_REPRODUCIBLE is set to 1 to make this
# stage reproducible even when linters that query external sources fail.
FROM dependencies AS linter
ADD . /app
WORKDIR /app
RUN \
--mount=type=cache,id=gocache,target=/root/.cache/go-build \
--mount=type=cache,id=gopath,target=/go \
<<-'EOF'
set -e -f -o 'pipefail' -u -x
export GOMAXPROCS=2
make \
BRANCH='master' \
IGNORE_NON_REPRODUCIBLE='1' \
REVISION='0000000000000000000000000000000000000000' \
VERBOSE=1 \
go-lint \
md-lint \
sh-lint \
txt-lint \
2>&1 \
| tee /lint-output.txt \
;
EOF
# linter-exporter exports the test result to the host machine so that it could
# parse and analyze it. This stage should only be used in a CI.
FROM scratch AS linter-exporter
ARG CACHE_BUSTER=0
COPY --from=linter /lint-output.txt /lint-output.txt
# The test stage. TEST_REPORTS_DIR is set to create JUnit reports for the
# tester-exporter stage; run with --build-arg TEST_REPORTS_DIR='' if you don't
# need them on your machine.
#
# Use fake BRANCH and REVISION values to both prevent git calls and also not
# ruin the caching with ARGs.
#
# To run the tests:
#
# docker build --target tester -t 'app' .
#
# Projects that have go-bench and/or go-fuzz targets should add them here as
# well.
FROM linter AS tester
ARG CACHE_BUSTER=0
ARG TEST_INTEGRATION=1
ARG TEST_REPORTS_DIR=/test-reports
RUN \
--mount=type=cache,id=gocache,target=/root/.cache/go-build \
--mount=type=cache,id=gopath,target=/go \
<<-'EOF'
set -e -f -o 'pipefail' -u -x
export GOMAXPROCS=2
export TEST_REDIS_PORT=6379
readonly TEST_REDIS_PORT
readonly redis_server_pidfile='redis-server.pid'
redis-server \
--daemonize yes \
--pidfile "$redis_server_pidfile" \
--port "$TEST_REDIS_PORT" \
;
make \
BRANCH='master' \
REVISION='0000000000000000000000000000000000000000' \
TEST_REPORTS_DIR="$TEST_REPORTS_DIR" \
VERBOSE=1 \
go-test \
;
exit_code="$(cat "${TEST_REPORTS_DIR}/test-exit-code.txt")"
readonly exit_code
redis_server_pid="$(head -n 1 "$redis_server_pidfile")"
readonly redis_server_pid
kill "$redis_server_pid"
make \
BRANCH='master' \
REVISION='0000000000000000000000000000000000000000' \
VERBOSE=1 \
go-fuzz \
go-bench \
;
exit "$exit_code"
EOF
# tester-exporter exports the test result to the host machine so that it could
# parse and analyze it. This stage should only used in a CI.
#
# It the file test-report.xml, which contains test results in the JUnit format.
#
# Run the following command to export the test result:
#
# docker build \
# --output . \
# --progress plain \
# --target tester-exporter \
# .
FROM scratch AS tester-exporter
ARG CACHE_BUSTER=0
ARG TEST_REPORTS_DIR=/test-reports
COPY --from=tester "$TEST_REPORTS_DIR" "$TEST_REPORTS_DIR"
# The builder stage is used to build release artifacts. Real BRANCH and
# REVISION must be used here.
FROM dependencies AS builder
ARG APP_VERSION=""
ARG BRANCH=master
ARG CACHE_BUSTER=0
ARG OUTPUT="agdns"
ARG RACE=0
ARG REVISION=0000000000000000000000000000000000000000
ARG SOURCE_DATE_EPOCH=0
ADD . /app
WORKDIR /app
RUN \
--mount=type=cache,id=gocache,target=/root/.cache/go-build \
--mount=type=cache,id=gopath,target=/go \
<<-'EOF'
set -e -f -o 'pipefail' -u -x
make \
APP_VERSION="$APP_VERSION" \
BRANCH="$BRANCH" \
GOAMD64='v4' \
OUT="dist/${OUTPUT}" \
RACE="$RACE" \
REVISION="$REVISION" \
SOURCE_DATE_EPOCH="$SOURCE_DATE_EPOCH" \
VERBOSE=1 \
go-build \
;
EOF
# The deb-builder stage packs the binary into a .deb package.
FROM alanfranz/fpm-within-docker:ubuntu-bionic AS deb-builder
WORKDIR /app
ARG COMMIT_SHA
ARG NAME=agdns
COPY --from=builder "/app/dist/${NAME}" "/app/${NAME}"
RUN <<-'EOF'
# TODO(a.garipov): Use set -o 'pipefail' when the image supports it.
set -e -f -u -x
fpm \
--category 'non-free/web' \
--deb-user 'web' \
--license 'proprietary' \
--prefix '/usr/local/bin' \
--url 'https://adguard.com/' \
-a 'noarch' \
-m 'Adguard Go Team' \
-n "adguard-${NAME}-service" \
-s 'dir' \
-t 'deb' \
-v "1.${COMMIT_SHA}" \
"${NAME}" \
;
EOF
# The runtime stage.
#
# NOTE: For .deb services this includes only the package.
FROM scratch AS runtime
COPY --from=deb-builder /app/*.deb /