From 52ab290c52f8a3b6857e553e13a7c0a49c90180a Mon Sep 17 00:00:00 2001 From: Luc DUZAN Date: Wed, 14 Feb 2024 19:03:09 +0100 Subject: [PATCH] add version in cli --- .github/workflows/build-docker.yml | 7 ++++++- .github/workflows/release.yml | 1 + cmd/version.go | 23 +++++++++++++++++++++++ docker/Dockerfile | 4 +++- 4 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 cmd/version.go diff --git a/.github/workflows/build-docker.yml b/.github/workflows/build-docker.yml index e70a8d4..e7ee389 100644 --- a/.github/workflows/build-docker.yml +++ b/.github/workflows/build-docker.yml @@ -65,6 +65,8 @@ jobs: org.opencontainers.image.documentation=${{ env.LABEL_IMAGE_DOCUMENTATION }} org.opencontainers.image.vendor=${{ env.LABEL_IMAGE_VENDOR }} org.opencontainers.image.url=${{ env.LABEL_IMAGE_URL }} + org.opencontainers.image.revision=${{ github.sha }} + org.opencontainers.image.version=${{ github.event.release.tag_name || github.ref_name }} - uses: actions/checkout@v3 - name: Build ${{ env.IMAGE_NAME }} for ${{ steps.buildx.outputs.platforms }} id: build @@ -78,4 +80,7 @@ jobs: labels: ${{ steps.docker_meta.outputs.labels }} cache-from: type=registry,ref=${{env.HARBOR_IMAGE}}:main cache-to: type=inline,mode=min - github-token: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file + github-token: ${{ secrets.GITHUB_TOKEN }} + build-args: | + hash=${{ github.sha }} + version=${{ github.event.release.tag_name || github.ref_name }} \ No newline at end of file diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 52662f1..b5f7e93 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -96,6 +96,7 @@ jobs: goos: ${{ matrix.goos }} goarch: ${{ matrix.goarch }} goversion: "1.22.0" + build_flags: -X 'github.com/conduktor/ctl/cmd.version=${{ github.event.release.tag_name }}' -X 'github.com/conduktor/ctl/cmd.hash=${{ github.sha }}' project_path: "./" binary_name: "conduktor" build-docker: diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 0000000..6bfb98d --- /dev/null +++ b/cmd/version.go @@ -0,0 +1,23 @@ +package cmd + +import ( + "fmt" + "github.com/spf13/cobra" +) + +var version = "unknown" +var hash = "unknown" + +// versionCmd represents the apply command +var versionCmd = &cobra.Command{ + Use: "version", + Short: "display the version of conduktor", + Long: ``, + Run: func(cmd *cobra.Command, args []string) { + fmt.Printf("Version: %s\nHash: %s\n", version, hash) + }, +} + +func init() { + rootCmd.AddCommand(versionCmd) +} diff --git a/docker/Dockerfile b/docker/Dockerfile index f02ae29..d4b1899 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,9 +1,11 @@ FROM golang:1.22 +ARG version=unknown +ARG hash=unknown WORKDIR /app COPY go.mod go.sum ./ RUN go mod download COPY . ./ -RUN CGO_ENABLED=0 GOOS=linux go build -o /conduktor . && rm -rf /app +RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-X 'github.com/conduktor/ctl/cmd.version=$version' -X 'github.com/conduktor/ctl/cmd.hash=$hash'" -o /conduktor . && rm -rf /app CMD ["/bin/conduktor"] FROM scratch