Add version command and fix commit version

Signed-off-by: Itxaka <itxaka.garcia@spectrocloud.com>
This commit is contained in:
Itxaka
2023-02-16 15:43:33 +01:00
parent bc53b0de3a
commit 91d6fa6945
4 changed files with 23 additions and 7 deletions

View File

@@ -14,8 +14,11 @@ version:
FROM alpine FROM alpine
RUN apk add git RUN apk add git
COPY . ./ COPY . ./
RUN --no-cache echo $(git describe --always --tags --dirty) > VERSION RUN --no-cache echo $(git describe --tags | sed 's/\(.*\)-.*/\1/') > VERSION
RUN --no-cache echo $(git describe --always --dirty) > COMMIT
ARG VERSION=$(cat VERSION) ARG VERSION=$(cat VERSION)
ARG COMMIT=$(cat COMMMIT)
SAVE ARTIFACT COMMIT COMMIT
SAVE ARTIFACT VERSION VERSION SAVE ARTIFACT VERSION VERSION
golang-image: golang-image:
@@ -58,8 +61,10 @@ build-immucore:
COPY --dir internal /work COPY --dir internal /work
COPY --dir pkg /work COPY --dir pkg /work
COPY +version/VERSION ./ COPY +version/VERSION ./
COPY +version/COMMIT ./
ARG VERSION=$(cat VERSION) ARG VERSION=$(cat VERSION)
ARG LDFLAGS="-s -w -X github.com/kairos-io/immucore/internal/version.version=$VERSION" ARG COMMIT=$(cat COMMIT)
ARG LDFLAGS="-s -w -X github.com/kairos-io/immucore/internal/version.version=$VERSION -X github.com/kairos-io/immucore/internal/version.gitCommit=$COMMIT"
RUN echo ${LDFLAGS} RUN echo ${LDFLAGS}
RUN CGO_ENABLED=0 go build -o immucore -ldflags "${LDFLAGS}" RUN CGO_ENABLED=0 go build -o immucore -ldflags "${LDFLAGS}"
SAVE ARTIFACT /work/immucore immucore AS LOCAL build/immucore-$VERSION SAVE ARTIFACT /work/immucore immucore AS LOCAL build/immucore-$VERSION

View File

@@ -5,6 +5,7 @@ import (
"os" "os"
"github.com/kairos-io/immucore/internal/utils" "github.com/kairos-io/immucore/internal/utils"
"github.com/kairos-io/immucore/internal/version"
"github.com/kairos-io/immucore/pkg/mount" "github.com/kairos-io/immucore/pkg/mount"
"github.com/rs/zerolog" "github.com/rs/zerolog"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
@@ -39,6 +40,9 @@ Sends a generic event payload with the configuration found in the scanned direct
zerolog.SetGlobalLevel(zerolog.DebugLevel) zerolog.SetGlobalLevel(zerolog.DebugLevel)
} }
v := version.Get()
log.Logger.Info().Str("commit", v.GitCommit).Str("compiled with", v.GoVersion).Str("version", v.Version).Msg("Immucore")
g := herd.DAG(herd.EnableInit) g := herd.DAG(herd.EnableInit)
// You can pass rd.cos.disable in the cmdline to disable the whole immutable stuff // You can pass rd.cos.disable in the cmdline to disable the whole immutable stuff
@@ -86,4 +90,15 @@ Sends a generic event payload with the configuration found in the scanned direct
return err return err
}, },
}, },
{
Name: "version",
Usage: "version",
Action: func(c *cli.Context) error {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).With().Logger()
zerolog.SetGlobalLevel(zerolog.InfoLevel)
v := version.Get()
log.Logger.Info().Str("commit", v.GitCommit).Str("compiled with", v.GoVersion).Str("version", v.Version).Msg("Immucore")
return nil
},
},
} }

View File

@@ -4,7 +4,7 @@ import "runtime"
var ( var (
version = "v0.0.1" version = "v0.0.1"
// gitCommit is the git sha1 // gitCommit is the git sha1 + dirty if build from a dirty git
gitCommit = "" gitCommit = ""
) )

View File

@@ -2,11 +2,9 @@ package main
import ( import (
"fmt" "fmt"
"github.com/kairos-io/immucore/internal/version"
"os" "os"
"github.com/kairos-io/immucore/internal/cmd" "github.com/kairos-io/immucore/internal/cmd"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
) )
@@ -25,8 +23,6 @@ func main() {
Commands: cmd.Commands, Commands: cmd.Commands,
} }
fmt.Println(version.Get())
err := app.Run(os.Args) err := app.Run(os.Args)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)