Implement option to add image labels and generate automatic labels (#19)
Fixes #16 Results into labels:  Co-authored-by: Lauris BH <lauris@nix.lv> Reviewed-on: https://codeberg.org/woodpecker-plugins/plugin-docker-buildx/pulls/19 Reviewed-by: 6543 <6543@obermui.de> Co-authored-by: Lauris BH <lafriks@noreply.codeberg.org> Co-committed-by: Lauris BH <lafriks@noreply.codeberg.org>
This commit is contained in:
@@ -126,6 +126,10 @@ func commandBuild(build Build, dryrun bool) *exec.Cmd {
|
||||
args = append(args, "-t", fmt.Sprintf("%s:%s", build.Repo, arg))
|
||||
}
|
||||
|
||||
for _, l := range build.Labels.Value() {
|
||||
args = append(args, "--label", l)
|
||||
}
|
||||
|
||||
return exec.Command(dockerExe, args...)
|
||||
}
|
||||
|
||||
|
||||
@@ -49,6 +49,8 @@ type Build struct {
|
||||
TagsAuto bool // Docker build auto tag
|
||||
TagsSuffix string // Docker build tags with suffix
|
||||
Tags cli.StringSlice // Docker build tags
|
||||
LabelsAuto bool // Docker build auto labels
|
||||
Labels cli.StringSlice // Docker build labels
|
||||
Platforms cli.StringSlice // Docker build target platforms
|
||||
Args cli.StringSlice // Docker build args
|
||||
ArgsEnv cli.StringSlice // Docker build args from env
|
||||
@@ -98,6 +100,9 @@ func (p *Plugin) Validate() error {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
if p.settings.Build.LabelsAuto {
|
||||
p.settings.Build.Labels = *cli.NewStringSlice(p.Labels()...)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
32
plugin/labels.go
Normal file
32
plugin/labels.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package plugin
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/coreos/go-semver/semver"
|
||||
)
|
||||
|
||||
// Labels returns list of labels to use for image
|
||||
func (p *Plugin) Labels() []string {
|
||||
l := p.settings.Build.Labels.Value()
|
||||
// As described in https://github.com/opencontainers/image-spec/blob/main/annotations.md
|
||||
l = append(l, fmt.Sprintf("org.opencontainers.image.created=%s", time.Now().UTC().Format(time.RFC3339)))
|
||||
if p.settings.Build.Remote != "" {
|
||||
l = append(l, fmt.Sprintf("org.opencontainers.image.source=%s", p.settings.Build.Remote))
|
||||
}
|
||||
if p.pipeline.Repo.Link != "" {
|
||||
l = append(l, fmt.Sprintf("org.opencontainers.image.url=%s", p.pipeline.Repo.Link))
|
||||
}
|
||||
if p.pipeline.Commit.SHA != "" {
|
||||
l = append(l, fmt.Sprintf("org.opencontainers.image.revision=%s", p.pipeline.Commit.SHA))
|
||||
}
|
||||
if p.settings.Build.Ref != "" && strings.HasPrefix(p.settings.Build.Ref, "refs/tags/") {
|
||||
v, err := semver.NewVersion(strings.TrimPrefix(p.settings.Build.Ref[10:], "v"))
|
||||
if err == nil && v != nil {
|
||||
l = append(l, fmt.Sprintf("org.opencontainers.image.version=%s", v.String()))
|
||||
}
|
||||
}
|
||||
return l
|
||||
}
|
||||
Reference in New Issue
Block a user