From 9e0d5ab9a3a92264da2160482b507a31159f1721 Mon Sep 17 00:00:00 2001 From: Guilherme Cassolato Date: Tue, 3 Sep 2024 13:39:55 +0200 Subject: [PATCH] Build info (version and image tag) from the git ref name instead of using a local build-info file This makes the injection of build-info, particularly the version, in the binary consistent across local dev env and remote builds (ci). Signed-off-by: Guilherme Cassolato --- Makefile | 54 ++++++++++++++++++------------------------- RELEASE.md | 16 ++++--------- build.yaml | 3 --- hack/write-version.sh | 18 --------------- 4 files changed, 27 insertions(+), 64 deletions(-) delete mode 100644 build.yaml delete mode 100755 hack/write-version.sh diff --git a/Makefile b/Makefile index 7086554c..65fc1035 100644 --- a/Makefile +++ b/Makefile @@ -1,17 +1,24 @@ # Use bash as shell SHELL = /bin/bash -VERSION ?= 0.0.0 - # Use vi as default editor EDITOR ?= vi -# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set) -ifeq (,$(shell go env GOBIN)) -GOBIN=$(shell go env GOPATH)/bin +# Set version and image tag +ifeq ($(VERSION),) +VERSION = $(shell git rev-parse --abbrev-ref HEAD) +endif +ifeq ($(VERSION),main) +override VERSION = latest +endif +using_semantic_version := $(shell [[ $(VERSION) =~ ^[0-9]+\.[0-9]+\.[0-9]+(-.+)?$$ ]] && echo "true") +ifdef using_semantic_version +IMAGE_TAG=v$(VERSION) else -GOBIN=$(shell go env GOBIN) +IMAGE_TAG=local endif +IMAGE_REPO ?= authorino +AUTHORINO_IMAGE ?= $(IMAGE_REPO):$(IMAGE_TAG) PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST)))) export PATH := $(PROJECT_DIR)/bin:$(PATH) @@ -41,10 +48,6 @@ help: ##@ Dependencies -YQ = $(PROJECT_DIR)/bin/yq -yq: ## Download yq locally if necessary. - $(call go-get-tool,$(YQ),github.com/mikefarah/yq/v4@v4.34.2) - CONTROLLER_GEN = $(PROJECT_DIR)/bin/controller-gen controller-gen: ## Installs controller-gen in $PROJECT_DIR/bin $(call go-get-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen@v0.9.0) @@ -80,6 +83,13 @@ ifeq ($(SED),) exit 1 endif +# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set) +ifeq (,$(shell go env GOBIN)) +GOBIN=$(shell go env GOPATH)/bin +else +GOBIN=$(shell go env GOBIN) +endif + # go-get-tool will 'go install' any package $2 and install it to $1. define go-get-tool @[ -f $(1) ] || { \ @@ -115,38 +125,20 @@ manifests: controller-gen kustomize ## Generates the manifests in $PROJECT_DIR/i controller-gen crd:crdVersions=v1 rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=install/crd output:rbac:artifacts:config=install/rbac && $(KUSTOMIZE) build install > $(AUTHORINO_MANIFESTS) $(MAKE) patch-webhook -run: yq -run:$(shell $(PROJECT_DIR)/hack/write-version.sh) -run:version=$(shell $(YQ) '.build.version' build-recent.yaml) run:git_sha=$(shell git rev-parse HEAD) run:dirty=$(shell $(PROJECT_DIR)/hack/check-git-dirty.sh || echo "unknown") run: generate manifests ## Runs the application against the Kubernetes cluster configured in ~/.kube/config - go run -ldflags "-X main.version=$(version) -X main.gitSHA=${git_sha} -X main.dirty=${dirty}" ./main.go server + go run -ldflags "-X main.version=$(VERSION) -X main.gitSHA=${git_sha} -X main.dirty=${dirty}" ./main.go server -build: yq -build:$(shell $(PROJECT_DIR)/hack/write-version.sh) -build:version=$(shell $(YQ) '.build.version' build-recent.yaml) build:git_sha=$(shell git rev-parse HEAD) build:dirty=$(shell $(PROJECT_DIR)/hack/check-git-dirty.sh || echo "unknown") build: generate ## Builds the manager binary - CGO_ENABLED=0 GO111MODULE=on go build -a -ldflags "-X main.version=$(version) -X main.gitSHA=${git_sha} -X main.dirty=${dirty}" -o bin/authorino main.go - -IMAGE_REPO ?= authorino -using_semantic_version := $(shell [[ $(VERSION) =~ ^[0-9]+\.[0-9]+\.[0-9]+(-.+)?$$ ]] && echo "true") -ifdef using_semantic_version -IMAGE_TAG=v$(VERSION) -else -IMAGE_TAG=local -endif -AUTHORINO_IMAGE ?= $(IMAGE_REPO):$(IMAGE_TAG) + CGO_ENABLED=0 GO111MODULE=on go build -a -ldflags "-X main.version=$(VERSION) -X main.gitSHA=${git_sha} -X main.dirty=${dirty}" -o bin/authorino main.go -docker-build: yq -docker-build:$(shell $(PROJECT_DIR)/hack/write-version.sh) -docker-build:version=$(shell $(YQ) '.build.version' build-recent.yaml) docker-build:git_sha=$(shell git rev-parse HEAD) docker-build:dirty=$(shell $(PROJECT_DIR)/hack/check-git-dirty.sh || echo "unknown") docker-build: ## Builds an image based on the current branch - docker build --build-arg version=$(version) --build-arg git_sha=$(git_sha) --build-arg dirty=$(dirty) -t $(AUTHORINO_IMAGE) . + docker build --build-arg version=$(VERSION) --build-arg git_sha=$(git_sha) --build-arg dirty=$(dirty) -t $(AUTHORINO_IMAGE) . test: generate manifests envtest ## Runs the tests KUBEBUILDER_ASSETS='$(strip $(shell $(ENVTEST) use -p path 1.21.2 --os linux))' go test ./... -coverprofile cover.out diff --git a/RELEASE.md b/RELEASE.md index 4ea90d72..44729aa3 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -4,21 +4,13 @@ To release a version “vX.Y.Z” of Authorino in GitHub and Quay.io, follow these steps: -1. Create a release branch as source. +1. Pick a `` (SHA-1) as source. ```shell -git checkout release-vX.Y.Z +git checkout ``` -2. Change the `version` field in `build.yaml` to `vX.Y.Z` and save. - -```yaml -# This is where the build configuration is stored. -build: - version: vX.Y.Z -``` - -3. Create a new tag and named release `vX.Y.Z`. Push the tag to GitHub. +2. Create a new tag and named release `vX.Y.Z`. Push the tag to GitHub. ```shell git tag -a vX.Y.Z -m "vX.Y.Z" @@ -29,7 +21,7 @@ Then at the GitHub repository, create a new release from the tag you just pushed release notes and then write the change notes highlighting all the new features, bug fixes, enhancements, etc. ([example](https://github.com/Kuadrant/authorino/releases/tag/v0.9.0)). -4. Run the GHA ‘Build and push images’ for the `vX.Y.Z` tag. This will cause a new image to be built and pushed to quay.io/kuadrant/authorino. +3. Run the GHA ‘Build and push images’ for the `vX.Y.Z` tag. This will cause a new image to be built and pushed to quay.io/kuadrant/authorino. ## Notes on Authorino’s automated builds diff --git a/build.yaml b/build.yaml deleted file mode 100644 index 5faa3dbe..00000000 --- a/build.yaml +++ /dev/null @@ -1,3 +0,0 @@ -# This is where the build configuration is stored. -build: - version: latest diff --git a/hack/write-version.sh b/hack/write-version.sh deleted file mode 100755 index e11527ec..00000000 --- a/hack/write-version.sh +++ /dev/null @@ -1,18 +0,0 @@ -#!/usr/bin/env bash - -# Get the current git branch -branch=$(git rev-parse --abbrev-ref HEAD) - -# Check if the branch is "main" -if [[ "$branch" == "main" ]]; then - version="latest" -# Otherwise, use the branch name as the version -else - version="$branch" -fi - -# copy contents of build.yaml to build-recent.yaml -cp build.yaml build-recent.yaml - -# Use yq to set build.version to version variable -yq eval ".build.version = \"$version\"" -i build-recent.yaml