From 7a7d9040e7667319835a7d149d13d4d603604965 Mon Sep 17 00:00:00 2001 From: ehearneredhat Date: Fri, 28 Jun 2024 09:39:26 +0100 Subject: [PATCH] add build-info (version, git sha, dirty) to the binary Signed-off-by: ehearneredhat Signed-off-by: Guilherme Cassolato --- .github/workflows/build-images.yaml | 16 +++++++--- .gitignore | 1 + Dockerfile | 9 ++++-- Makefile | 47 +++++++++++++++++----------- RELEASE.md | 10 +++--- api/v1beta1/zz_generated.deepcopy.go | 4 +-- api/v1beta2/zz_generated.deepcopy.go | 4 +-- hack/check-git-dirty.sh | 15 +++++++++ main.go | 10 ++++-- 9 files changed, 80 insertions(+), 36 deletions(-) create mode 100755 hack/check-git-dirty.sh diff --git a/.github/workflows/build-images.yaml b/.github/workflows/build-images.yaml index be9a9458..1d987b28 100644 --- a/.github/workflows/build-images.yaml +++ b/.github/workflows/build-images.yaml @@ -30,14 +30,18 @@ jobs: id: add-branch-tag run: | echo "IMG_TAGS=${GITHUB_REF_NAME/\//-} ${{ env.IMG_TAGS }}" >> $GITHUB_ENV - - name: Set Authorino version - id: authorino-version + - name: Set Authorino build info + id: authorino-build-info run: | if [[ ${GITHUB_REF_NAME/\//-} =~ ^v[0-9]+\.[0-9]+\.[0-9]+(-.+)?$ ]]; then - echo "VERSION=${GITHUB_REF_NAME/\//-}" >> $GITHUB_ENV + tag=${GITHUB_REF_NAME/\//-} + echo "version=${tag#v}" >> $GITHUB_ENV + elif [[ ${GITHUB_REF_NAME/\//-} == "main" ]]; then + echo "version=latest" >> $GITHUB_ENV else - echo "VERSION=${{ github.sha }}" >> $GITHUB_ENV + echo "version=${{ github.ref_name }}" >> $GITHUB_ENV fi + echo "git_sha=${{ github.sha }}" >> $GITHUB_ENV - name: Set up QEMU uses: docker/setup-qemu-action@v3 - name: Build Image @@ -48,7 +52,9 @@ jobs: tags: ${{ env.IMG_TAGS }} platforms: linux/amd64,linux/arm64,linux/s390x,linux/ppc64le build-args: | - version=${{ env.VERSION }} + git_sha=${{ env.git_sha }} + version=${{ env.version }} + dirty=${{ env.dirty }} containerfiles: | ./Dockerfile - name: Push Image diff --git a/.gitignore b/.gitignore index 423f9c0c..b8fbacc8 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,4 @@ vendor tmp target .scannerwork +build-recent.yaml diff --git a/Dockerfile b/Dockerfile index f4a4f541..010a6d26 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,8 +4,13 @@ FROM registry.access.redhat.com/ubi9/go-toolset:1.21 AS builder USER root WORKDIR /usr/src/authorino COPY ./ ./ -ARG version=latest -RUN CGO_ENABLED=0 GO111MODULE=on go build -a -ldflags "-X main.version=${version}" -o /usr/bin/authorino main.go +ARG version +ENV version=${version:-unknown} +ARG git_sha +ENV git_sha=${git_sha:-unknown} +ARG dirty +ENV dirty=${dirty:-unknown} +RUN CGO_ENABLED=0 GO111MODULE=on go build -a -ldflags "-X main.version=${version} -X main.gitSHA=${git_sha} -X main.dirty=${dirty}" -o /usr/bin/authorino main.go # Use Red Hat minimal base image to package the binary # https://catalog.redhat.com/software/containers/ubi9-minimal diff --git a/Makefile b/Makefile index 2cf43cdf..17d17838 100644 --- a/Makefile +++ b/Makefile @@ -1,18 +1,24 @@ # Use bash as shell SHELL = /bin/bash -# Authorino version -VERSION = $(shell git rev-parse HEAD) - # 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) @@ -77,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) ] || { \ @@ -112,22 +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: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)" ./main.go server + go run -ldflags "-X main.version=$(VERSION) -X main.gitSHA=${git_sha} -X main.dirty=${dirty}" ./main.go server +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)" -o bin/authorino main.go + 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) +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) -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 cd4aa156..44729aa3 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -2,7 +2,7 @@ ## Process -To release a version “v0.X.Y” of Authorino in GitHub and Quay.io, follow these steps: +To release a version “vX.Y.Z” of Authorino in GitHub and Quay.io, follow these steps: 1. Pick a `` (SHA-1) as source. @@ -10,18 +10,18 @@ To release a version “v0.X.Y” of Authorino in GitHub and Quay.io, follow the git checkout ``` -2. Create a new tag and named release `v0.X.Y`. 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 v0.X.Y -m "v0.X.Y" -git push origin v0.X.Y +git tag -a vX.Y.Z -m "vX.Y.Z" +git push origin vX.Y.Z ``` Then at the GitHub repository, create a new release from the tag you just pushed. One could start autogenerating the 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)). -3. Run the GHA ‘Build and push images’ for the `v0.X.Y` 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/api/v1beta1/zz_generated.deepcopy.go b/api/v1beta1/zz_generated.deepcopy.go index 3fe74ccf..c70bb9df 100644 --- a/api/v1beta1/zz_generated.deepcopy.go +++ b/api/v1beta1/zz_generated.deepcopy.go @@ -1,4 +1,5 @@ //go:build !ignore_autogenerated +// +build !ignore_autogenerated /* Copyright 2020 Red Hat, Inc. @@ -122,8 +123,7 @@ func (in *AuthConfigSpec) DeepCopyInto(out *AuthConfigSpec) { if val == nil { (*out)[key] = nil } else { - inVal := (*in)[key] - in, out := &inVal, &outVal + in, out := &val, &outVal *out = make(JSONPatternExpressions, len(*in)) copy(*out, *in) } diff --git a/api/v1beta2/zz_generated.deepcopy.go b/api/v1beta2/zz_generated.deepcopy.go index 29171143..3647917e 100644 --- a/api/v1beta2/zz_generated.deepcopy.go +++ b/api/v1beta2/zz_generated.deepcopy.go @@ -1,4 +1,5 @@ //go:build !ignore_autogenerated +// +build !ignore_autogenerated /* Copyright 2020 Red Hat, Inc. @@ -136,8 +137,7 @@ func (in *AuthConfigSpec) DeepCopyInto(out *AuthConfigSpec) { if val == nil { (*out)[key] = nil } else { - inVal := (*in)[key] - in, out := &inVal, &outVal + in, out := &val, &outVal *out = make(PatternExpressions, len(*in)) copy(*out, *in) } diff --git a/hack/check-git-dirty.sh b/hack/check-git-dirty.sh new file mode 100755 index 00000000..a32333d5 --- /dev/null +++ b/hack/check-git-dirty.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash + +if ! command -v git &>/dev/null +then + echo "git not found..." >&2 + exit 1 +fi + +if output=$(git diff --stat 2>/dev/null) +then +[ -n "$output" ] && echo "true" || echo "false" +else + # Not a git repository + exit 1 +fi diff --git a/main.go b/main.go index c09243b5..dddc07ee 100644 --- a/main.go +++ b/main.go @@ -73,6 +73,8 @@ const ( var ( // ldflags version string + dirty string + gitSHA string scheme = runtime.NewScheme() logger logr.Logger @@ -373,7 +375,7 @@ func runWebhookServer(cmd *cobra.Command, _ []string) { func setup(cmd *cobra.Command, log logOptions, telemetry telemetryOptions) { setupLogger(log) - logger.Info("booting up authorino", "version", version, "cmd", cmd.Use) + logger.Info("build information", "version", version, "commit", gitSHA, "dirty", dirty, "cmd", cmd.Use) // log the command-line args if logger.V(1).Enabled() { @@ -562,5 +564,9 @@ func timeoutMs(timeout int) time.Duration { } func printVersion(_ *cobra.Command, _ []string) { - fmt.Println("Authorino", version) + if dirty == "true" { + fmt.Printf("Authorino %s (%s-dirty)\n", version, gitSHA) + } else { + fmt.Printf("Authorino %s (%s)\n", version, gitSHA) + } }