Allow using auto_tag while supplying additional tags via tags (#35)

This implements #32, allowing to append tags supplied via `tags` along side auto generated tags.

Tags supplied via `tags` are *not* suffixed by the `auto_tag_suffix`

Co-authored-by: Gapodo <gapodo@datenclown.at>
Reviewed-on: https://codeberg.org/woodpecker-plugins/plugin-docker-buildx/pulls/35
Reviewed-by: 6543 <6543@obermui.de>
Co-authored-by: gapodo <gapodo@geekvoid.net>
Co-committed-by: gapodo <gapodo@geekvoid.net>
This commit is contained in:
gapodo 2022-11-04 23:47:43 +01:00 committed by 6543
parent 466dfce406
commit f10f7b4c7f
2 changed files with 19 additions and 11 deletions

View File

@ -97,7 +97,7 @@ It will automatically generate buildkit configuration to use custom CA certifica
| `daemon_off` | `false` | disables the startup of the docker daemon | `daemon_off` | `false` | disables the startup of the docker daemon
| `buildkit_config` | *none* | sets content of the docker [buildkit TOML config](https://github.com/moby/buildkit/blob/master/docs/buildkitd.toml.md) | `buildkit_config` | *none* | sets content of the docker [buildkit TOML config](https://github.com/moby/buildkit/blob/master/docs/buildkitd.toml.md)
| `context` | `.` | sets the path of the build context to use | `context` | `.` | sets the path of the build context to use
| `default_tags`/`auto_tag` | `false` | generates tag names automatically based on git branch and git tag | `default_tags`/`auto_tag` | `false` | generates tag names automatically based on git branch and git tag, tags supplied via `tags` are additionally added to the auto_tags without suffix
| `default_suffix"`/`auto_tag_suffix`| *none* | generates tag names with the given suffix | `default_suffix"`/`auto_tag_suffix`| *none* | generates tag names with the given suffix
| `label`/`labels` | *none* | sets labels to use for the image in format `<name>=<value>` | `label`/`labels` | *none* | sets labels to use for the image in format `<name>=<value>`
| `default_labels`/`auto_labels` | `true` | sets docker image labels based on git information | `default_labels`/`auto_labels` | `true` | sets docker image labels based on git information

View File

@ -86,7 +86,7 @@ func (l Login) anonymous() bool {
func (p *Plugin) InitSettings() error { func (p *Plugin) InitSettings() error {
if p.settings.LoginsRaw != "" { if p.settings.LoginsRaw != "" {
if err := json.Unmarshal([]byte(p.settings.LoginsRaw), &p.settings.Logins); err != nil { if err := json.Unmarshal([]byte(p.settings.LoginsRaw), &p.settings.Logins); err != nil {
return fmt.Errorf("Could not unmarshal logins: %v", err) return fmt.Errorf("could not unmarshal logins: %v", err)
} }
} }
@ -131,21 +131,17 @@ func (p *Plugin) Validate() error {
logrus.Printf("cannot build docker image for %s, invalid semantic version", p.settings.Build.Ref) logrus.Printf("cannot build docker image for %s, invalid semantic version", p.settings.Build.Ref)
return err return err
} }
// include user supplied tags
tag = append(tag, p.sanitizedUserTags()...)
p.settings.Build.Tags = *cli.NewStringSlice(tag...) p.settings.Build.Tags = *cli.NewStringSlice(tag...)
} else { } else {
logrus.Printf("skipping automated docker build for %s", p.settings.Build.Ref) logrus.Printf("skipping automated docker build for %s", p.settings.Build.Ref)
return nil return nil
} }
} else { } else {
// ignore empty tags p.settings.Build.Tags = *cli.NewStringSlice(p.sanitizedUserTags()...)
var tags []string
for _, t := range p.settings.Build.Tags.Value() {
t = strings.TrimSpace(t)
if t != "" {
tags = append(tags, t)
}
}
p.settings.Build.Tags = *cli.NewStringSlice(tags...)
} }
if p.settings.Build.LabelsAuto { if p.settings.Build.LabelsAuto {
@ -155,6 +151,18 @@ func (p *Plugin) Validate() error {
return nil return nil
} }
func (p *Plugin) sanitizedUserTags() []string {
// ignore empty tags
var tags []string
for _, t := range p.settings.Build.Tags.Value() {
t = strings.TrimSpace(t)
if t != "" {
tags = append(tags, t)
}
}
return tags
}
func (p *Plugin) writeBuildkitConfig() error { func (p *Plugin) writeBuildkitConfig() error {
// no buildkit config, automatically generate buildkit configuration to use a custom CA certificate for each registry // no buildkit config, automatically generate buildkit configuration to use a custom CA certificate for each registry
if p.settings.Daemon.BuildkitConfig == "" && p.settings.Daemon.Registry != "" { if p.settings.Daemon.BuildkitConfig == "" && p.settings.Daemon.Registry != "" {