From 2b74e52b8241c5485673a0c7e09a14fe56d79949 Mon Sep 17 00:00:00 2001 From: Chris Date: Sat, 30 Mar 2024 01:37:04 +0200 Subject: [PATCH] Fully automated versioning --- .gitignore | 1 + Makefile | 35 ++++++++++---- cmd/buildnumber.go | 39 ---------------- cmd/buildversion.go | 78 +++++++++++++++++++++++++++++++ deploy/sthome-webhook/Chart.yaml | 5 +- deploy/sthome-webhook/values.yaml | 2 +- sthome/solver_local.go | 5 +- version.txt | 6 +-- 8 files changed, 113 insertions(+), 58 deletions(-) delete mode 100644 cmd/buildnumber.go create mode 100644 cmd/buildversion.go diff --git a/.gitignore b/.gitignore index 073347d..83392c2 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ charts _test icon vendor +_out diff --git a/Makefile b/Makefile index cc9a0b6..2aab649 100644 --- a/Makefile +++ b/Makefile @@ -2,8 +2,11 @@ GO ?= $(shell which go) OS ?= $(shell $(GO) env GOOS) ARCH ?= $(shell $(GO) env GOARCH) -IMAGE_NAME := "cert-manager-webhook-sthome" -IMAGE_TAG := "0.0.3-alpha.2" +IMAGE_NAME := cert-manager-webhook-sthome +IMAGE_TAG = "0.0.3" + +TMP = \Temp\gotemp +export TMP OUT := $(shell pwd)/_out @@ -13,7 +16,15 @@ HELM_FILES := $(shell find deploy/sthome-webhook) dependencies: go mod tidy - go mod vendor +# go mod vendor + +bin/buildversion.exe: cmd/buildversion.go + set TMP="C:\Temp\gotemp" + go build -o bin/buildversion.exe cmd/buildversion.go + set TMP="C:\Users\Chris\AppData\Local\Temp" + +version.txt: bin/buildversion.exe + bin/buildversion.exe test: _test/kubebuilder-$(KUBEBUILDER_VERSION)-$(OS)-$(ARCH)/etcd _test/kubebuilder-$(KUBEBUILDER_VERSION)-$(OS)-$(ARCH)/kube-apiserver _test/kubebuilder-$(KUBEBUILDER_VERSION)-$(OS)-$(ARCH)/kubectl TEST_ASSET_ETCD=_test/kubebuilder-$(KUBEBUILDER_VERSION)-$(OS)-$(ARCH)/etcd \ @@ -31,21 +42,25 @@ _test/kubebuilder-$(KUBEBUILDER_VERSION)-$(OS)-$(ARCH)/etcd _test/kubebuilder-$( clean: rm -r _test $(OUT) +.PHONY: package +package: rendered-manifest.yaml + helm package deploy\sthome-webhook -d \\\truenas\Shared_data\Chris\clusterissuer\charts\ + .PHONY: build -build: dependencies - go run cmd/buildnumber.go - docker build -t "$(IMAGE_NAME):$(IMAGE_TAG)" . - docker tag $(IMAGE_NAME) "docker.io/stuurmcp/$(IMAGE_NAME):$(IMAGE_TAG)" - docker image push "stuurmcp/$(IMAGE_NAME):$(IMAGE_TAG)" +build: rendered-manifest.yaml dependencies version.txt + docker build -t "$(IMAGE_NAME):$(shell head -n 1 version.txt)" . + docker tag $(IMAGE_NAME) "docker.io/stuurmcp/$(IMAGE_NAME):$(shell head -n 1 version.txt)" + docker image push "stuurmcp/$(IMAGE_NAME):$(shell head -n 1 version.txt)" + helm package deploy\sthome-webhook -d \\\truenas\Shared_data\Chris\clusterissuer\charts\ .PHONY: rendered-manifest.yaml rendered-manifest.yaml: $(OUT)/rendered-manifest.yaml $(OUT)/rendered-manifest.yaml: $(HELM_FILES) | $(OUT) helm template \ - --name sthome-webhook \ + sthome-webhook -n sthome-webhook2\ --set image.repository=$(IMAGE_NAME) \ - --set image.tag=$(IMAGE_TAG) \ + --set image.tag=$(shell head -n 1 version.txt) \ deploy/sthome-webhook > $@ _test $(OUT) _test/kubebuilder-$(KUBEBUILDER_VERSION)-$(OS)-$(ARCH): diff --git a/cmd/buildnumber.go b/cmd/buildnumber.go deleted file mode 100644 index af3bf14..0000000 --- a/cmd/buildnumber.go +++ /dev/null @@ -1,39 +0,0 @@ -package main - -import ( - "fmt" - "os" - "strconv" - "strings" - "time" -) - -var ( - version = "0.0.3-alpha.2" - buildTime string -) - -func main() { - // Load the file content - vFileData, _ := os.ReadFile("version.txt") - - // Convert from Byte array to string and split - // on newlines. We now have a slice of strings - vLines := strings.Split(string(vFileData), "\n") - - // Generate a timestamp. - buildTime = time.Now().Format("20060102-1504") - - // Load the count from the 3rd line of the file - // It's a string so we need to convert to integer - // Then increment it by 1 - bNum, _ := strconv.Atoi(vLines[2]) - bNum++ - - // Generate a single string to write back to the file - // Note, we didn't change the version string - outStr := version + "\n" + buildTime + "\n" + fmt.Sprint(bNum) - - // Write the data back to the file. - _ = os.WriteFile("version.txt", []byte(outStr), 0777) -} diff --git a/cmd/buildversion.go b/cmd/buildversion.go new file mode 100644 index 0000000..fbf3f5f --- /dev/null +++ b/cmd/buildversion.go @@ -0,0 +1,78 @@ +package main + +import ( + "fmt" + "log" + "os" + "strconv" + "strings" + "time" +) + +const ( + version = "0.0.3-alpha.2" + chartfile = "./deploy/sthome-webhook/Chart.yaml" + valuesfile = "./deploy/sthome-webhook/values.yaml" + tagprefix = " tag: " + versiontxt = "./version.txt" + apiVersion = "v1" + description = "Cert-Manager webhook for sthome" + name = "sthome-webhook" +) + +var ( + mfimagetag string + vfimagetag string + buildTime string + appVersion string + longversion string + versiontext string +) + +func main() { + // Load the file content + vFileData, _ := os.ReadFile("version.txt") + + // Convert from Byte array to string and split + // on newlines. We now have a slice of strings + vLines := strings.Split(string(vFileData), "\n") + + // Generate a timestamp. + buildTime = time.Now().Format("20060102-1504") + + // Load the count from the 3rd line of the file + // It's a string so we need to convert to integer + // Then increment it by 1 + bNum, _ := strconv.Atoi(vLines[2]) + bNum++ + longversion = version + "." + fmt.Sprint(bNum) + mfimagetag = longversion + appVersion = "v" + longversion + // Generate a single string to write back to the file + versiontext = longversion + "\n" + buildTime + "\n" + fmt.Sprint(bNum) + chartStr := "apiVersion: " + apiVersion + "\nappVersion: " + appVersion + "\ndescription: " + description + "\nname: " + name + "\nversion: " + longversion + "\n" + // Write the data back to the file. + _ = os.WriteFile(versiontxt, []byte(versiontext), 0777) + _ = os.WriteFile(chartfile, []byte(chartStr), 0777) + replacetxtfilelines(valuesfile, tagprefix, tagprefix+longversion) +} + +func replacetxtfilelines(filename string, textLinePrefix string, replacetext string) { + input, err := os.ReadFile(filename) + if err != nil { + log.Fatalln(err) + } + + lines := strings.Split(string(input), "\n") + + for i, line := range lines { + if strings.HasPrefix(line, textLinePrefix) { + lines[i] = replacetext + } + } + output := strings.Join(lines, "\n") + err = os.WriteFile(filename, []byte(output), 0644) + if err != nil { + log.Fatalln(err) + } +} diff --git a/deploy/sthome-webhook/Chart.yaml b/deploy/sthome-webhook/Chart.yaml index 3becbe2..626325f 100644 --- a/deploy/sthome-webhook/Chart.yaml +++ b/deploy/sthome-webhook/Chart.yaml @@ -1,6 +1,5 @@ apiVersion: v1 -appVersion: "v0.0.3-alpha.2" +appVersion: v0.0.3-alpha.2.39 description: Cert-Manager webhook for sthome name: sthome-webhook -version: 0.0.3-alpha.2 - +version: 0.0.3-alpha.2.39 diff --git a/deploy/sthome-webhook/values.yaml b/deploy/sthome-webhook/values.yaml index cbcc9e2..060662e 100644 --- a/deploy/sthome-webhook/values.yaml +++ b/deploy/sthome-webhook/values.yaml @@ -31,7 +31,7 @@ clusterIssuer: image: repository: stuurmcp/cert-manager-webhook-sthome #repository: wstat.sthome.net:5000/cert-manager-webhook-sthome - tag: 0.0.3-alpha.2 + tag: 0.0.3-alpha.2.39 #pullPolicy should be IfNotPresent. Set to Always for testing purposes pullPolicy: IfNotPresent diff --git a/sthome/solver_local.go b/sthome/solver_local.go index c509dee..a8f5c5d 100644 --- a/sthome/solver_local.go +++ b/sthome/solver_local.go @@ -86,7 +86,7 @@ func (loc *LocalDNSProviderSolver) Present(ch *v1alpha1.ChallengeRequest) error fmt.Sprintf("arg5=%s", ch.Key), } success, _ := Execute(dnsUpdaterScript, command) - klog.InfoS("CZ: Execute returned", "success", success) + klog.InfoS("CZ: Execute set returned", "success", success) return nil } @@ -108,7 +108,8 @@ func (loc *LocalDNSProviderSolver) CleanUp(ch *v1alpha1.ChallengeRequest) error "arg4=TXT", fmt.Sprintf("arg5=%s", ch.Key), } - Execute(dnsUpdaterScript, command) + success, _ := Execute(dnsUpdaterScript, command) + klog.InfoS("CZ: Execute unset returned", "success", success) return nil } diff --git a/version.txt b/version.txt index 554cdf5..73b701f 100644 --- a/version.txt +++ b/version.txt @@ -1,3 +1,3 @@ -0.0.3-alpha.2 -20240329-1707 -27 \ No newline at end of file +0.0.3-alpha.2.39 +20240330-0131 +39 \ No newline at end of file