mirror of
https://github.com/thegeeklab/wp-docker-buildx.git
synced 2024-11-22 00:00:40 +00:00
Possible fix for https://github.com/thegeeklab/drone-docker-buildx/issues/28 (#30)
This commit is contained in:
parent
a6cc96fbda
commit
2e0e0dd7d7
@ -61,13 +61,12 @@ func commandBuildx() *exec.Cmd {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// helper function to create the docker build command.
|
// helper function to create the docker build command.
|
||||||
func commandBuild(build Build) *exec.Cmd {
|
func commandBuild(build Build, dryrun bool) *exec.Cmd {
|
||||||
args := []string{
|
args := []string{
|
||||||
"buildx",
|
"buildx",
|
||||||
"build",
|
"build",
|
||||||
"--rm=true",
|
"--rm=true",
|
||||||
"-f", build.Dockerfile,
|
"-f", build.Dockerfile,
|
||||||
"-t", build.Name,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
defaultBuildArgs := []string{
|
defaultBuildArgs := []string{
|
||||||
@ -75,6 +74,9 @@ func commandBuild(build Build) *exec.Cmd {
|
|||||||
}
|
}
|
||||||
|
|
||||||
args = append(args, build.Context)
|
args = append(args, build.Context)
|
||||||
|
if ! dryrun {
|
||||||
|
args = append(args, "--push")
|
||||||
|
}
|
||||||
if build.Squash {
|
if build.Squash {
|
||||||
args = append(args, "--squash")
|
args = append(args, "--squash")
|
||||||
}
|
}
|
||||||
@ -106,16 +108,14 @@ func commandBuild(build Build) *exec.Cmd {
|
|||||||
args = append(args, "--quiet")
|
args = append(args, "--quiet")
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(build.Platforms.Value()) > 1 {
|
|
||||||
args = append(args, "--push")
|
|
||||||
} else {
|
|
||||||
args = append(args, "--load")
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(build.Platforms.Value()) > 0 {
|
if len(build.Platforms.Value()) > 0 {
|
||||||
args = append(args, "--platform", strings.Join(build.Platforms.Value()[:], ","))
|
args = append(args, "--platform", strings.Join(build.Platforms.Value()[:], ","))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, arg := range build.Tags.Value() {
|
||||||
|
args = append(args, "-t", fmt.Sprintf("%s:%s", build.Repo, arg))
|
||||||
|
}
|
||||||
|
|
||||||
return exec.Command(dockerExe, args...)
|
return exec.Command(dockerExe, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -162,23 +162,6 @@ func hasProxyBuildArg(build *Build, key string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// helper function to create the docker tag command.
|
|
||||||
func commandTag(build Build, tag string) *exec.Cmd {
|
|
||||||
var (
|
|
||||||
source = build.Name
|
|
||||||
target = fmt.Sprintf("%s:%s", build.Repo, tag)
|
|
||||||
)
|
|
||||||
return exec.Command(
|
|
||||||
dockerExe, "tag", source, target,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// helper function to create the docker push command.
|
|
||||||
func commandPush(build Build, tag string) *exec.Cmd {
|
|
||||||
target := fmt.Sprintf("%s:%s", build.Repo, tag)
|
|
||||||
return exec.Command(dockerExe, "push", target)
|
|
||||||
}
|
|
||||||
|
|
||||||
// helper function to create the docker daemon command.
|
// helper function to create the docker daemon command.
|
||||||
func commandDaemon(daemon Daemon) *exec.Cmd {
|
func commandDaemon(daemon Daemon) *exec.Cmd {
|
||||||
args := []string{
|
args := []string{
|
||||||
@ -216,24 +199,6 @@ func commandDaemon(daemon Daemon) *exec.Cmd {
|
|||||||
return exec.Command(dockerdExe, args...)
|
return exec.Command(dockerdExe, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// helper to check if args match "docker prune"
|
|
||||||
func isCommandPrune(args []string) bool {
|
|
||||||
return len(args) > 3 && args[2] == "prune"
|
|
||||||
}
|
|
||||||
|
|
||||||
func commandPrune() *exec.Cmd {
|
|
||||||
return exec.Command(dockerExe, "system", "prune", "-f")
|
|
||||||
}
|
|
||||||
|
|
||||||
// helper to check if args match "docker rmi"
|
|
||||||
func isCommandRmi(args []string) bool {
|
|
||||||
return len(args) > 2 && args[1] == "rmi"
|
|
||||||
}
|
|
||||||
|
|
||||||
func commandRmi(tag string) *exec.Cmd {
|
|
||||||
return exec.Command(dockerExe, "rmi", tag)
|
|
||||||
}
|
|
||||||
|
|
||||||
// trace writes each command to stdout with the command wrapped in an xml
|
// trace writes each command to stdout with the command wrapped in an xml
|
||||||
// tag so that it can be extracted and displayed in the logs.
|
// tag so that it can be extracted and displayed in the logs.
|
||||||
func trace(cmd *exec.Cmd) {
|
func trace(cmd *exec.Cmd) {
|
||||||
|
@ -83,10 +83,6 @@ func (p *Plugin) Validate() error {
|
|||||||
p.settings.Build.Ref = p.pipeline.Commit.Ref
|
p.settings.Build.Ref = p.pipeline.Commit.Ref
|
||||||
p.settings.Daemon.Registry = p.settings.Login.Registry
|
p.settings.Daemon.Registry = p.settings.Login.Registry
|
||||||
|
|
||||||
if len(p.settings.Build.Platforms.Value()) > 1 && p.settings.Dryrun {
|
|
||||||
return fmt.Errorf("dryrun is not supported on multi-platform builds")
|
|
||||||
}
|
|
||||||
|
|
||||||
if p.settings.Build.TagsAuto {
|
if p.settings.Build.TagsAuto {
|
||||||
// return true if tag event or default branch
|
// return true if tag event or default branch
|
||||||
if UseDefaultTag(
|
if UseDefaultTag(
|
||||||
@ -177,20 +173,7 @@ func (p *Plugin) Execute() error {
|
|||||||
cmds = append(cmds, commandPull(img))
|
cmds = append(cmds, commandPull(img))
|
||||||
}
|
}
|
||||||
|
|
||||||
cmds = append(cmds, commandBuild(p.settings.Build)) // docker build
|
cmds = append(cmds, commandBuild(p.settings.Build, p.settings.Dryrun)) // docker build
|
||||||
|
|
||||||
for _, tag := range p.settings.Build.Tags.Value() {
|
|
||||||
cmds = append(cmds, commandTag(p.settings.Build, tag)) // docker tag
|
|
||||||
|
|
||||||
if !p.settings.Dryrun {
|
|
||||||
cmds = append(cmds, commandPush(p.settings.Build, tag)) // docker push
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if p.settings.Cleanup {
|
|
||||||
cmds = append(cmds, commandRmi(p.settings.Build.Name)) // docker rmi
|
|
||||||
cmds = append(cmds, commandPrune()) // docker system prune -f
|
|
||||||
}
|
|
||||||
|
|
||||||
// execute all commands in batch mode.
|
// execute all commands in batch mode.
|
||||||
for _, cmd := range cmds {
|
for _, cmd := range cmds {
|
||||||
@ -201,10 +184,6 @@ func (p *Plugin) Execute() error {
|
|||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
if err != nil && isCommandPull(cmd.Args) {
|
if err != nil && isCommandPull(cmd.Args) {
|
||||||
fmt.Printf("Could not pull cache-from image %s. Ignoring...\n", cmd.Args[2])
|
fmt.Printf("Could not pull cache-from image %s. Ignoring...\n", cmd.Args[2])
|
||||||
} else if err != nil && isCommandPrune(cmd.Args) {
|
|
||||||
fmt.Printf("Could not prune system containers. Ignoring...\n")
|
|
||||||
} else if err != nil && isCommandRmi(cmd.Args) {
|
|
||||||
fmt.Printf("Could not remove image %s. Ignoring...\n", cmd.Args[2])
|
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user