Skip to content

Commit 9a81df8

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 9a81df8

File tree

4 files changed

+76
-7
lines changed

4 files changed

+76
-7
lines changed

template/golang-http/Dockerfile

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,24 @@ ARG GO111MODULE="on"
2020
ARG GOPROXY=""
2121
ARG GOFLAGS=""
2222
ARG CGO_ENABLED=0
23-
ENV CGO_ENABLED=${CGO_ENABLED}
23+
ARG DEBUG=0
2424

25+
ENV DEBUG=${DEBUG}
26+
ENV CGO_ENABLED=${CGO_ENABLED}
27+
ENV GOOS=${TARGETOS}
28+
ENV GOARCH=${TARGETARCH}
29+
ENV GO=/go/src/handler/go.sh
2530

2631
# Run a gofmt and exclude all vendored code.
2732
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; }
2833

2934
WORKDIR /go/src/handler/function
3035
RUN mkdir -p /go/src/handler/function/static
3136

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

3439
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 .
40+
RUN ${GO} build --ldflags "-s -w" -a -installsuffix cgo -o handler .
3741

3842
FROM --platform=${TARGETPLATFORM:-linux/amd64} alpine:3.15
3943
# 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: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,24 @@ 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

2531
# Run a gofmt and exclude all vendored code.
2632
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; }
2733

2834
WORKDIR /go/src/handler/function
2935
RUN mkdir -p /go/src/handler/function/static
3036

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

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

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

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)