From 37718ded77131706c6c566b9c3817d5d9e35b7d5 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Tue, 2 May 2023 19:25:49 +0000 Subject: [PATCH] Add option to overwrites `tags` option with values specified in an file (#62) Reviewed-on: https://codeberg.org/woodpecker-plugins/docker-buildx/pulls/62 Reviewed-by: qwerty287 --- README.md | 2 +- cmd/docker-buildx/config.go | 6 ++++++ docs.md | 13 +++++++------ plugin/impl.go | 19 +++++++++++++++++++ utils/lamda.go | 9 +++++++++ utils/lamda_test.go | 18 ++++++++++++++++++ 6 files changed, 60 insertions(+), 7 deletions(-) create mode 100644 utils/lamda.go create mode 100644 utils/lamda_test.go diff --git a/README.md b/README.md index 0613462..8e199e4 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Get it on Codeberg -Woodpecker CI plugin to build multiarch Docker images with buildx. This plugin is a fork of [thegeeklab/drone-docker-buildx](https://github.com/thegeeklab/drone-docker-buildx/) which itself is a fork of [drone-plugins/drone-docker](https://github.com/drone-plugins/drone-docker). You can find the full documentation at [woodpecker-ci.org](https://woodpecker-ci.org/plugins/Docker%20Buildx). +Woodpecker CI plugin to build multiarch Docker images with buildx. This plugin is a fork of [thegeeklab/drone-docker-buildx](https://github.com/thegeeklab/drone-docker-buildx/) which itself is a fork of [drone-plugins/drone-docker](https://github.com/drone-plugins/drone-docker). You can find the full documentation at [woodpecker-ci.org](https://woodpecker-ci.org/plugins/Docker%20Buildx) ([docs.md](./docs.md)). ## Contributors diff --git a/cmd/docker-buildx/config.go b/cmd/docker-buildx/config.go index 980a036..3b42772 100644 --- a/cmd/docker-buildx/config.go +++ b/cmd/docker-buildx/config.go @@ -121,6 +121,12 @@ func settingsFlags(settings *plugin.Settings) []cli.Flag { FilePath: ".tags", Destination: &settings.Build.Tags, }, + &cli.StringFlag{ + Name: "tags.file", + EnvVars: []string{"PLUGIN_TAGS_FILE", "PLUGIN_TAG_FILE"}, + Usage: "overwrites tags flag with values find in set file", + Destination: &settings.Build.TagsFile, + }, &cli.BoolFlag{ Name: "tags.auto", EnvVars: []string{"PLUGIN_AUTO_TAG"}, diff --git a/docs.md b/docs.md index 66c76b4..cc620c6 100644 --- a/docs.md +++ b/docs.md @@ -9,7 +9,7 @@ containerImageUrl: https://hub.docker.com/r/woodpeckerci/plugin-docker-buildx url: https://codeberg.org/woodpecker-plugins/docker-buildx --- -Woodpecker CI plugin to build multiarch Docker images with buildx. This plugin is a fork of [thegeeklab/drone-docker-buildx](https://github.com/thegeeklab/drone-docker-buildx/) which itself is a fork of [drone-plugins/drone-docker](https://github.com/drone-plugins/drone-docker). You can find the full documentation at [woodpecker-ci.org](https://woodpecker-ci.org/plugins/Docker%20Buildx). +Woodpecker CI plugin to build multiarch Docker images with buildx. This plugin is a fork of [thegeeklab/drone-docker-buildx](https://github.com/thegeeklab/drone-docker-buildx/) which itself is a fork of [drone-plugins/drone-docker](https://github.com/drone-plugins/drone-docker). ## Features @@ -37,7 +37,7 @@ It will automatically generate buildkit configuration to use custom CA certifica | `email` | *none* | sets email address to authenticates with | `registry` | `https://index.docker.io/v1/` | sets docker registry to authenticate with | `dockerfile` | `Dockerfile` | sets dockerfile to use for the image build -| `tag`/`tags` | @".tags" | sets repository tags to use for the image +| `tag`/`tags` | *none* | sets repository tags to use for the image | `platforms` | *none* | sets target platform for build ## auto_tag @@ -48,7 +48,7 @@ If it's not a tag event, and no default branch, automated tags are skipped. ## Examples -```yml +```yaml publish-next-agent: image: woodpeckerci/plugin-docker-buildx secrets: [docker_username, docker_password] @@ -62,7 +62,7 @@ If it's not a tag event, and no default branch, automated tags are skipped. event: push ``` -```yml +```yaml publish: image: woodpeckerci/plugin-docker-buildx settings: @@ -75,7 +75,7 @@ If it's not a tag event, and no default branch, automated tags are skipped. from_secret: cb_token ``` -```yml +```yaml docker-build: image: woodpeckerci/plugin-docker-buildx settings: @@ -102,6 +102,7 @@ If it's not a tag event, and no default branch, automated tags are skipped. | `debug` | `false` | enables verbose debug mode for 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) +| `tags_file` | *none* | overwrites `tags` option with values find in specified file | `context` | `.` | sets the path of the build context to use | `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 @@ -126,7 +127,7 @@ If it's not a tag event, and no default branch, automated tags are skipped. Only supported with `woodpecker >= 1.0.0` (next-da997fa3). -```yml +```yaml settings: repo: a6543/tmp,codeberg.org/6543/tmp tag: demo diff --git a/plugin/impl.go b/plugin/impl.go index 9008bc2..d6e16c8 100644 --- a/plugin/impl.go +++ b/plugin/impl.go @@ -12,6 +12,8 @@ import ( "github.com/sirupsen/logrus" "github.com/urfave/cli/v2" + + "codeberg.org/woodpecker-plugins/plugin-docker-buildx/utils" ) // Daemon defines Docker daemon parameters. @@ -52,6 +54,7 @@ type Build struct { TagsDefaultName string // Docker build auto tag name override TagsSuffix string // Docker build tags with suffix Tags cli.StringSlice // Docker build tags + TagsFile string // Docker build tags read from an file LabelsAuto bool // Docker build auto labels Labels cli.StringSlice // Docker build labels Platforms cli.StringSlice // Docker build target platforms @@ -123,6 +126,22 @@ func (p *Plugin) Validate() error { } } + // overload tags flag with tags.file if set + if p.settings.Build.TagsFile != "" { + tagsFile, err := os.ReadFile(p.settings.Build.TagsFile) + if err != nil { + return fmt.Errorf("could not read tags file: %w", err) + } + + // split file content into slice of tags + tagsFileList := strings.Split(strings.TrimSpace(string(tagsFile)), "\n") + // trim space of each tag + tagsFileList = utils.Map(tagsFileList, func(s string) string { return strings.TrimSpace(s) }) + + // finally overwrite + p.settings.Build.Tags = *cli.NewStringSlice(tagsFileList...) + } + if p.settings.Build.TagsAuto { // we only generate tags on default branch or an tag event if UseDefaultTag( diff --git a/utils/lamda.go b/utils/lamda.go new file mode 100644 index 0000000..6c91014 --- /dev/null +++ b/utils/lamda.go @@ -0,0 +1,9 @@ +package utils + +func Map[T any](in []T, fn func(T) T) []T { + out := in + for i := range in { + out[i] = fn(out[i]) + } + return out +} diff --git a/utils/lamda_test.go b/utils/lamda_test.go new file mode 100644 index 0000000..3d06e94 --- /dev/null +++ b/utils/lamda_test.go @@ -0,0 +1,18 @@ +package utils + +import ( + "strings" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestMap(t *testing.T) { + ints := []int{1, 2, 3, 4} + ints = Map(ints, func(i int) int { return i * 10 }) + assert.EqualValues(t, []int{10, 20, 30, 40}, ints) + + sl := []string{"a ", "b", " c"} + sl = Map(sl, func(s string) string { return "#" + strings.TrimSpace(s) }) + assert.EqualValues(t, []string{"#a", "#b", "#c"}, sl) +}