Skip to content

Commit e16dac9

Browse files
author
Raisa Varghese
committed
fix(RHTAP-5734): Display the commit id in the application version
- ldflags added for commit id - Makefile updated to have full commit id - Makefile updated to use goreleaser build - Set the default value for image as <quay location>:<commit>/<latest> for mcp-server command - tekton task added for getting the version tag from repo Assisted-By: Cursor Signed-off-by: Raisa Varghese <[email protected]>
1 parent 859e82c commit e16dac9

File tree

9 files changed

+99
-13
lines changed

9 files changed

+99
-13
lines changed

.goreleaser.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ builds:
1919
- amd64
2020
ldflags:
2121
- -X github.com/redhat-appstudio/tssc-cli/pkg/constants.Version={{.Version}}
22+
- -X github.com/redhat-appstudio/tssc-cli/pkg/constants.Commit={{.Commit}}

.tekton/tssc-cli-pull-request.yaml

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ spec:
3131
value: 5d
3232
- name: dockerfile
3333
value: Dockerfile
34+
- name: version
35+
value: ""
36+
- name: prefetch-input
37+
value: ""
3438
- name: path-context
3539
value: .
3640
pipelineSpec:
@@ -84,10 +88,10 @@ spec:
8488
description: Execute the build with network isolation
8589
name: hermetic
8690
type: string
87-
- default: ""
88-
description: Build dependencies to be prefetched by Cachi2
91+
- description: Build dependencies to be prefetched by Cachi2
8992
name: prefetch-input
9093
type: string
94+
default: ""
9195
- default: ""
9296
description: Image tag expiration time, time values could be something like 1h, 2d, 3w for hours, days, and weeks, respectively.
9397
name: image-expires-after
@@ -176,6 +180,65 @@ spec:
176180
workspaces:
177181
- name: basic-auth
178182
workspace: git-auth
183+
- name: get-version
184+
params:
185+
- name: url
186+
value: $(params.git-url)
187+
- name: revision
188+
value: $(params.revision)
189+
runAfter:
190+
- clone-repository
191+
taskSpec:
192+
description: Extract version from git repository by cloning it
193+
params:
194+
- name: url
195+
description: Git repository URL
196+
type: string
197+
- name: revision
198+
description: Git revision to checkout
199+
type: string
200+
results:
201+
- name: version
202+
description: Git version/tag
203+
workspaces:
204+
- name: basic-auth
205+
description: Workspace for git authentication credentials
206+
optional: true
207+
steps:
208+
- name: clone-and-get-version
209+
image: quay.io/konflux-ci/appstudio-utils:ab6b0b8e40e440158e7288c73aff1cf83a2cc8a9@sha256:24179f0efd06c65d16868c2d7eb82573cce8e43533de6cea14fec3b7446e0b14
210+
env:
211+
- name: GIT_URL
212+
value: $(params.url)
213+
- name: GIT_REVISION
214+
value: $(params.revision)
215+
script: |
216+
#!/usr/bin/env bash
217+
set -eo pipefail
218+
219+
# Create temporary directory
220+
WORKDIR=$(mktemp -d)
221+
cd "$WORKDIR"
222+
223+
# Clone repository with tags
224+
echo "Cloning repository..."
225+
# Use unshallow clone to get full history and tags for git describe
226+
git clone --no-single-branch "$GIT_URL" .
227+
228+
# Get version using git describe (same logic as Makefile)
229+
# Try to get the closest tag, or use commit SHA as fallback
230+
VERSION=$(git describe --tags --always 2>/dev/null || echo "v0.0.0-SNAPSHOT")
231+
232+
echo "Computed version: $VERSION"
233+
printf "%s" "$VERSION" > $(results.version.path)
234+
when:
235+
- input: $(tasks.init.results.build)
236+
operator: in
237+
values:
238+
- "true"
239+
workspaces:
240+
- name: basic-auth
241+
workspace: git-auth
179242
- name: prefetch-dependencies
180243
params:
181244
- name: input
@@ -206,6 +269,10 @@ spec:
206269
params:
207270
- name: IMAGE
208271
value: $(params.output-image)
272+
- name: revision
273+
value: $(params.revision)
274+
- name: version
275+
value: $(tasks.get-version.results.version)
209276
- name: DOCKERFILE
210277
value: $(params.dockerfile)
211278
- name: CONTEXT
@@ -220,6 +287,8 @@ spec:
220287
value: $(tasks.clone-repository.results.commit)
221288
- name: BUILD_ARGS
222289
value:
290+
- revision=$(params.revision)
291+
- version=$(tasks.get-version.results.version)
223292
- $(params.build-args[*])
224293
- name: BUILD_ARGS_FILE
225294
value: $(params.build-args-file)
@@ -233,6 +302,7 @@ spec:
233302
value: $(params.buildah-format)
234303
runAfter:
235304
- prefetch-dependencies
305+
- get-version
236306
taskRef:
237307
params:
238308
- name: name

.tekton/tssc-cli-push.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ spec:
2828
value: quay.io/redhat-user-workloads/rhtap-shared-team-tenant/tssc-cli:{{revision}}
2929
- name: dockerfile
3030
value: Dockerfile
31+
- name: prefetch-input
32+
value: '{"type": "generic", "path": "."}'
3133
- name: path-context
3234
value: .
3335
pipelineSpec:

Dockerfile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
FROM registry.redhat.io/openshift4/ose-tools-rhel9@sha256:c1baccf320b0acaed693a07fd8df76758db0a38767ace30ccc79aed9ba8c4987 AS ose-tools
66
FROM registry.access.redhat.com/ubi9/go-toolset:9.7-1763038106 AS builder
77

8+
ARG revision
9+
ARG version
10+
811
USER root
912
WORKDIR /workdir/tssc
1013

@@ -17,11 +20,12 @@ COPY test/ ./test/
1720
COPY image/ ./image/
1821
COPY vendor/ ./vendor/
1922

20-
COPY go.mod go.sum Makefile ./
23+
COPY go.mod go.sum Makefile .goreleaser.yaml ./
2124

2225
RUN tar -xvf ./image/gh_2.81.0_linux_amd64.tar.gz -C ./image
26+
2327
RUN make test
24-
RUN make GOFLAGS='-buildvcs=false'
28+
RUN make GOFLAGS='-buildvcs=false' COMMIT=${revision} VERSION=${version}
2529

2630
#
2731
# Run

Makefile

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ VERSION ?= $(shell \
5656
else git describe --tags --always || echo "v0.0.0-SNAPSHOT"; \
5757
fi)
5858

59+
# Commit will be set at build time via git commit hash
60+
COMMIT ?= $(shell git rev-parse --short HEAD)
61+
5962
.EXPORT_ALL_VARIABLES:
6063

6164
.default: build
@@ -67,9 +70,10 @@ VERSION ?= $(shell \
6770
# Builds the application executable with installer resources embedded.
6871
.PHONY: $(BIN)
6972
$(BIN): installer-tarball
73+
$(BIN):
7074
@echo "# Building '$(BIN)'"
7175
@[ -d $(BIN_DIR) ] || mkdir -p $(BIN_DIR)
72-
go build -ldflags "-X github.com/redhat-appstudio/tssc-cli/pkg/constants.Version=$(VERSION)" -o $(BIN) $(CMD)
76+
go build -ldflags "-X github.com/redhat-appstudio/tssc-cli/pkg/constants.Version=$(VERSION) -X github.com/redhat-appstudio/tssc-cli/pkg/constants.Commit=$(COMMIT)" -o $(BIN) $(CMD)
7377

7478
.PHONY: build
7579
build: $(BIN)
@@ -160,8 +164,8 @@ tool-gh:
160164
# Installs GoReleaser.
161165
tool-goreleaser: GOFLAGS =
162166
tool-goreleaser:
163-
@which goreleaser >/dev/null 2>&1 || \
164-
go install github.com/goreleaser/goreleaser@latest >/dev/null 2>&1
167+
@which goreleaser || \
168+
go install github.com/goreleaser/goreleaser@latest
165169

166170
#
167171
# Test and Lint

go.sum

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,4 +587,4 @@ sigs.k8s.io/randfill v1.0.0/go.mod h1:XeLlZ/jmk4i1HRopwe7/aU3H5n1zNUcX6TM94b3QxO
587587
sigs.k8s.io/structured-merge-diff/v6 v6.3.0 h1:jTijUJbW353oVOd9oTlifJqOGEkUw2jB/fXCbTiQEco=
588588
sigs.k8s.io/structured-merge-diff/v6 v6.3.0/go.mod h1:M3W8sfWvn2HhQDIbGWj3S099YozAsymCo/wrT5ohRUE=
589589
sigs.k8s.io/yaml v1.6.0 h1:G8fkbMSAFqgEFgh4b1wmtzDnioxFCUgTZhlbj5P9QYs=
590-
sigs.k8s.io/yaml v1.6.0/go.mod h1:796bPqUfzR/0jLAl6XjHl3Ck7MiyVv8dbTdyT3/pMf4=
590+
sigs.k8s.io/yaml v1.6.0/go.mod h1:796bPqUfzR/0jLAl6XjHl3Ck7MiyVv8dbTdyT3/pMf4=

pkg/constants/constants.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ var (
2020

2121
// Version is the application version, set at build time via ldflags.
2222
var Version = "v0.0.0-SNAPSHOT"
23+
var Commit = "unknown"

pkg/flags/flags.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (f *Flags) LoggerWith(l *slog.Logger) *slog.Logger {
6666

6767
// ShowVersion shows the application version and exits
6868
func (f *Flags) ShowVersion() {
69-
fmt.Printf("%s %s\n", constants.AppName, constants.Version)
69+
fmt.Printf("%s Version: %s\nCommit: %s\n", constants.AppName, constants.Version, constants.Commit)
7070
}
7171

7272
// NewFlags instantiates the global flags with default values.

pkg/subcmd/mcpserver.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package subcmd
22

33
import (
4+
"fmt"
45
"io"
56
"log/slog"
67

78
"github.com/redhat-appstudio/tssc-cli/pkg/chartfs"
89
"github.com/redhat-appstudio/tssc-cli/pkg/config"
10+
"github.com/redhat-appstudio/tssc-cli/pkg/constants"
911
"github.com/redhat-appstudio/tssc-cli/pkg/flags"
1012
"github.com/redhat-appstudio/tssc-cli/pkg/installer"
1113
"github.com/redhat-appstudio/tssc-cli/pkg/integrations"
@@ -37,11 +39,13 @@ Starts the MCP server for the TSSC installer, using STDIO communication.
3739
// PersistentFlags adds flags to the command.
3840
func (m *MCPServer) PersistentFlags(cmd *cobra.Command) {
3941
p := cmd.PersistentFlags()
40-
p.StringVar(&m.image, "image", "", "container image for the installer")
41-
42-
if err := cmd.MarkPersistentFlagRequired("image"); err != nil {
43-
panic(err)
42+
var defaultImage string
43+
if constants.Commit == "unknown" || constants.Commit == "" {
44+
defaultImage = "quay.io/redhat-user-workloads/rhtap-shared-team-tenant/tssc-cli:latest"
45+
} else {
46+
defaultImage = fmt.Sprintf("quay.io/redhat-user-workloads/rhtap-shared-team-tenant/tssc-cli:on-pr-%s", constants.Commit)
4447
}
48+
p.StringVar(&m.image, "image", defaultImage, "container image for the installer\n")
4549
}
4650

4751
// Cmd exposes the cobra instance.

0 commit comments

Comments
 (0)