Skip to content

Commit

Permalink
Add short script to move vendor folder around
Browse files Browse the repository at this point in the history
**What**
- Add Dockerfile testp to mov ethe vendor folder up from the function
  volder to function root. This allows the user to control the vendor
  folder and avoid collisions/validation errors from Go in modules mode
- This new flow allows dynamic downloading of Go modules _or_ disabling
  Go modules and only using vendor. You can not use vendor _and_ modules
  at the same time due to how Go will validate that the vendor folder
  and the go.mod are insync

Signed-off-by: Lucas Roesler <[email protected]>
  • Loading branch information
LucasRoesler authored and alexellis committed May 5, 2021
1 parent 886beea commit 675f544
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 136 deletions.
35 changes: 24 additions & 11 deletions template/golang-http/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,54 @@ ARG TARGETARCH

RUN apk --no-cache add git

ENV CGO_ENABLED=0

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

ENV CGO_ENABLED=0

RUN mkdir -p /go/src/handler
WORKDIR /go/src/handler
COPY . .

ARG GO111MODULE="off"
ARG GOPROXY=""
ARG GOFLAGS=""

# Add user overrides to the root go.mod, which is the only place "replace" can be used
RUN cat function/GO_REPLACE.txt >> ./go.mod || exit 0
RUN if [ -d ./function/vendor ]; then \
echo "moving handler vendor" && \
mv -f ./function/vendor . && go mod tidy; \
else \
echo "using modules or vendor not found "; \
fi

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

ARG GO111MODULE="off"
ARG GOPROXY=""

WORKDIR /go/src/handler/function

RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} go test ./... -cover

WORKDIR /go/src/handler
RUN CGO_ENABLED=${CGO_ENABLED} GOOS=${TARGETOS} GOARCH=${TARGETARCH} \
go build --ldflags "-s -w" -a -installsuffix cgo -o handler .
RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} go test handler/function/... -cover

FROM --platform=${TARGETPLATFORM:-linux/amd64} alpine:3.13
# Add non root user and certs
RUN apk --no-cache add ca-certificates \
&& addgroup -S app && adduser -S -g app app
# Split instructions so that buildkit can run & cache
# Split instructions so that buildkit can run & cache
# the previous command ahead of time.
RUN mkdir -p /home/app \
&& chown app /home/app

WORKDIR /home/app

COPY --from=build /go/src/handler/handler .
COPY --from=build /usr/bin/fwatchdog .
COPY --from=build /go/src/handler/function/ .

RUN chown -R app /home/app
COPY --from=build --chown=app /go/src/handler/handler .
COPY --from=build --chown=app /usr/bin/fwatchdog .
COPY --from=build --chown=app /go/src/handler/function/ .

USER app

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

2 changes: 0 additions & 2 deletions template/golang-http/vendor/modules.txt

This file was deleted.

16 changes: 11 additions & 5 deletions template/golang-middleware/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,22 @@ RUN mkdir -p /go/src/handler
WORKDIR /go/src/handler
COPY . .

ARG GO111MODULE="off"
ARG GOPROXY=""
ARG GOFLAGS=""

# Add user overrides to the root go.mod, which is the only place "replace" can be used
RUN cat function/GO_REPLACE.txt >> ./go.mod || exit 0
RUN if [ -d ./function/vendor ] && [ "${GO111MODULE}" == "off" ]; then \
echo "moving handler vendor" && \
mv -f ./function/vendor .; \
else \
echo "using modules or vendor not found "; \
fi

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

ARG GO111MODULE="off"
ARG GOPROXY=""
ARG GOFLAGS=""

WORKDIR /go/src/handler/function

RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} go test ./... -cover
Expand All @@ -39,7 +45,7 @@ FROM --platform=${TARGETPLATFORM:-linux/amd64} alpine:3.13
# Add non root user and certs
RUN apk --no-cache add ca-certificates \
&& addgroup -S app && adduser -S -g app app
# Split instructions so that buildkit can run & cache
# Split instructions so that buildkit can run & cache
# the previous command ahead of time.
RUN mkdir -p /home/app \
&& chown app /home/app
Expand Down

0 comments on commit 675f544

Please sign in to comment.