Skip to content

Commit 5ac7399

Browse files
Merge pull request #1387 from raisavarghese/RHTAP-5734
fix(RHTAP-5734): Display the commit id in the application version
2 parents 859e82c + 87f0344 commit 5ac7399

File tree

8 files changed

+156
-13
lines changed

8 files changed

+156
-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.CommitID={{.Commit}}

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

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ spec:
3131
value: 5d
3232
- name: dockerfile
3333
value: Dockerfile
34+
- name: prefetch-input
35+
value: ""
3436
- name: path-context
3537
value: .
3638
pipelineSpec:
@@ -84,10 +86,10 @@ spec:
8486
description: Execute the build with network isolation
8587
name: hermetic
8688
type: string
87-
- default: ""
88-
description: Build dependencies to be prefetched by Cachi2
89+
- description: Build dependencies to be prefetched by Cachi2
8990
name: prefetch-input
9091
type: string
92+
default: ""
9193
- default: ""
9294
description: Image tag expiration time, time values could be something like 1h, 2d, 3w for hours, days, and weeks, respectively.
9395
name: image-expires-after
@@ -176,6 +178,65 @@ spec:
176178
workspaces:
177179
- name: basic-auth
178180
workspace: git-auth
181+
- name: get-version
182+
params:
183+
- name: url
184+
value: $(params.git-url)
185+
- name: revision
186+
value: $(params.revision)
187+
runAfter:
188+
- clone-repository
189+
taskSpec:
190+
description: Extract version from git repository by cloning it
191+
params:
192+
- name: url
193+
description: Git repository URL
194+
type: string
195+
- name: revision
196+
description: Git revision to checkout
197+
type: string
198+
results:
199+
- name: version
200+
description: Git version/tag
201+
steps:
202+
- name: clone-and-get-version
203+
image: quay.io/konflux-ci/appstudio-utils:ab6b0b8e40e440158e7288c73aff1cf83a2cc8a9@sha256:24179f0efd06c65d16868c2d7eb82573cce8e43533de6cea14fec3b7446e0b14
204+
env:
205+
- name: GIT_URL
206+
value: $(params.url)
207+
- name: GIT_REVISION
208+
value: $(params.revision)
209+
script: |
210+
#!/usr/bin/env bash
211+
set -eo pipefail
212+
213+
# Create temporary directory
214+
WORKDIR=$(mktemp -d)
215+
cd "$WORKDIR"
216+
217+
# Clone repository with tags
218+
echo "Cloning repository..."
219+
# Use unshallow clone to get full history and tags for git describe
220+
git clone --no-single-branch "$GIT_URL" .
221+
222+
# Checkout the specific revision
223+
git checkout "$GIT_REVISION"
224+
225+
# Fetch all tags explicitly
226+
git fetch origin --tags
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+
printf "%s" "$VERSION" > $(results.version.path)
233+
echo "Version: $VERSION"
234+
echo "Commit ID: $GIT_REVISION"
235+
when:
236+
- input: $(tasks.init.results.build)
237+
operator: in
238+
values:
239+
- "true"
179240
- name: prefetch-dependencies
180241
params:
181242
- name: input
@@ -220,6 +281,8 @@ spec:
220281
value: $(tasks.clone-repository.results.commit)
221282
- name: BUILD_ARGS
222283
value:
284+
- COMMIT_ID=$(params.revision)
285+
- VERSION_ID=$(tasks.get-version.results.version)
223286
- $(params.build-args[*])
224287
- name: BUILD_ARGS_FILE
225288
value: $(params.build-args-file)
@@ -233,6 +296,7 @@ spec:
233296
value: $(params.buildah-format)
234297
runAfter:
235298
- prefetch-dependencies
299+
- get-version
236300
taskRef:
237301
params:
238302
- name: name

.tekton/tssc-cli-push.yaml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,65 @@ spec:
173173
workspaces:
174174
- name: basic-auth
175175
workspace: git-auth
176+
- name: get-version
177+
params:
178+
- name: url
179+
value: $(params.git-url)
180+
- name: revision
181+
value: $(params.revision)
182+
runAfter:
183+
- clone-repository
184+
taskSpec:
185+
description: Extract version from git repository by cloning it
186+
params:
187+
- name: url
188+
description: Git repository URL
189+
type: string
190+
- name: revision
191+
description: Git revision to checkout
192+
type: string
193+
results:
194+
- name: version
195+
description: Git version/tag
196+
steps:
197+
- name: clone-and-get-version
198+
image: quay.io/konflux-ci/appstudio-utils:ab6b0b8e40e440158e7288c73aff1cf83a2cc8a9@sha256:24179f0efd06c65d16868c2d7eb82573cce8e43533de6cea14fec3b7446e0b14
199+
env:
200+
- name: GIT_URL
201+
value: $(params.url)
202+
- name: GIT_REVISION
203+
value: $(params.revision)
204+
script: |
205+
#!/usr/bin/env bash
206+
set -eo pipefail
207+
208+
# Create temporary directory
209+
WORKDIR=$(mktemp -d)
210+
cd "$WORKDIR"
211+
212+
# Clone repository with tags
213+
echo "Cloning repository..."
214+
# Use unshallow clone to get full history and tags for git describe
215+
git clone --no-single-branch "$GIT_URL" .
216+
217+
# Checkout the specific revision
218+
git checkout "$GIT_REVISION"
219+
220+
# Fetch all tags explicitly
221+
git fetch origin --tags
222+
223+
# Get version using git describe (same logic as Makefile)
224+
# Try to get the closest tag, or use commit SHA as fallback
225+
VERSION=$(git describe --tags --always 2>/dev/null || echo "v0.0.0-SNAPSHOT")
226+
227+
printf "%s" "$VERSION" > $(results.version.path)
228+
echo "Version: $VERSION"
229+
echo "Commit ID: $GIT_REVISION"
230+
when:
231+
- input: $(tasks.init.results.build)
232+
operator: in
233+
values:
234+
- "true"
176235
- name: prefetch-dependencies
177236
params:
178237
- name: input
@@ -217,6 +276,8 @@ spec:
217276
value: $(tasks.clone-repository.results.commit)
218277
- name: BUILD_ARGS
219278
value:
279+
- COMMIT_ID=$(params.revision)
280+
- VERSION_ID=$(tasks.get-version.results.version)
220281
- $(params.build-args[*])
221282
- name: BUILD_ARGS_FILE
222283
value: $(params.build-args-file)

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 COMMIT_ID
9+
ARG VERSION_ID
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_ID=${COMMIT_ID} VERSION=${VERSION_ID}
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_ID ?= $(shell git rev-parse 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.CommitID=$(COMMIT_ID)" -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

pkg/constants/constants.go

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

2121
// Version is the application version, set at build time via ldflags.
2222
var Version = "v0.0.0-SNAPSHOT"
23+
24+
// CommitID is the commit ID of the application, set at build time via git commit hash.
25+
var CommitID = ""

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.CommitID)
7070
}
7171

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

pkg/subcmd/mcpserver.go

Lines changed: 11 additions & 5 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,7 @@ 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)
44-
}
42+
p.StringVar(&m.image, "image", m.image, "container image for the installer\n")
4543
}
4644

4745
// Cmd exposes the cobra instance.
@@ -107,6 +105,14 @@ func NewMCPServer(
107105
cfs: cfs,
108106
kube: kube,
109107
}
108+
109+
m.image = "quay.io/redhat-user-workloads/rhtap-shared-team-tenant/tssc-cli"
110+
// Set default image based on CommitID
111+
if constants.CommitID == "" {
112+
m.image = fmt.Sprintf("%s:latest", m.image)
113+
} else {
114+
m.image = fmt.Sprintf("%s:%s", m.image, constants.CommitID)
115+
}
110116
m.PersistentFlags(m.cmd)
111117
return m
112118
}

0 commit comments

Comments
 (0)