Skip to content

Commit

Permalink
build: inject version into forego binary
Browse files Browse the repository at this point in the history
mirroring what's being done with docker-gen
  • Loading branch information
buchdag committed Dec 23, 2023
1 parent 66c4eac commit b9dae4f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ jobs:
uses: docker/build-push-action@v5
with:
context: .
build-args: FOREGO_VERSION=${{ steps.forego_version.outputs.VERSION }}
platforms: linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64,linux/ppc64le,linux/s390x
file: Dockerfile.${{ matrix.base }}
sbom: true
Expand Down
9 changes: 7 additions & 2 deletions Dockerfile.alpine
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
ARG FOREGO_VERSION=main

# Build forego
FROM --platform=$BUILDPLATFORM golang:1.21.5-alpine as go-builder

ENV CGO_ENABLED=0

ARG TARGETOS TARGETARCH TARGETVARIANT
ARG FOREGO_VERSION TARGETOS TARGETARCH TARGETVARIANT
ENV GOOS=$TARGETOS GOARCH=$TARGETARCH VARIANT=$TARGETVARIANT

RUN apk add --no-cache musl-dev
Expand All @@ -21,11 +23,14 @@ RUN set -eu; \
*) [ -z "$VARIANT" ] ;; \
esac; \
go env | grep -E 'OS=|ARCH=|ARM=|AMD64='; \
go build -o forego .; \
go build -ldflags "-X main.buildVersion=${FOREGO_VERSION}" -o forego .; \
go clean -cache

FROM --platform=$TARGETPLATFORM alpine:3.19.0

ARG FOREGO_VERSION
ENV FOREGO_VERSION=${FOREGO_VERSION}

RUN apk add --no-cache bash

# Install Forego
Expand Down
9 changes: 7 additions & 2 deletions Dockerfile.debian
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
ARG FOREGO_VERSION=main

# Build forego
FROM --platform=$BUILDPLATFORM golang:1.21.5 as go-builder

ENV CGO_ENABLED=0

ARG TARGETOS TARGETARCH TARGETVARIANT
ARG FOREGO_VERSION TARGETOS TARGETARCH TARGETVARIANT
ENV GOOS=$TARGETOS GOARCH=$TARGETARCH VARIANT=$TARGETVARIANT

WORKDIR /build
Expand All @@ -19,11 +21,14 @@ RUN set -eu; \
*) [ -z "$VARIANT" ] ;; \
esac; \
go env | grep -E 'OS=|ARCH=|ARM=|AMD64='; \
go build -o forego .; \
go build -ldflags "-X main.buildVersion=${FOREGO_VERSION}" -o forego .; \
go clean -cache

FROM --platform=$TARGETPLATFORM debian:12.4-slim

ARG FOREGO_VERSION
ENV FOREGO_VERSION=${FOREGO_VERSION}

# Install Forego
COPY --from=go-builder /build/forego /usr/local/bin/forego

Expand Down
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
BIN = forego
SRC = $(shell find . -name '*.go')

TAG:=`git describe --tags`
LDFLAGS:=-X main.buildVersion=$(TAG)

.PHONY: all build clean lint test

all: build
Expand All @@ -20,4 +23,4 @@ test: lint get-deps build
go test -v -race -cover ./...

$(BIN): $(SRC)
go build -o $@
go build -ldflags "$(LDFLAGS)" -o $@

0 comments on commit b9dae4f

Please sign in to comment.