Skip to content

Commit 5b51289

Browse files
committed
Add the ability to save Dockerfile during building.
1 parent 40b89fb commit 5b51289

File tree

5 files changed

+63
-18
lines changed

5 files changed

+63
-18
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ build
22
*.exe
33
nimcache
44
*.sublime-*
5-
Dockerfile
5+
./Dockerfile
66
bin/

Dockerfiles/2.2.2/regular/Dockerfile

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM nimlang/nim:2.2.2-alpine-slim
2+
LABEL authors="Konstantin Molchanov <[email protected]>, \
3+
Guilherme Thomazi Bonicontro <[email protected]>, \
4+
Dominik Picheta <http://picheta.me>, \
5+
nigredo-tori <https://github.com/nigredo-tori>, \
6+
markprocess <https://github.com/markprocess>, \
7+
theAkito <[email protected]>"
8+
RUN apk add --no-cache git mercurial openssl
9+
RUN cd nim; nim c koch; ./koch tools;\
10+
ln -s `pwd`/bin/nimble /bin/nimble;\
11+
ln -s `pwd`/bin/nimsuggest /bin/nimsuggest;\
12+
ln -s `pwd`/bin/testament /bin/testament
13+
ENV PATH="/root/.nimble/bin:$PATH"

Dockerfiles/2.2.2/slim/Dockerfile

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
FROM alpine:3.20
2+
LABEL authors="Konstantin Molchanov <[email protected]>, \
3+
Guilherme Thomazi Bonicontro <[email protected]>, \
4+
Dominik Picheta <http://picheta.me>, \
5+
nigredo-tori <https://github.com/nigredo-tori>, \
6+
markprocess <https://github.com/markprocess>, \
7+
theAkito <[email protected]>"
8+
RUN apk add --no-cache g++ curl tar xz nodejs
9+
RUN mkdir -p /nim; \
10+
curl -sL "https://nim-lang.org/download/nim-2.2.2.tar.xz" \
11+
|tar xJ --strip-components=1 -C /nim; \
12+
cd /nim; sh build.sh; \
13+
rm -r c_code tests; \
14+
ln -s `pwd`/bin/nim /bin/nim

nimage.nimble

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Package
22

3-
version = "1.0.0"
3+
version = "1.0.1"
44
author = "Constantine Molchanov"
55
description = "Build and push Nim Docker images to Docker Hub."
66
license = "MIT"

src/nimage.nim

+34-16
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import climate
44

55
import nimage/flavors/[slim, regular]
66

7-
87
proc isDefault(props: JsonNode): bool =
98
props.getOrDefault("default").getBool
109

@@ -44,7 +43,7 @@ proc getTags(
4443
result.add flavor
4544

4645
proc generateDockerfile(
47-
version, base, flavor: string, labels: openarray[(string, string)]
46+
version, base, flavor: string, labels: openarray[(string, string)], dockerfileDir: string
4847
) =
4948
var content = ""
5049

@@ -68,18 +67,20 @@ proc generateDockerfile(
6867
else:
6968
discard
7069

71-
writeFile("Dockerfile", content)
70+
createDir(dockerfileDir)
71+
72+
writeFile(dockerfileDir / "Dockerfile", content)
7273

73-
proc buildAndPushImage(tags: openarray[string], tagPrefix: string) =
74+
proc buildAndPushImage(tags: openarray[string], tagPrefix: string, dockerfileDir: string) =
7475
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 $# $#"
7677

7778
var tagLine = ""
7879

7980
for tag in tags:
8081
tagLine &= " -t $#:$# " % [tagPrefix, tag]
8182

82-
discard execShellCmd dockerBuildCommand % tagLine
83+
discard execShellCmd dockerBuildCommand % [tagLine, dockerfileDir]
8384

8485
proc testImage(image: string, flavor: string) =
8586
let succeeded =
@@ -105,12 +106,16 @@ proc showHelp(context: Context): int =
105106
106107
Usage:
107108
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> ...]
109110
110111
Build and push specific versions:
111112
112113
$ nimage build-and-push <version1> <version2> ...
113114
115+
Build and push specific versions and save the Dockerfiles in `Dockerfiles/<version>/<flavor>`:
116+
117+
$ nimage build-and-push --save <version1> <version2> ...
118+
114119
Build and push all versions listed in the config file:
115120
116121
$ nimage build-and-push --all
@@ -121,7 +126,7 @@ Use custom config file (by default, `config.json` in the current directory is us
121126
122127
Dry run (nothing is built or pushed, use to check the config and command args):
123128
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> ...
125130
"""
126131

127132
echo helpMessage
@@ -143,11 +148,13 @@ proc buildAndPushImages(context: Context): int =
143148
labels = {"authors": authors}
144149
tagPrefix = "nimlang/nim"
145150
flavors = ["slim", "regular"]
151+
dockerfilesDir = "Dockerfiles"
146152

147153
var
148154
configFile = "config.json"
149155
buildAll = false
150156
dryRun = false
157+
save = false
151158
targets: seq[string] = @[]
152159

153160
context.opt("config", "c"):
@@ -159,6 +166,9 @@ proc buildAndPushImages(context: Context): int =
159166
context.flag("dry", "d"):
160167
dryRun = true
161168

169+
context.flag("save", "s"):
170+
save = true
171+
162172
context.args:
163173
targets = args
164174

@@ -171,26 +181,34 @@ proc buildAndPushImages(context: Context): int =
171181
if buildAll or version.key in targets:
172182
for base in bases.pairs:
173183
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]
175189

176-
echo "Building and pushing $#... " % tags[0]
177190
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+
181200
echo "Done!"
182201

183202
# Anything before this is broken and too old to fix.
184203
if version.key >= "0.16.0":
185204
echo "Testing $#... " % tags[0]
205+
186206
if not dryRun:
187207
testImage("$#:$#" % [tagPrefix, tags[0]], flavor)
188-
echo "Done!"
189208

209+
echo "Done!"
190210

191211
const commands = {"build-and-push": buildAndPushImages, "setup": createBuilder}
192212

193-
194213
when isMainModule:
195214
quit parseCommands(commands, defaultHandler = showHelp)
196-

0 commit comments

Comments
 (0)