-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker-bake.hcl
209 lines (184 loc) · 4.68 KB
/
docker-bake.hcl
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
# docker-bake.hcl
# # # groups
group "default" {
targets = ["scratch"]
}
# # # variables
# # docs.docker.com/build/bake/reference/#variable
# # set an environment variable to change the default variables
# # tag variables
variable "REGISTRY_OWNER" {
default = "local"
}
variable "REPO_SHA" {
default = ""
}
variable "SHORT_SHA" {
default = "${shortSHA(REPO_SHA)}"
}
# # build arguments
variable "BIN_DIR" {
default = "/usr/local/bin"
}
variable "TARGET_PATH" {
default = "/tool/net/redbean"
}
variable "TARGET_NAME" {
default = "${filename(TARGET_PATH)}"
}
variable "MODE" {
default = "optlinux"
}
variable "ALPINE" {
# alpine:3.21.0
default = "alpine@sha256:21dc6063fd678b478f57c0e13f47560d0ea4eeba26dfc947b2a4f81f686b9f45"
}
variable "DEBIAN" {
# debian:bookworm-20241223-slim
default = "debian@sha256:b877a1a3fdf02469440f1768cf69c9771338a875b7add5e80c45b756c92ac20a"
}
# # output arguments
variable "OUTPUT_DIR" {
default = "output"
}
# # # user-defined functions
function "shortSHA" {
# trim the given string, keep the first 6 characters
params = [given]
result = substr(given, 0, 6)
}
function "filename" {
# extracts the final component of a given file path
params = [given]
result = element(split("/", given), length(split("/", given)) - 1)
}
# # # inheritable targets
target "_annotations" {
# https://specs.opencontainers.org/image-spec/annotations/
annotations = [
"org.opencontainers.image.created=${timestamp()}",
"org.opencontainers.image.authors=${REGISTRY_OWNER}",
"org.opencontainers.image.revision=${REPO_SHA}",
"org.opencontainers.image.vendor=Cosmopolitan https://github.com/jart/cosmopolitan",
"org.opencontainers.image.licenses=ISC",
"org.opencontainers.image.title=${TARGET_NAME}",
"org.opencontainers.image.description=build-once run-anywhere",
]
}
target "_labels" {
labels = {
"build.date" = timestamp(),
"cosmopolitan.commit" = equal("", REPO_SHA) ? "HEAD" : REPO_SHA,
"cosmopolitan.mode" = MODE,
"cosmopolitan.target" = TARGET_PATH
}
}
# # # targets
# # sub step images
target "repo" {
context = "."
dockerfile = "dockerfiles/Dockerfile.repo"
contexts = {
debian = "docker-image://${DEBIAN}"
}
args = {
REPO_SHA = REPO_SHA # default ""
}
}
target "repo-local" {
inherits = ["repo", "_labels"]
tags = [
equal("", REPO_SHA) ? "repo:latest" : "repo:${SHORT_SHA}",
]
}
target "compile" {
context = "."
dockerfile = "dockerfiles/Dockerfile.compile"
contexts = {
repo = "target:repo"
}
args = {
MODE = MODE, # default "optlinux"
TARGET_NAME = TARGET_NAME, # default redbean
TARGET_PATH = TARGET_PATH, # default "/tool/net/redbean"
}
}
target "compile-local" {
inherits = ["compile", "_labels"]
tags = [
equal("", REPO_SHA) ? "compile:latest" : "compile:${SHORT_SHA}",
]
}
# # final images
target "scratch" {
inherits = ["_annotations", "_labels"]
context = "."
dockerfile = "dockerfiles/Dockerfile.scratch"
contexts = {
compile = "target:compile"
}
args = {
BIN_DIR = BIN_DIR, # default "/usr/local/bin"
TARGET_NAME = TARGET_NAME, # default redbean
}
tags = equal("", REPO_SHA) ? [
"${TARGET_NAME}:${MODE}",
"ghcr.io/${REGISTRY_OWNER}/${TARGET_NAME}:${MODE}",
] : [
"${TARGET_NAME}:${MODE}-${SHORT_SHA}",
"ghcr.io/${REGISTRY_OWNER}/${TARGET_NAME}:${MODE}-${SHORT_SHA}",
]
}
target "alpine" {
inherits = ["_annotations", "_labels"]
context = "."
dockerfile = "dockerfiles/Dockerfile.alpine"
contexts = {
alpine = "docker-image://${ALPINE}"
compile = "target:compile"
}
args = {
BIN_DIR = BIN_DIR, # default "/usr/local/bin"
TARGET_NAME = TARGET_NAME, # default redbean
}
tags = equal("", REPO_SHA) ? [
"${TARGET_NAME}:${MODE}-alpine",
"ghcr.io/${REGISTRY_OWNER}/${TARGET_NAME}:${MODE}-alpine",
] : [
"${TARGET_NAME}:${MODE}-alpine-${SHORT_SHA}",
"ghcr.io/${REGISTRY_OWNER}/${TARGET_NAME}:${MODE}-alpine-${SHORT_SHA}",
]
}
# # output files
target "repo-output" {
contexts = {
context = "target:repo"
}
dockerfile-inline = <<EOT
FROM scratch
COPY --from=context /cosmopolitan /
EOT
output = equal("", REPO_SHA) ? [
"type=local,dest=${OUTPUT_DIR}/cosmopolitan"
] : [
"type=local,dest=${OUTPUT_DIR}-${SHORT_SHA}/cosmopolitan"
]
}
target "binary-output" {
contexts = {
context = "target:compile"
}
args = {
TARGET_NAME = TARGET_NAME, # default redbean
}
dockerfile-inline = <<EOT
FROM scratch
ARG TARGET_NAME
COPY --from=context "/usr/local/bin/${TARGET_NAME}" /
EOT
output = equal("", REPO_SHA) ? [
"type=local,dest=${OUTPUT_DIR}"
] : [
"type=local,dest=${OUTPUT_DIR}-${SHORT_SHA}"
]
}