Skip to content

Commit

Permalink
Merge pull request #409 from fenollp/moar-dockerfile
Browse files Browse the repository at this point in the history
 add Dockerfile target to build from source
  • Loading branch information
bojand authored Apr 15, 2023
2 parents f3ac855 + e1e0907 commit c1e6f86
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 14 deletions.
57 changes: 43 additions & 14 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,32 @@
# syntax=docker.io/docker/dockerfile:1@sha256:42399d4635eddd7a9b8a24be879d2f9a930d0ed040a61324cfdf59ef1357b3b2
# syntax=docker.io/docker/dockerfile:1.3-labs@sha256:250ce669e1aeeb5ffb892b18039c3f0801466536cb4210c8eb2638e628859bfd

FROM --platform=$BUILDPLATFORM docker.io/library/alpine:3.17 AS alpine
FROM --platform=$BUILDPLATFORM docker.io/library/golang@sha256:403f48633fb5ebd49f9a2b6ad6719f912df23dae44974a0c9445be331e72ff5e AS golang
FROM --platform=$BUILDPLATFORM gcr.io/distroless/base:nonroot@sha256:e406b1da09bc455495417a809efe48a03c48011a89f6eb57b0ab882508021c0d AS distroless


FROM golang AS builder
WORKDIR /app
ARG TARGETOS TARGETARCH
ENV CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH
COPY go.??? .
RUN \
--mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build <<EOF
set -eux
go mod download
EOF
COPY . .
RUN \
--mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build <<EOF
set -eux
go build -mod=readonly -o ./dist/ghz -a -installsuffix cgo -ldflags '-w -s -extldflags "-static"' ./cmd/ghz/...
EOF

FROM scratch AS ghz-binary-built
COPY --from=builder /app/dist/ghz /

FROM --platform=$BUILDPLATFORM docker.io/library/alpine:3.16 AS alpine
FROM --platform=$BUILDPLATFORM gcr.io/distroless/base:nonroot@sha256:02f667185ccf78dbaaf79376b6904aea6d832638e1314387c2c2932f217ac5cb AS distroless

FROM alpine AS osmap-linux
RUN echo linux >/os
Expand All @@ -13,18 +38,22 @@ FROM osmap-$TARGETOS AS osmap

FROM alpine AS fetcher
WORKDIR /app
ARG VERSION
RUN \
--mount=from=osmap,source=/os,target=/os \
set -ux \
&& apk add --no-cache curl \
&& export url=https://github.com/bojand/ghz/releases \
&& export arch=x86_64 \
&& export VERSION=$( ( curl -#fSLo /dev/null -w '%{url_effective}' $url/latest && echo ) | while read -r x; do basename $x; done) \
&& curl -#fSLo exe.tar.gz $url/download/$VERSION/ghz-$(cat /os)-$arch.tar.gz \
&& curl -#fSLo sha2 $url/download/$VERSION/ghz-$(cat /os)-$arch.tar.gz.sha256 \
&& sha256sum exe.tar.gz | grep -F $(cat sha2) \
&& tar xvf exe.tar.gz \
&& rm ghz-web* && mkdir exe && mv ghz* exe/
--mount=from=osmap,source=/os,target=/os <<EOF
set -eux
apk add --no-cache curl
export url=https://github.com/bojand/ghz/releases
export arch=x86_64
if [ "${VERSION:-}" = '' ]; then
export VERSION=$( ( curl -#fSLo /dev/null -w '%{url_effective}' $url/latest && echo ) | while read -r x; do basename $x; done)
fi
curl -#fSLo exe.tar.gz $url/download/$VERSION/ghz-$(cat /os)-$arch.tar.gz
curl -#fSLo sha2 $url/download/$VERSION/ghz-$(cat /os)-$arch.tar.gz.sha256
sha256sum exe.tar.gz | grep -F $(cat sha2)
tar xvf exe.tar.gz
rm ghz-web* && mkdir exe && mv ghz* exe/
EOF

FROM scratch AS ghz-binary
COPY --from=fetcher /app/exe/* /
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ go build .
go install github.com/bojand/ghz/cmd/ghz@latest
```

**Install using Docker**

```sh
DOCKER_BUILDKIT=1 docker build --output=/usr/local/bin --target=ghz-binary-built https://github.com/bojand/ghz.git
```

## Usage

```
Expand Down

0 comments on commit c1e6f86

Please sign in to comment.