Skip to content

Commit a452bc3

Browse files
committed
Set build flags in makefile
1 parent cba03dd commit a452bc3

File tree

3 files changed

+16
-21
lines changed

3 files changed

+16
-21
lines changed

.github/workflows/build.yml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,6 @@ jobs:
5555
if: ${{ startsWith(github.ref, 'refs/heads/master') || startsWith(github.ref, 'refs/tags/v') }}
5656
run: echo "DOCKER_BUILD_PUSH=true" >> $GITHUB_ENV
5757

58-
- name: Store commit in environment variable
59-
run: echo "COMMIT=${{ github.sha }}" >> $GITHUB_ENV
60-
61-
- name: Setup build flags
62-
run: |
63-
export LD_FLAGS="-w -s -X main.Version=$VERSION -X main.BuildDate=$(date "+%F-%T") -X main.Commit=$(git rev-parse --verify HEAD) -X main.Mode=prod"
64-
echo "LD_FLAGS=$LD_FLAGS" >> $GITHUB_ENV
65-
6658
- name: Set up Docker Buildx
6759
uses: docker/setup-buildx-action@v3
6860

Makefile

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
SHELL := /bin/bash
22

3-
# Env var inputs for container builds
3+
# Env var inputs for container image builds
44
# REGISTRY: The registry to which the build image should be pushed to. Defaults to "docker.io" (Docker Hub)
55
# IMAGE: The name of the image to build and publish in the afore mentioned registry. Defaults to "gotify/cli"
66
# PLATFORM: The platform for which the image should be built, defaults to amd64, arm64, i386, arm/v7 and riscv64
77
REGISTRY ?= docker.io
88
IMAGE ?= gotify/cli
99
PLATFORM ?= linux/amd64,linux/arm64,linux/386,linux/arm/v7,linux/riscv64
1010

11+
# Env var inputs for all builds
12+
# VERSION: The version for which the container image or the binary is being built. This variable has not default.
13+
# When it is not provided, no version will be specified in the built package.
14+
# COMMIT: The commit of this project for which the cli is being built, for reference in the tool's "version" command.
15+
# Default to git's HEAD
16+
# LD_FLAGS: Build flags, for the tool's "version" command. Defaults to current date, commit and version (if any).
17+
COMMIT ?= $(shell git rev-parse --verify HEAD)
18+
LD_FLAGS ?= $(if $(VERSION),-X main.Version=${VERSION}) \
19+
-X main.BuildDate=$(shell date "+%F-%T") \
20+
-X main.Commit=${COMMIT}
21+
1122
# Resolve go version
1223
ifdef GOTOOLCHAIN
1324
GO_VERSION=$(GOTOOLCHAIN)
@@ -16,31 +27,24 @@ else
1627
endif
1728
DOCKER_GO_BUILD=go build -mod=readonly -a -installsuffix cgo -ldflags "$$LD_FLAGS"
1829

19-
require-commit:
20-
if [ -z ${COMMIT} ]; then echo "Need to set COMMIT" && exit 1; fi;
21-
22-
build-docker-multiarch: require-commit
30+
build-docker-multiarch:
2331
docker buildx build \
24-
--sbom=true \
25-
--provenance=true \
2632
$(if $(DOCKER_BUILD_PUSH),--push) \
2733
-t ${REGISTRY}/${IMAGE}:${COMMIT} \
2834
$(if $(VERSION),-t ${REGISTRY}/${IMAGE}:latest) \
2935
$(if $(VERSION),-t ${REGISTRY}/${IMAGE}:${VERSION}) \
3036
$(if $(VERSION),-t ${REGISTRY}/${IMAGE}:$(shell echo $(VERSION) | cut -d '.' -f -2)) \
3137
$(if $(VERSION),-t ${REGISTRY}/${IMAGE}:$(shell echo $(VERSION) | cut -d '.' -f -1)) \
3238
--build-arg GO_VERSION=$(GO_VERSION) \
33-
--build-arg LD_FLAGS="$$LD_FLAGS" \
39+
--build-arg LD_FLAGS="$(LD_FLAGS)" \
3440
--platform $(PLATFORM) \
3541
-f docker/Dockerfile .
3642

37-
_build_within_docker: OUTPUT = gotify-app
43+
_build_within_docker: OUTPUT = gotify-cli
3844
_build_within_docker:
3945
${DOCKER_GO_BUILD} -o ${OUTPUT}
4046

4147
build:
42-
if [ '$(shell echo "${GIT_TAG}" | cut -c 1 )' != 'v' ]; then exit 1; fi;
43-
$(eval LD_FLAGS := -X main.Version=$(shell echo ${GIT_TAG} | cut -c 2-) -X main.BuildDate=$(shell date "+%F-%T") -X main.Commit=$(shell git rev-parse --verify HEAD))
4448
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags="${LD_FLAGS}" -o build/gotify-cli-windows-amd64.exe cli.go
4549
CGO_ENABLED=0 GOOS=windows GOARCH=386 go build -ldflags="${LD_FLAGS}" -o build/gotify-cli-windows-386.exe cli.go
4650
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="${LD_FLAGS}" -o build/gotify-cli-linux-amd64 cli.go

docker/Dockerfile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ ARG __TARGETPLATFORM_DASHES=${TARGETPLATFORM/\//-}
1111
ARG __TARGETPLATFORM_GO_NOTATION=${__TARGETPLATFORM_DASHES/arm\/v7/arm-7}
1212

1313
# --- Go Builder ---
14-
15-
FROM --platform=${BUILDPLATFORM} docker.io/golang:1.24 AS builder
14+
FROM --platform=${BUILDPLATFORM} docker.io/golang:${GO_VERSION} AS builder
1615

1716
ARG LD_FLAGS=""
1817
ENV DEBIAN_FRONTEND=noninteractive

0 commit comments

Comments
 (0)