diff --git a/Dockerfile b/Dockerfile index 3289e5c9..dfee46f3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,6 +11,7 @@ WORKDIR /src COPY go.mod go.sum ./ COPY ./cmd ./cmd COPY ./pkg ./pkg +COPY .git .git RUN --mount=type=cache,id=gomod,target=/go/pkg/mod \ --mount=type=cache,id=gobuild,target=/root/.cache/go-build \ diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index e88f2603..68766f0d 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -26,7 +26,7 @@ ## Bug Fixes * [#405](https://github.com/suse-edge/edge-image-builder/issues/405) - OCI registries are assumed to include the chart name - +* [#457](https://github.com/suse-edge/edge-image-builder/issues/457) - Added version command and version marker on CRB images --- # v1.0.0 diff --git a/cmd/eib/main.go b/cmd/eib/main.go index 1e264bc1..f3e625df 100644 --- a/cmd/eib/main.go +++ b/cmd/eib/main.go @@ -14,6 +14,7 @@ func main() { app.Commands = []*cli.Command{ cmd.NewBuildCommand(build.Run), cmd.NewValidateCommand(build.Validate), + cmd.NewVersionCommand(build.Version), } if err := app.Run(os.Args); err != nil { diff --git a/pkg/cli/build/version.go b/pkg/cli/build/version.go new file mode 100644 index 00000000..bd208ecb --- /dev/null +++ b/pkg/cli/build/version.go @@ -0,0 +1,12 @@ +package build + +import ( + "github.com/suse-edge/edge-image-builder/pkg/log" + "github.com/suse-edge/edge-image-builder/pkg/template" + "github.com/urfave/cli/v2" +) + +func Version(_ *cli.Context) error { + log.AuditInfof("Edge Image Builder Version: %s", template.GetVersion()) + return nil +} diff --git a/pkg/cli/cmd/version.go b/pkg/cli/cmd/version.go new file mode 100644 index 00000000..f47995b2 --- /dev/null +++ b/pkg/cli/cmd/version.go @@ -0,0 +1,16 @@ +package cmd + +import ( + "fmt" + + "github.com/urfave/cli/v2" +) + +func NewVersionCommand(action func(*cli.Context) error) *cli.Command { + return &cli.Command{ + Name: "version", + Usage: "Inspect program version", + UsageText: fmt.Sprintf("%s version", appName), + Action: action, + } +} diff --git a/pkg/combustion/message.go b/pkg/combustion/message.go index 9ad3bb19..359ff3e3 100644 --- a/pkg/combustion/message.go +++ b/pkg/combustion/message.go @@ -6,6 +6,8 @@ import ( "os" "path/filepath" + "github.com/suse-edge/edge-image-builder/pkg/template" + "github.com/suse-edge/edge-image-builder/pkg/fileio" "github.com/suse-edge/edge-image-builder/pkg/image" "github.com/suse-edge/edge-image-builder/pkg/log" @@ -20,11 +22,22 @@ const ( var messageScript string func configureMessage(ctx *image.Context) ([]string, error) { - filename := filepath.Join(ctx.CombustionDir, messageScriptName) + values := struct { + Version string + }{ + Version: template.GetVersion(), + } - if err := os.WriteFile(filename, []byte(messageScript), fileio.ExecutablePerms); err != nil { + data, err := template.Parse(messageScriptName, messageScript, &values) + if err != nil { + return nil, fmt.Errorf("parsing message script template: %w", err) + } + + filename := filepath.Join(ctx.CombustionDir, messageScriptName) + err = os.WriteFile(filename, []byte(data), fileio.ExecutablePerms) + if err != nil { log.AuditComponentFailed(messageComponentName) - return nil, fmt.Errorf("copying script %s: %w", messageScriptName, err) + return nil, fmt.Errorf("writing message script: %w", err) } log.AuditComponentSuccessful(messageComponentName) diff --git a/pkg/combustion/templates/48-message.sh b/pkg/combustion/templates/48-message.sh index a122cc17..4f4b53a8 100644 --- a/pkg/combustion/templates/48-message.sh +++ b/pkg/combustion/templates/48-message.sh @@ -1,3 +1,3 @@ #!/bin/bash set -euo pipefail -echo "Configured with the Edge Image Builder" >> /etc/issue.d/eib +echo "Configured with the Edge Image Builder {{ .Version }}" >> /etc/issue.d/eib diff --git a/pkg/template/template.go b/pkg/template/template.go index fdf4267b..7857e89a 100644 --- a/pkg/template/template.go +++ b/pkg/template/template.go @@ -3,10 +3,13 @@ package template import ( "bytes" "fmt" + "runtime/debug" "strings" "text/template" ) +var version string + func Parse(name string, contents string, templateData any) (string, error) { if templateData == nil { return "", fmt.Errorf("template data not provided") @@ -26,3 +29,20 @@ func Parse(name string, contents string, templateData any) (string, error) { return buff.String(), nil } + +func GetVersion() string { + if version != "" { + return version + } + + if info, ok := debug.ReadBuildInfo(); ok { + fmt.Println(info) + for _, setting := range info.Settings { + if setting.Key == "vcs.revision" { + return fmt.Sprintf("git-%s", setting.Value) + } + } + } + + return "" +}