From 7535a55e2176031ed4d788bd62f5977db4142795 Mon Sep 17 00:00:00 2001 From: Andrej Petras Date: Fri, 16 Feb 2024 16:08:35 +0100 Subject: [PATCH] feat: add docker annotations --- cmd/docker.go | 5 +-- cmd/docker_build.go | 33 +++++++++++++------- cmd/project.go | 75 +++++++++++++++++++++++++++++++++------------ 3 files changed, 80 insertions(+), 33 deletions(-) diff --git a/cmd/docker.go b/cmd/docker.go index f42dfbc..8eb4904 100644 --- a/cmd/docker.go +++ b/cmd/docker.go @@ -27,7 +27,7 @@ func createDockerCmd() *cobra.Command { TraverseChildren: true, } - addBoolFlag(cmd, "docker-skip-open-containers-labels", "", false, "skip open containers labels ") + addBoolFlag(cmd, "docker-skip-opencontainers-labels", "", false, "skip open containers labels ") addStringFlag(cmd, "docker-registry", "", "", "the docker registry") addStringFlag(cmd, "docker-group", "", "", "the docker repository group") addStringFlag(cmd, "docker-repository", "", "", "the docker repository. Default value is the project name.") @@ -126,9 +126,10 @@ func dockerLabels(project *Project, skipLabels bool, skipOpenContainersLabels bo if !skipOpenContainersLabels { result["org.opencontainers.image.created"] = created result["org.opencontainers.image.title"] = project.Name() + result["org.opencontainers.image.description"] = project.Description() result["org.opencontainers.image.revision"] = project.Hash() result["org.opencontainers.image.version"] = project.Version() - result["org.opencontainers.image.source"] = project.Source() + result["org.opencontainers.image.source"] = project.Url() } // add custom labels diff --git a/cmd/docker_build.go b/cmd/docker_build.go index cd8340c..310ee81 100644 --- a/cmd/docker_build.go +++ b/cmd/docker_build.go @@ -7,17 +7,19 @@ import ( ) type dockerBuildFlags struct { - Docker dockerFlags `mapstructure:",squash"` - File string `mapstructure:"docker-file"` - Profile string `mapstructure:"docker-profile"` - Context string `mapstructure:"docker-context"` - Platform string `mapstructure:"docker-platform"` - Provenance string `mapstructure:"docker-provenance"` - BuildX bool `mapstructure:"docker-buildx"` - SkipDevBuild bool `mapstructure:"docker-skip-dev"` - SkipPull bool `mapstructure:"docker-skip-pull"` - BuildPush bool `mapstructure:"docker-build-push"` - SkipRemoveBuild bool `mapstructure:"docker-remove-build-skip"` + Docker dockerFlags `mapstructure:",squash"` + File string `mapstructure:"docker-file"` + Profile string `mapstructure:"docker-profile"` + Context string `mapstructure:"docker-context"` + Platform string `mapstructure:"docker-platform"` + Provenance string `mapstructure:"docker-provenance"` + BuildX bool `mapstructure:"docker-buildx"` + SkipDevBuild bool `mapstructure:"docker-skip-dev"` + SkipPull bool `mapstructure:"docker-skip-pull"` + BuildPush bool `mapstructure:"docker-build-push"` + SkipRemoveBuild bool `mapstructure:"docker-remove-build-skip"` + AddLabelsAnnotation bool `mapstructure:"docker-add-labels-annotations"` + PrefixLabelsAnnotation string `mapstructure:"docker-prefix-labels-annotations"` } func createDockerBuildCmd() *cobra.Command { @@ -45,6 +47,8 @@ func createDockerBuildCmd() *cobra.Command { addBoolFlag(cmd, "docker-buildx", "", false, "extended build capabilities with BuildKit") addBoolFlag(cmd, "docker-skip-dev", "", false, "skip build image {{ .Name }}:latest") addBoolFlag(cmd, "docker-remove-intermediate-img-skip", "", false, "skip remove build intermediate containers") + addBoolFlag(cmd, "docker-add-labels-annotations", "", true, "add all labels as container annotations") + addStringFlag(cmd, "docker-prefix-labels-annotations", "", "index:", "prefix for all labels as container annotations") return cmd } @@ -98,6 +102,13 @@ func dockerBuild(project *Project, flags dockerBuildFlags) { command = append(command, "--label", key+"="+value) } + // create annotations + if flags.BuildX && flags.AddLabelsAnnotation { + for key, value := range labels { + command = append(command, "--annotation", flags.PrefixLabelsAnnotation+key+"="+value) + } + } + // add tags for _, tag := range tags { command = append(command, "-t", tag) diff --git a/cmd/project.go b/cmd/project.go index fdda823..b5a0f19 100644 --- a/cmd/project.go +++ b/cmd/project.go @@ -23,6 +23,8 @@ type projectFlags struct { BranchTemplate string `mapstructure:"branch-template"` SkipLabels bool `mapstructure:"skip-samo-labels"` LabelTemplate string `mapstructure:"labels-template-list"` + Description string `mapstructure:"description"` + Url string `mapstructure:"url"` } var sourceLinkRegex = `\/\/.*@` @@ -38,6 +40,8 @@ func createProjectCmd() *cobra.Command { TraverseChildren: true, } + addStringFlag(cmd, "description", "", "", "project description") + addStringFlag(cmd, "url", "", "", "project url") addStringFlag(cmd, "first-version", "", "0.0.0", "the first version of the project") addBoolFlag(cmd, "release-major", "", false, "create a major release") addBoolFlag(cmd, "release-patch", "", false, "create a patch release") @@ -66,16 +70,18 @@ func createProjectCmd() *cobra.Command { // Project common project interface type Project struct { - name string - describe tools.GitDescribe - rc tools.GitDescribe - branch string - source string - patchBuild bool - version *semver.Version - rcVersion *semver.Version - release *semver.Version - rcRelease *semver.Version + name string + describe tools.GitDescribe + rc tools.GitDescribe + branch string + source string + url string + description string + patchBuild bool + version *semver.Version + rcVersion *semver.Version + release *semver.Version + rcRelease *semver.Version } // Name project name @@ -107,6 +113,14 @@ func (g Project) Version() string { return g.version.String() } +func (g Project) Description() string { + return g.description +} + +func (g Project) Url() string { + return g.url +} + func (g Project) Release() string { return g.release.String() } @@ -206,17 +220,38 @@ func loadProject(flags projectFlags) *Project { } } + var url = flags.Url + if len(url) == 0 { + // remove .git suffix + url = strings.TrimSuffix(source, ".git") + + // replace git@server:path -> https://server/path + if strings.HasPrefix(url, "git@") { + url = strings.TrimPrefix(url, "git@") + url = strings.Replace(url, ":", "/", 1) + url = "https://" + url + } + + } + + var description = flags.Description + if len(description) == 0 { + description = describe.Hash + } + p := &Project{ - name: name, - describe: describe, - branch: branch, - source: source, - patchBuild: patchBuild, - rc: rc, - rcVersion: createVersion(lastRC, branch, flags.VersionTemplate, rc), - rcRelease: tools.CreateSemVer(lastRC), - version: createVersion(version, branch, flags.VersionTemplate, describe), - release: tools.CreateSemVer(version), + name: name, + describe: describe, + branch: branch, + source: source, + description: description, + patchBuild: patchBuild, + url: url, + rc: rc, + rcVersion: createVersion(lastRC, branch, flags.VersionTemplate, rc), + rcRelease: tools.CreateSemVer(lastRC), + version: createVersion(version, branch, flags.VersionTemplate, describe), + release: tools.CreateSemVer(version), } log.Debug("Versions", log.Fields{"version": p.Version(), "release": p.Release(), "rcVersion": p.rcVersion.String(), "rcRelease": p.rcRelease.String()}) return p