Skip to content

Commit

Permalink
build: simplify image and remove upx compression
Browse files Browse the repository at this point in the history
  • Loading branch information
upsetbit committed Jun 14, 2024
1 parent fe17d38 commit 093eba8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 27 deletions.
39 changes: 18 additions & 21 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
FROM golang:1.21.2-alpine3.18 AS image
# --------------------------------------------------------------------------------------------------
FROM golang:1.22.4-alpine3.20 AS build
LABEL maintainer="Caian Ertl <hi@caian.org>"

FROM image AS base
ENV GOCACHE=/root/.cache/go-build
WORKDIR /go/app
COPY go.mod .
COPY go.sum .
RUN go mod download \
&& go list -m all \
| tail -n +2 \
| cut -f 1 -d " " \
| awk 'NF{print $0 "/..."}' \
| CGO_ENABLED=0 GOOS=linux xargs -n1 \
go build -v -trimpath -installsuffix cgo -i; echo "done"

FROM base AS build
WORKDIR /go/app
COPY . .
RUN CGO_ENABLED=0 \
GOOS=linux \
go build -v -trimpath -installsuffix cgo -o list-github-stars -ldflags "-s -w"
RUN apk update \
&& apk add --no-cache "make=~4.4" \
&& rm -rf /var/cache/apk/*

COPY Makefile .
COPY go.* .
RUN go mod download

COPY main.go .
RUN --mount=type=cache,target="/root/.cache/go-build" make release

FROM alpine:3.18.4 AS runtime
COPY --from=build /go/app/list-github-stars /usr/local/bin
ENTRYPOINT ["list-github-stars"]
# --------------------------------------------------------------------------------------------------
FROM scratch
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=build /go/app/lgs /
ENTRYPOINT ["/lgs"]
16 changes: 10 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
.DEFAULT_GOAL := build

ARTIFACT = list-github-stars
LDFLAGS = -w -s
ARTIFACT = lgs

ifeq ($(OS),Windows_NT)
ARTIFACT = list-github-stars.exe
ARTIFACT = lgs.exe
endif


build:
@go build
@go build -o $(ARTIFACT)

release:
@go build -ldflags "$(LDFLAGS)"
@upx --best --lzma $(ARTIFACT)
@CGO_ENABLED=0 \
go build \
-v \
-trimpath \
-gcflags=all="-l -B" \
-ldflags="-w -s" \
-o $(ARTIFACT)

run: build
@./$(ARTIFACT)
Expand Down

0 comments on commit 093eba8

Please sign in to comment.