File tree Expand file tree Collapse file tree 4 files changed +79
-6
lines changed
Expand file tree Collapse file tree 4 files changed +79
-6
lines changed Original file line number Diff line number Diff line change @@ -20,20 +20,26 @@ 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
31+ RUN chmod +x ${GO}
2532
2633# Run a gofmt and exclude all vendored code.
2734RUN 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
2936WORKDIR /go/src/handler/function
3037RUN mkdir -p /go/src/handler/function/static
3138
32- RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} go test ./... -cover
39+ RUN ${GO} test ./... -cover
3340
3441WORKDIR /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
3844FROM --platform=${TARGETPLATFORM:-linux/amd64} alpine:3.15
3945# 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,26 @@ 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
30+
31+ RUN chmod +x ${GO}
2432
2533# Run a gofmt and exclude all vendored code.
2634RUN 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
2836WORKDIR /go/src/handler/function
2937RUN mkdir -p /go/src/handler/function/static
3038
31- RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} go test ./... -cover
39+ RUN ${GO} test ./... -cover
3240
3341WORKDIR /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
3744FROM --platform=${TARGETPLATFORM:-linux/amd64} alpine:3.15 as ship
3845
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