Skip to content

Commit

Permalink
feat: add docker annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
andrejpetras committed Feb 16, 2024
1 parent ad3be37 commit 7535a55
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 33 deletions.
5 changes: 3 additions & 2 deletions cmd/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down Expand Up @@ -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
Expand Down
33 changes: 22 additions & 11 deletions cmd/docker_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down
75 changes: 55 additions & 20 deletions cmd/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `\/\/.*@`
Expand All @@ -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")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7535a55

Please sign in to comment.