@@ -4,7 +4,6 @@ import climate
4
4
5
5
import nimage/ flavors/ [slim, regular]
6
6
7
-
8
7
proc isDefault (props: JsonNode ): bool =
9
8
props.getOrDefault (" default" ).getBool
10
9
@@ -44,7 +43,7 @@ proc getTags(
44
43
result .add flavor
45
44
46
45
proc generateDockerfile (
47
- version, base, flavor: string , labels: openarray [(string , string )]
46
+ version, base, flavor: string , labels: openarray [(string , string )], dockerfileDir: string
48
47
) =
49
48
var content = " "
50
49
@@ -68,18 +67,20 @@ proc generateDockerfile(
68
67
else :
69
68
discard
70
69
71
- writeFile (" Dockerfile" , content)
70
+ createDir (dockerfileDir)
71
+
72
+ writeFile (dockerfileDir / " Dockerfile" , content)
72
73
73
- proc buildAndPushImage (tags: openarray [string ], tagPrefix: string ) =
74
+ proc buildAndPushImage (tags: openarray [string ], tagPrefix: string , dockerfileDir: string ) =
74
75
const dockerBuildCommand =
75
- " docker buildx build --push --platform linux/amd64,linux/arm64,linux/arm $# . "
76
+ " docker buildx build --push --platform linux/amd64,linux/arm64,linux/arm $# $# "
76
77
77
78
var tagLine = " "
78
79
79
80
for tag in tags:
80
81
tagLine &= " -t $#:$# " % [tagPrefix, tag]
81
82
82
- discard execShellCmd dockerBuildCommand % tagLine
83
+ discard execShellCmd dockerBuildCommand % [ tagLine, dockerfileDir]
83
84
84
85
proc testImage (image: string , flavor: string ) =
85
86
let succeeded =
@@ -105,12 +106,16 @@ proc showHelp(context: Context): int =
105
106
106
107
Usage:
107
108
108
- $ nimage build-and-push [--config|-c=config.json] [--all|-a] [--dry|-d] [<version> <version> ...]
109
+ $ nimage build-and-push [--config|-c=config.json] [--all|-a] [--dry|-d] [--save|-s] [ <version> <version> ...]
109
110
110
111
Build and push specific versions:
111
112
112
113
$ nimage build-and-push <version1> <version2> ...
113
114
115
+ Build and push specific versions and save the Dockerfiles in `Dockerfiles/<version>/<flavor>`:
116
+
117
+ $ nimage build-and-push --save <version1> <version2> ...
118
+
114
119
Build and push all versions listed in the config file:
115
120
116
121
$ nimage build-and-push --all
@@ -121,7 +126,7 @@ Use custom config file (by default, `config.json` in the current directory is us
121
126
122
127
Dry run (nothing is built or pushed, use to check the config and command args):
123
128
124
- $ nimage build-and-push --dry search_shortcut: ALT+SHIFT+SPACEsearch_shortcut: ALT+SHIFT+SPACE <version1> <version2> ...
129
+ $ nimage build-and-push --dry <version1> <version2> ...
125
130
"""
126
131
127
132
echo helpMessage
@@ -143,11 +148,13 @@ proc buildAndPushImages(context: Context): int =
143
148
labels = {" authors" : authors}
144
149
tagPrefix = " nimlang/nim"
145
150
flavors = [" slim" , " regular" ]
151
+ dockerfilesDir = " Dockerfiles"
146
152
147
153
var
148
154
configFile = " config.json"
149
155
buildAll = false
150
156
dryRun = false
157
+ save = false
151
158
targets: seq [string ] = @ []
152
159
153
160
context.opt (" config" , " c" ):
@@ -159,6 +166,9 @@ proc buildAndPushImages(context: Context): int =
159
166
context.flag (" dry" , " d" ):
160
167
dryRun = true
161
168
169
+ context.flag (" save" , " s" ):
170
+ save = true
171
+
162
172
context.args:
163
173
targets = args
164
174
@@ -171,26 +181,34 @@ proc buildAndPushImages(context: Context): int =
171
181
if buildAll or version.key in targets:
172
182
for base in bases.pairs:
173
183
for flavor in flavors:
174
- let tags = getTags (version, base, flavor)
184
+ let
185
+ dockerfileDir = dockerfilesDir / version.key / flavor
186
+ tags = getTags (version, base, flavor)
187
+
188
+ echo " Building and pushing $# from $#... " % [tags[0 ], dockerfileDir]
175
189
176
- echo " Building and pushing $#... " % tags[0 ]
177
190
if not dryRun:
178
- generateDockerfile (version.key, base.key, flavor, labels)
179
- buildAndPushImage (tags, tagPrefix)
180
- removeFile (" Dockerfile" )
191
+ generateDockerfile (version.key, base.key, flavor, labels, dockerfileDir)
192
+
193
+ buildAndPushImage (tags, tagPrefix, dockerfileDir)
194
+
195
+ if save:
196
+ echo " Saving Dockerfile to $#..." % dockerfileDir
197
+ else :
198
+ removeDir (dockerfileDir)
199
+
181
200
echo " Done!"
182
201
183
202
# Anything before this is broken and too old to fix.
184
203
if version.key >= " 0.16.0" :
185
204
echo " Testing $#... " % tags[0 ]
205
+
186
206
if not dryRun:
187
207
testImage (" $#:$#" % [tagPrefix, tags[0 ]], flavor)
188
- echo " Done!"
189
208
209
+ echo " Done!"
190
210
191
211
const commands = {" build-and-push" : buildAndPushImages, " setup" : createBuilder}
192
212
193
-
194
213
when isMainModule :
195
214
quit parseCommands (commands, defaultHandler = showHelp)
196
-
0 commit comments