Skip to content

Commit

Permalink
chore: add version information to binaries at build time
Browse files Browse the repository at this point in the history
  • Loading branch information
le0m committed Sep 13, 2024
1 parent 514e343 commit 47509df
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 10 deletions.
17 changes: 11 additions & 6 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,15 @@ jobs:

- name: Build
env:
TAG: ${{ github.ref_name }}
CGO_ENABLED: 0
run: |
GOOS=linux GOARCH=amd64 go build -C ${{ matrix.module }} -ldflags '-s' -o ../build/print2pdf-${{ matrix.module }}-linux-amd64
GOOS=linux GOARCH=arm64 go build -C ${{ matrix.module }} -ldflags '-s' -o ../build/print2pdf-${{ matrix.module }}-linux-arm64
GOOS=darwin GOARCH=amd64 go build -C ${{ matrix.module }} -ldflags '-s' -o ../build/print2pdf-${{ matrix.module }}-darwin-amd64
GOOS=darwin GOARCH=arm64 go build -C ${{ matrix.module }} -ldflags '-s' -o ../build/print2pdf-${{ matrix.module }}-darwin-arm64
GOOS=windows GOARCH=amd64 go build -C ${{ matrix.module }} -ldflags '-s' -o ../build/print2pdf-${{ matrix.module }}-windows-amd64
GOOS=windows GOARCH=arm64 go build -C ${{ matrix.module }} -ldflags '-s' -o ../build/print2pdf-${{ matrix.module }}-windows-arm64
GOOS=linux GOARCH=amd64 go build -C ${{ matrix.module }} -ldflags "-s -X main.Version=${TAG#v}" -o ../build/print2pdf-${{ matrix.module }}-linux-amd64
GOOS=linux GOARCH=arm64 go build -C ${{ matrix.module }} -ldflags "-s -X main.Version=${TAG#v}" -o ../build/print2pdf-${{ matrix.module }}-linux-arm64
GOOS=darwin GOARCH=amd64 go build -C ${{ matrix.module }} -ldflags "-s -X main.Version=${TAG#v}" -o ../build/print2pdf-${{ matrix.module }}-darwin-amd64
GOOS=darwin GOARCH=arm64 go build -C ${{ matrix.module }} -ldflags "-s -X main.Version=${TAG#v}" -o ../build/print2pdf-${{ matrix.module }}-darwin-arm64
GOOS=windows GOARCH=amd64 go build -C ${{ matrix.module }} -ldflags "-s -X main.Version=${TAG#v}" -o ../build/print2pdf-${{ matrix.module }}-windows-amd64
GOOS=windows GOARCH=arm64 go build -C ${{ matrix.module }} -ldflags "-s -X main.Version=${TAG#v}" -o ../build/print2pdf-${{ matrix.module }}-windows-arm64
- name: Release
uses: softprops/action-gh-release@v2
Expand Down Expand Up @@ -111,10 +112,14 @@ jobs:

- name: Build and push
uses: docker/build-push-action@v6
env:
TAG: ${{ github.ref_name }}
with:
context: ${{ matrix.module }}
file: ${{ matrix.module }}/Dockerfile
platforms: linux/amd64,linux/arm64
build-args: |
VERSION=${TAG#v}
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ IMAGE_NAME ?= print2pdf
IMAGE_TAG ?= dev

bin-plain:
CGO_ENABLED=0 go build -ldflags '-s' -o build/print2pdf-plain plain/main.go
CGO_ENABLED=0 go build -C plain/ -ldflags '-s' -o ../build/print2pdf-plain

bin-lambda:
CGO_ENABLED=0 go build -ldflags '-s' -tags 'lambda.norpc' -o build/print2pdf-lambda lambda/main.go
CGO_ENABLED=0 go build -C lambda/ -ldflags '-s' -tags 'lambda.norpc' -o ../build/print2pdf-lambda

docker-plain:
docker build -t $(IMAGE_NAME):$(IMAGE_TAG) --file plain/Dockerfile plain/
Expand Down
3 changes: 2 additions & 1 deletion lambda/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
FROM --platform=$BUILDPLATFORM golang:1.22-alpine AS builder
ARG TARGETOS
ARG TARGETARCH
ARG VERSION=development

WORKDIR /app

COPY go.mod go.sum /app/
RUN go mod download

COPY *.go /app/
RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -ldflags '-s' -tags 'lambda.norpc' -o build/print2pdf
RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -ldflags "-s -X main.Version=${VERSION}" -tags 'lambda.norpc' -o build/print2pdf

###
# Download chromium for AWS Lambda
Expand Down
7 changes: 7 additions & 0 deletions lambda/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ type ResponseError struct {
Message string `json:"message"`
}

// Version string, set at build time.
var Version = "development"

// S3 bucket name. Required.
var BucketName = os.Getenv("BUCKET")

Expand All @@ -33,6 +36,10 @@ var CorsAllowedHosts = os.Getenv("CORS_ALLOWED_HOSTS")

// Init function checks for required environment variables.
func init() {
if len(os.Args) > 1 && slices.Contains([]string{"-v", "--version"}, os.Args[1]) {
fmt.Printf("Version: %s\n", Version)
os.Exit(0)
}
if BucketName == "" {
fmt.Fprintln(os.Stderr, "missing required environment variable BUCKET")
os.Exit(1)
Expand Down
3 changes: 2 additions & 1 deletion plain/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
FROM --platform=$BUILDPLATFORM golang:1.22-alpine AS builder
ARG TARGETOS
ARG TARGETARCH
ARG VERSION=development

WORKDIR /app

COPY go.mod go.sum /app/
RUN go mod download

COPY *.go /app/
RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -ldflags '-s' -o build/print2pdf
RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -ldflags "-s -X main.Version=${VERSION}" -o build/print2pdf

###
# Final image
Expand Down
7 changes: 7 additions & 0 deletions plain/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ type ResponseError struct {
Message string `json:"message"`
}

// Version string, set at build time.
var Version = "development"

// S3 bucket name. Required for "/v1/print" endpoint.
var BucketName = os.Getenv("BUCKET")

Expand All @@ -37,6 +40,10 @@ var CorsAllowedHosts = os.Getenv("CORS_ALLOWED_HOSTS")

// Init function set default values to environment variables.
func init() {
if len(os.Args) > 1 && slices.Contains([]string{"-v", "--version"}, os.Args[1]) {
fmt.Printf("Version: %s\n", Version)
os.Exit(0)
}
if Port == "" {
Port = "3000"
}
Expand Down

0 comments on commit 47509df

Please sign in to comment.