From 6541f19b67a2129669fb1b505d976cca44dcb714 Mon Sep 17 00:00:00 2001 From: Dario Tranchitella Date: Tue, 17 Nov 2020 19:20:31 +0100 Subject: [PATCH] Automating version pick-up according to current git version and minor Kustomize hotfixes (#135) --- .github/workflows/main.yml | 4 +++- Dockerfile | 18 ++++++++++++++---- Makefile | 4 ++-- config/manager/kustomization.yaml | 4 +--- config/manager/manager.yaml | 2 +- main.go | 5 +++-- version/version.go => version.go | 17 +++++++++++++++-- 7 files changed, 39 insertions(+), 15 deletions(-) rename version/version.go => version.go (74%) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 52e3f419..9638f111 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -33,7 +33,9 @@ jobs: k8s-version: ['v1.16.15', 'v1.17.11', 'v1.18.8', 'v1.19.1'] runs-on: ubuntu-18.04 steps: - - uses: actions/checkout@master + - uses: actions/checkout@v2 + with: + fetch-depth: 0 - name: Cache Go modules and Docker images uses: actions/cache@v1 env: diff --git a/Dockerfile b/Dockerfile index c41ab059..1e00430f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,15 +11,25 @@ RUN go mod download # Copy the go source COPY main.go main.go +COPY version.go version.go COPY api/ api/ COPY controllers/ controllers/ COPY pkg/ pkg/ -COPY version/ version/ - -ARG VERSION +COPY .git .git # Build -RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -ldflags "-X github.com/clastix/capsule/version.Version=${VERSION}" -o manager main.go +RUN git config --get remote.origin.url > /tmp/GIT_REPO && \ + git rev-parse --short HEAD > /tmp/GIT_HEAD_COMMIT && \ + git describe --abbrev=0 --tags > /tmp/GIT_LAST_TAG && \ + git rev-parse --short $(cat /tmp/GIT_LAST_TAG) > /tmp/GIT_TAG_COMMIT && \ + git diff $(cat /tmp/GIT_HEAD_COMMIT) $(cat /tmp/GIT_TAG_COMMIT) --quiet > /tmp/GIT_MODIFIED1 || echo '.dev' > /tmp/GIT_MODIFIED1 && \ + git diff --quiet > /tmp/GIT_MODIFIED2 || echo '.dirty' > /tmp/GIT_MODIFIED2 && \ + cat /tmp/GIT_MODIFIED1 /tmp/GIT_MODIFIED2 | tr -d '\n' > /tmp/GIT_MODIFIED && \ + date '+%Y-%m-%dT%H:%M:%S' > /tmp/BUILD_DATE &&\ + CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build \ + -gcflags "-N -l" \ + -ldflags "-X main.GitRepo=$(cat /tmp/GIT_REPO) -X main.GitTag=$(cat /tmp/GIT_LAST_TAG) -X main.GitCommit=$(cat /tmp/GIT_HEAD_COMMIT) -X main.GitDirty=$(cat /tmp/GIT_MODIFIED) -X main.BuildTime=$(cat /tmp/BUILD_DATE)" \ + -o manager # Use distroless as minimal base image to package the manager binary # Refer to https://github.com/GoogleContainerTools/distroless for more details diff --git a/Makefile b/Makefile index 1b4d351f..0d276c36 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ # Current Operator version -VERSION ?= 0.0.1 +VERSION ?= $$(git describe --abbrev=0 --tags) # Default bundle image tag BUNDLE_IMG ?= quay.io/clastix/capsule:$(VERSION)-bundle @@ -75,7 +75,7 @@ generate: controller-gen # Build the docker image docker-build: test - docker build . --build-arg=VERSION=${VERSION} -t ${IMG} + docker build . -t ${IMG} # Push the docker image docker-push: diff --git a/config/manager/kustomization.yaml b/config/manager/kustomization.yaml index a2407d5e..46147119 100644 --- a/config/manager/kustomization.yaml +++ b/config/manager/kustomization.yaml @@ -5,6 +5,4 @@ kind: Kustomization images: - name: controller newName: quay.io/clastix/capsule - newTag: 0.0.1 -- name: quay.io/clastix/capsule - newTag: 0.0.1 + newTag: v0.0.2 diff --git a/config/manager/manager.yaml b/config/manager/manager.yaml index bfefd7c4..0ff6002e 100644 --- a/config/manager/manager.yaml +++ b/config/manager/manager.yaml @@ -34,7 +34,7 @@ spec: valueFrom: fieldRef: fieldPath: metadata.namespace - image: quay.io/clastix/capsule:latest + image: controller imagePullPolicy: IfNotPresent name: manager resources: diff --git a/main.go b/main.go index e47304be..cb2ab9c3 100644 --- a/main.go +++ b/main.go @@ -47,7 +47,6 @@ import ( "github.com/clastix/capsule/pkg/webhook/tenant" "github.com/clastix/capsule/pkg/webhook/tenant_prefix" "github.com/clastix/capsule/pkg/webhook/utils" - "github.com/clastix/capsule/version" // +kubebuilder:scaffold:imports ) @@ -64,7 +63,9 @@ func init() { } func printVersion() { - setupLog.Info(fmt.Sprintf("Operator Version: %s", version.Version)) + setupLog.Info(fmt.Sprintf("Capsule Version %s %s%s", GitTag, GitCommit, GitDirty)) + setupLog.Info(fmt.Sprintf("Build from: %s", GitRepo)) + setupLog.Info(fmt.Sprintf("Build date: %s", BuildTime)) setupLog.Info(fmt.Sprintf("Go Version: %s", goRuntime.Version())) setupLog.Info(fmt.Sprintf("Go OS/Arch: %s/%s", goRuntime.GOOS, goRuntime.GOARCH)) } diff --git a/version/version.go b/version.go similarity index 74% rename from version/version.go rename to version.go index e9e0f2a4..e23c8c02 100644 --- a/version/version.go +++ b/version.go @@ -14,6 +14,19 @@ See the License for the specific language governing permissions and limitations under the License. */ -package version +package main -var Version = "dev" +//GitRepo ... +var GitRepo = "" + +//GitTag ... +var GitTag = "dev" + +//GitCommit ... +var GitCommit = "" + +//GitDirty ... +var GitDirty = "dirty" + +//BuildTime ... +var BuildTime = ""