Skip to content

Commit 3d829ed

Browse files
authored
Merge pull request #154 from replicatedhq/issue/145
A support for “scratch as” and “from —platform”
2 parents 6d757c7 + 23a27be commit 3d829ed

File tree

3 files changed

+143
-5
lines changed

3 files changed

+143
-5
lines changed

lib/checks.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,24 @@ var commands = module.exports = {
3030
},
3131

3232
base_image_tag: function(args, previousStageNames) {
33-
// scratch is a special base layer for Docker, it means there is no base layer
34-
if (args === 'scratch') {
35-
return [];
36-
}
3733
var baseImage;
3834
if (args.includes('@')) {
3935
baseImage = args.split('@');
4036
} else {
41-
baseImage = args.split(' ')[0].split(":");
37+
const argWords = args.split(' ');
38+
let maybeBaseImage = argWords[0];
39+
if (maybeBaseImage.startsWith('--platform')) {
40+
maybeBaseImage = argWords[1];
41+
}
42+
baseImage = maybeBaseImage.split(":");
4243
}
4344
var result = [];
4445
if (baseImage.length === 1) {
46+
// from scratch does not need (or support) a tag
47+
if (baseImage[0].toLowerCase() === 'scratch') {
48+
return [];
49+
}
50+
4551
// support for multistage builds, a base image can reference a previous stage
4652
if (!previousStageNames || previousStageNames.indexOf(baseImage[0].toLowerCase()) === -1) {
4753
result.push('missing_tag');

test/examples/Dockerfile.issue145

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# we set this here because buildx normally sets it for us, but
2+
# CircleCI doesn't support a new enough docker, so we have to
3+
# pass this arg manually when in CI
4+
# ARG BUILDPLATFORM
5+
6+
# Run tests stage (we only run this on the BUILDPLATFORM)
7+
FROM --platform=$BUILDPLATFORM golang:alpine3.12 as test
8+
9+
WORKDIR /go/src/github.com/iggy/scurvy/
10+
11+
RUN apk add --no-cache git upx gcc libc-dev
12+
13+
RUN go get -u golang.org/x/lint/golint \
14+
honnef.co/go/tools/cmd/staticcheck \
15+
github.com/fzipp/gocyclo
16+
17+
# Use add here to invalidate the cache
18+
ADD . /go/src/github.com/iggy/scurvy/
19+
20+
# install deps the easy way
21+
RUN go get github.com/iggy/scurvy/...
22+
23+
# These are all separate so failures are a little easier to track
24+
RUN gofmt -l -s -w ./cmd ./pkg
25+
# RUN test -z $(gofmt -s -l $GO_FILES)
26+
# go test -race basically doesn't work with alpine/musl
27+
# RUN go test -v -race ./...
28+
RUN go vet ./...
29+
RUN staticcheck ./...
30+
# RUN gocyclo -over 19 $GO_FILES
31+
RUN golint -set_exit_status $(go list ./...)
32+
33+
34+
35+
# Build binaries stage (we only run this on the BUILDPLATFORM)
36+
FROM --platform=$BUILDPLATFORM golang:alpine3.12 as build
37+
38+
WORKDIR /go/src/github.com/iggy/scurvy/
39+
40+
RUN apk add --no-cache git upx gcc libc-dev
41+
42+
RUN go get -u github.com/mitchellh/gox \
43+
github.com/tcnksm/ghr
44+
45+
# Use add here to invalidate the cache
46+
ADD . /go/src/github.com/iggy/scurvy/
47+
48+
# install deps the easy way
49+
RUN go get github.com/iggy/scurvy/...
50+
51+
# build the binaries
52+
# release binaries (these may link against libc)
53+
RUN gox \
54+
-arch="amd64" \
55+
-os="linux" \
56+
-output="dist/{{.OS}}_{{.Arch}}_{{.Dir}}" \
57+
-ldflags='-extldflags "-static" -s -w' \
58+
-tags='netgo' ./...
59+
# docker binaries (these are prohibited to link against libc since they go in
60+
# a scratch image)
61+
# They are also compressed (upx) to make the docker images as small as possible
62+
RUN mkdir -p /ddist/etc
63+
RUN CGO_ENABLED=0 gox \
64+
-arch="amd64 arm64 arm" \
65+
-os="linux" \
66+
-output="/ddist/{{.OS}}_{{.Arch}}_{{.Dir}}" \
67+
-ldflags='-extldflags "-static" -s -w' \
68+
-tags='netgo' ./...
69+
RUN upx /ddist/linux*
70+
71+
72+
73+
# This builds the irc image from build binaries stage output
74+
FROM scratch as irc
75+
ARG TARGETOS
76+
ARG TARGETARCH
77+
COPY --from=build /ddist/${TARGETOS}_${TARGETARCH}_irc /ircbot
78+
COPY --from=build /ddist/etc /
79+
ENTRYPOINT ["/ircbot"]
80+
81+
82+
83+
# This builds the notifyd image from build binaries stage output
84+
FROM scratch as notifyd
85+
ARG TARGETOS
86+
ARG TARGETARCH
87+
COPY --from=build /ddist/${TARGETOS}_${TARGETARCH}_notifyd /notifyd
88+
COPY --from=build /ddist/etc /
89+
ENTRYPOINT ["/notifyd"]
90+
91+
92+
93+
# This builds the input-webhook image from build binaries stage output
94+
FROM scratch as input-webhook
95+
ARG TARGETOS
96+
ARG TARGETARCH
97+
COPY --from=build /ddist/${TARGETOS}_${TARGETARCH}_input-webhook /input-webhook
98+
COPY --from=build /ddist/etc /
99+
# just the one port that accepts webhook connections from sabnzbd/sickrage/CouchPotato
100+
EXPOSE 38475
101+
ENTRYPOINT ["/input-webhook"]
102+
103+
104+
105+
# This builds the syncd image from build binaries stage output
106+
# syncd runs a shell script to do the actual downloading, so can't use `scratch`
107+
FROM alpine:3.12.0 as syncd
108+
ARG TARGETOS
109+
ARG TARGETARCH
110+
COPY --from=build /ddist/${TARGETOS}_${TARGETARCH}_syncd /syncd
111+
COPY --from=build /ddist/etc /
112+
# COPY --from=build /go/src/github.com/iggy/scurvy/cmd/syncd/sync_files.sh /
113+
114+
# Need the ca-certificates for the NATS TLS cert and using rsync for the
115+
# file sync for now
116+
RUN apk --no-cache add rsync ca-certificates
117+
118+
# ENV SCURVY_BASE_URL "https://scurvy"
119+
# ENV SCURVY_DL_DIR "/scurvy/"
120+
# ENV SCURVY_COMPLETE_URL "scurvy/complete"
121+
# Where all the files are stored
122+
# VOLUME ["/scurvy"]
123+
# The script to run when syncd gets a new download message
124+
# If using rsync/ssh/etc, you'll need to also pass in ssh keys
125+
# VOLUME ["/sync_files.sh"]
126+
ENTRYPOINT ["/syncd"]

test/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ describe("index", function(){
5151
});
5252
});
5353

54+
describe("#issue145", function(){
55+
it("validates that issue145 does not return an error", function(){
56+
expect(dockerfilelint.run('./test/examples', fs.readFileSync('./test/examples/Dockerfile.issue145', 'UTF-8'))).to.be.empty;
57+
});
58+
});
59+
5460
describe("#redis", function(){
5561
it("validates the redis Dockerfile has no issues", function(){
5662
expect(dockerfilelint.run('./test/examples', fs.readFileSync('./test/examples/Dockerfile.redis', 'UTF-8'))).to.be.empty;

0 commit comments

Comments
 (0)