Skip to content

Commit

Permalink
Filter out docker build-arg option if no BASE_IMAGE (#3259) (#3261)
Browse files Browse the repository at this point in the history
* Filter out docker build-arg option if no BASE_IMAGE (#3259)

If the 'BASE_IMAGE' var is not defined in the environment, filter out
the build template entry for Goreleaser to invoke `docker build[x]` so
that we avoid calling `docker build` with an invliad `--build-arg= ` (no
value) option, which was causing the build to fail.

Signed-off-by: Rich Scott <[email protected]>

* Use golang.org/x/exp/slices.Delete

We are using Go 1.20 for builds, so use golang.org/x/exp/slices.Delete
instead of slices.DeleteFunc (new in Go 1.21 stdlib).

Signed-off-by: Rich Scott <[email protected]>

---------

Signed-off-by: Rich Scott <[email protected]>
  • Loading branch information
richscott authored Jan 26, 2024
1 parent d48147e commit a468449
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions magefiles/goreleaser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package main
import (
"fmt"
"os"
"regexp"
"time"

"golang.org/x/exp/slices"

goreleaserConfig "github.com/goreleaser/goreleaser/pkg/config"
"github.com/magefile/mage/sh"
"github.com/pkg/errors"
Expand Down Expand Up @@ -51,8 +54,30 @@ func goreleaserWriteMinimalReleaseConfig(dockerIds ...string) error {
dockerIdsToBuild := set(dockerIds)
dockersById := make(map[string]goreleaserConfig.Docker)
buildIds := make(map[string]bool)

// BASE_IMAGE_ARG is derived from BASE_IMAGE - see .goreleaser.yml
buildArgBaseRE := regexp.MustCompile("--build-arg=.*BASE_IMAGE_ARG")

for _, docker := range config.Dockers {
if dockerIdsToBuild[docker.ID] {
// Goreleaser constructs a faulty `docker build` command if the environment
// variable BASE_IMAGE is not defined - it constructs a `--build-arg= ` option
// (no value given) and `docker build` immediately errors. As a work-around
// we filter out that whole template option entry if BASE_IMAGE is not defined;
// see .goreleaser.yml for how it's used. Goreleaser does not support conditional
// templating of YAML in its 'dockers' section, so we filter it out here.
if os.Getenv("BASE_IMAGE") == "" {
deleteIdx := -1
for idx, tpl := range docker.BuildFlagTemplates {
if buildArgBaseRE.MatchString(tpl) {
deleteIdx = idx
break
}
}

docker.BuildFlagTemplates = slices.Delete(docker.BuildFlagTemplates, deleteIdx, deleteIdx+1)
}

dockersById[docker.ID] = docker
for _, id := range docker.IDs {
buildIds[id] = true
Expand Down

0 comments on commit a468449

Please sign in to comment.