File tree Expand file tree Collapse file tree 4 files changed +76
-7
lines changed
Expand file tree Collapse file tree 4 files changed +76
-7
lines changed Original file line number Diff line number Diff line change @@ -20,20 +20,24 @@ ARG GO111MODULE="on"
2020ARG GOPROXY=""
2121ARG GOFLAGS=""
2222ARG 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.
2732RUN 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
2934WORKDIR /go/src/handler/function
3035RUN mkdir -p /go/src/handler/function/static
3136
32- RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} go test ./... -cover
37+ RUN ${GO} test ./... -cover
3338
3439WORKDIR /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
3842FROM --platform=${TARGETPLATFORM:-linux/amd64} alpine:3.15
3943# Add non root user and certs
Original file line number Diff line number Diff line change 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 " $@ "
Original file line number Diff line number Diff line change @@ -20,19 +20,24 @@ ARG GO111MODULE="on"
2020ARG GOPROXY=""
2121ARG GOFLAGS=""
2222ARG CGO_ENABLED=0
23+ ARG DEBUG=0
24+
25+ ENV DEBUG=${DEBUG}
2326ENV 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.
2632RUN 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
2834WORKDIR /go/src/handler/function
2935RUN mkdir -p /go/src/handler/function/static
3036
31- RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} go test ./... -cover
37+ RUN ${GO} test ./... -cover
3238
3339WORKDIR /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
3742FROM --platform=${TARGETPLATFORM:-linux/amd64} alpine:3.15 as ship
3843
Original file line number Diff line number Diff line change 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 " $@ "
You can’t perform that action at this time.
0 commit comments