Skip to content

Commit 360077e

Browse files
LucasRoesleralexellis
authored andcommitted
Support go modules
**What** - Add support for nested go modules by supporting the GO_REPLACE.txt file and the build args for GO111MODULE and GOFLAGS Signed-off-by: Lucas Roesler <[email protected]>
1 parent 465644c commit 360077e

File tree

6 files changed

+50
-20
lines changed

6 files changed

+50
-20
lines changed

template/go-armhf/Dockerfile

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,41 @@
11
FROM openfaas/classic-watchdog:0.18.1 as watchdog
2-
FROM golang:1.12-alpine3.11 as builder
2+
FROM golang:1.13-alpine3.11 as builder
33

44
# Allows you to add additional packages via build-arg
55
ARG ADDITIONAL_PACKAGE
66
ARG CGO_ENABLED=0
77
ARG GO111MODULE="off"
88
ARG GOPROXY=""
9+
ARG GOFLAGS=""
910

1011
COPY --from=watchdog /fwatchdog /usr/bin/fwatchdog
1112
RUN chmod +x /usr/bin/fwatchdog
1213

14+
ENV CGO_ENABLED=0
15+
1316
WORKDIR /go/src/handler
1417
COPY . .
1518

19+
# Add user overrides to the root go.mod, which is the only place "replace" can be used
20+
RUN cat function/GO_REPLACE.txt >> ./go.mod || exit 0
21+
1622
# Run a gofmt and exclude all vendored code.
1723
RUN test -z "$(gofmt -l $(find . -type f -name '*.go' -not -path "./vendor/*" -not -path "./function/vendor/*"))" || { echo "Run \"gofmt -s -w\" on your Golang code"; exit 1; }
1824

25+
WORKDIR /go/src/handler/function
26+
27+
RUN go test ./... -cover
28+
29+
WORKDIR /go/src/handler
30+
1931
RUN CGO_ENABLED=${CGO_ENABLED} GOOS=linux \
20-
go build --ldflags "-s -w" -a -installsuffix cgo -o handler . && \
21-
go test $(go list ./... | grep -v /vendor/) -cover
32+
go build --ldflags "-s -w" -a -installsuffix cgo -o handler .
2233

2334
FROM alpine:3.11
24-
RUN apk --no-cache add \
25-
ca-certificates
26-
27-
# Add non root user
28-
RUN addgroup -S app && adduser -S -g app app
29-
RUN mkdir -p /home/app
35+
RUN apk --no-cache add ca-certificates \
36+
&& addgroup -S app && adduser -S -g app app \
37+
&& mkdir -p /home/app \
38+
&& chown app /home/app
3039

3140
WORKDIR /home/app
3241

template/go-armhf/go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module handler
2+
3+
go 1.13
4+
5+
replace handler/function => ./function

template/go-armhf/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ package main
66

77
import (
88
"fmt"
9-
"handler/function"
109
"io/ioutil"
1110
"log"
1211
"os"
12+
13+
"handler/function"
1314
)
1415

1516
func main() {

template/go/Dockerfile

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,41 @@
11
FROM openfaas/classic-watchdog:0.18.1 as watchdog
2-
FROM golang:1.12-alpine3.11 as builder
2+
FROM golang:1.13-alpine3.11 as builder
33

44
# Allows you to add additional packages via build-arg
55
ARG ADDITIONAL_PACKAGE
66
ARG CGO_ENABLED=0
77
ARG GO111MODULE="off"
88
ARG GOPROXY=""
9+
ARG GOFLAGS=""
910

1011
COPY --from=watchdog /fwatchdog /usr/bin/fwatchdog
1112
RUN chmod +x /usr/bin/fwatchdog
1213

14+
ENV CGO_ENABLED=0
15+
1316
WORKDIR /go/src/handler
1417
COPY . .
1518

19+
# Add user overrides to the root go.mod, which is the only place "replace" can be used
20+
RUN cat function/GO_REPLACE.txt >> ./go.mod || exit 0
21+
1622
# Run a gofmt and exclude all vendored code.
1723
RUN test -z "$(gofmt -l $(find . -type f -name '*.go' -not -path "./vendor/*" -not -path "./function/vendor/*"))" || { echo "Run \"gofmt -s -w\" on your Golang code"; exit 1; }
1824

25+
WORKDIR /go/src/handler/function
26+
27+
RUN go test ./... -cover
28+
29+
WORKDIR /go/src/handler
30+
1931
RUN CGO_ENABLED=${CGO_ENABLED} GOOS=linux \
20-
go build --ldflags "-s -w" -a -installsuffix cgo -o handler . && \
21-
go test $(go list ./... | grep -v /vendor/) -cover
32+
go build --ldflags "-s -w" -a -installsuffix cgo -o handler .
2233

2334
FROM alpine:3.11
24-
RUN apk --no-cache add \
25-
ca-certificates
26-
27-
# Add non root user
28-
RUN addgroup -S app && adduser -S -g app app
29-
RUN mkdir -p /home/app
35+
RUN apk --no-cache add ca-certificates \
36+
&& addgroup -S app && adduser -S -g app app \
37+
&& mkdir -p /home/app \
38+
&& chown app /home/app
3039

3140
WORKDIR /home/app
3241

template/go/go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module handler
2+
3+
go 1.13
4+
5+
replace handler/function => ./function

template/go/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ package main
66

77
import (
88
"fmt"
9-
"handler/function"
109
"io/ioutil"
1110
"log"
1211
"os"
12+
13+
"handler/function"
1314
)
1415

1516
func main() {

0 commit comments

Comments
 (0)