Skip to content

Commit 6ae0208

Browse files
committed
feat: automatically configure vendor mode
When a vendor folder is seen, we need to disable the go module mode, instead we need to use GOPATH mode. This is a simple toggle of one env variable, using a script means that users do not need to set any build args. Signed-off-by: Lucas Roesler <[email protected]>
1 parent 344418e commit 6ae0208

File tree

5 files changed

+81
-8
lines changed

5 files changed

+81
-8
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ The two templates are equivalent with `golang-http` using a structured request/r
2626

2727
You can manage dependencies in one of the following ways:
2828

29-
- To use Go modules without vendoring, the default already is set `GO111MODULE=on` but you also can make that explicit by adding `--build-arg GO111MODULE=on` to `faas-cli up`, you can also use `--build-arg GOPROXY=https://` if you want to use your own mirror for the modules
30-
- You can also Go modules with vendoring, run `go mod vendor` in your function folder and add `--build-arg GO111MODULE=off --build-arg GOFLAGS='-mod=vendor'` to `faas-cli up`
29+
- To use Go modules without vendoring, the default already is set `GO111MODULE=on` but you also can make that explicit by adding `--build-arg GO111MODULE=on` to `faas-cli up`, you can also use `--build-arg GOPROXY=https://` if you want to use your own mirror for the modules.
30+
- You can also Go modules with vendoring, run `go mod vendor`, the build process will automatically detect the vendor folder and configure the Go environment correctly.
3131
- If you have a private module dependency, we recommend using the vendoring technique from above.
3232

3333
## Adding static files to your image

template/golang-http/Dockerfile

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,26 @@ ARG GO111MODULE="on"
2020
ARG GOPROXY=""
2121
ARG GOFLAGS=""
2222
ARG CGO_ENABLED=0
23+
ARG DEBUG=0
24+
25+
ENV DEBUG=${DEBUG}
2326
ENV CGO_ENABLED=${CGO_ENABLED}
27+
ENV GOOS=${TARGETOS}
28+
ENV GOARCH=${TARGETARCH}
29+
ENV GO=/go/src/handler/go.sh
2430

31+
RUN chmod +x ${GO}
2532

2633
# Run a gofmt and exclude all vendored code.
2734
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; }
2835

2936
WORKDIR /go/src/handler/function
3037
RUN mkdir -p /go/src/handler/function/static
3138

32-
RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} go test ./... -cover
39+
RUN ${GO} test ./... -cover
3340

3441
WORKDIR /go/src/handler
35-
RUN CGO_ENABLED=${CGO_ENABLED} GOOS=${TARGETOS} GOARCH=${TARGETARCH} \
36-
go build --ldflags "-s -w" -a -installsuffix cgo -o handler .
42+
RUN ${GO} build --ldflags "-s -w" -a -installsuffix cgo -o handler .
3743

3844
FROM --platform=${TARGETPLATFORM:-linux/amd64} alpine:3.15
3945
# Add non root user and certs

template/golang-http/go.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/sh
2+
# go.sh is a wrapper for the Go command that will
3+
# automatically set the required module related flags
4+
# when a vendor folder is detected.
5+
#
6+
# Currently, in Go 1.18, Go Workspaces are incompatible
7+
# with vendored modules. As a result, we must disable
8+
# Go modules and use GOPATH mode.
9+
10+
# We use this bash script to wrap Go commands because
11+
# there is no clear way to set an env variable from the
12+
# a script in a Dockerfile.
13+
# It is possible to use `go env -w` but, but env varaibles
14+
# have precedence and if it is set as an arg/env variable,
15+
# then it will be ignored by the `go env -w`.
16+
17+
# if the function/vendor folder exists
18+
# then we set the env variables for
19+
# GO111MODULE=off
20+
if [ -d "/go/src/handler/function/vendor" ]; then
21+
echo "Setting vendor mode env variables"
22+
export GO111MODULE=off
23+
fi
24+
25+
# if DEBUG env is 1, print the go env
26+
if [ "${DEBUG:-0}" = "1" ]; then
27+
go env
28+
fi
29+
30+
go "$@"

template/golang-middleware/Dockerfile

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,26 @@ ARG GO111MODULE="on"
2020
ARG GOPROXY=""
2121
ARG GOFLAGS=""
2222
ARG CGO_ENABLED=0
23+
ARG DEBUG=0
24+
25+
ENV DEBUG=${DEBUG}
2326
ENV CGO_ENABLED=${CGO_ENABLED}
27+
ENV GOOS=${TARGETOS}
28+
ENV GOARCH=${TARGETARCH}
29+
ENV GO=/go/src/handler/go.sh
30+
31+
RUN chmod +x ${GO}
2432

2533
# Run a gofmt and exclude all vendored code.
2634
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; }
2735

2836
WORKDIR /go/src/handler/function
2937
RUN mkdir -p /go/src/handler/function/static
3038

31-
RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} go test ./... -cover
39+
RUN ${GO} test ./... -cover
3240

3341
WORKDIR /go/src/handler
34-
RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} \
35-
go build --ldflags "-s -w" -a -installsuffix cgo -o handler .
42+
RUN ${GO} build --ldflags "-s -w" -a -installsuffix cgo -o handler .
3643

3744
FROM --platform=${TARGETPLATFORM:-linux/amd64} alpine:3.15 as ship
3845

template/golang-middleware/go.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/sh
2+
# go.sh is a wrapper for the Go command that will
3+
# automatically set the required module related flags
4+
# when a vendor folder is detected.
5+
#
6+
# Currently, in Go 1.18, Go Workspaces are incompatible
7+
# with vendored modules. As a result, we must disable
8+
# Go modules and use GOPATH mode.
9+
10+
# We use this bash script to wrap Go commands because
11+
# there is no clear way to set an env variable from the
12+
# a script in a Dockerfile.
13+
# It is possible to use `go env -w` but, but env varaibles
14+
# have precedence and if it is set as an arg/env variable,
15+
# then it will be ignored by the `go env -w`.
16+
17+
# if the function/vendor folder exists
18+
# then we set the env variables for
19+
# GO111MODULE=off
20+
if [ -d "/go/src/handler/function/vendor" ]; then
21+
echo "Setting vendor mode env variables"
22+
export GO111MODULE=off
23+
fi
24+
25+
# if DEBUG env is 1, print the go env
26+
if [ "${DEBUG:-0}" = "1" ]; then
27+
go env
28+
fi
29+
30+
go "$@"

0 commit comments

Comments
 (0)