From 47dc8555ad13d56ad6d28d6b5ae7ba63628e93f6 Mon Sep 17 00:00:00 2001 From: Shubham Agrawal Date: Wed, 12 Jan 2022 22:32:57 +0530 Subject: [PATCH] Fix ECR & GCR docker publish on windows (#352) --- cmd/drone-acr/main.go | 7 ++++-- cmd/drone-docker/main.go | 49 ++++++++++++++++++++++---------------- cmd/drone-ecr/main.go | 7 ++++-- cmd/drone-gcr/main.go | 7 ++++-- docker.go | 9 +++++++ scripts/windows/latest.ps1 | 2 +- scripts/windows/tag.ps1 | 2 +- 7 files changed, 55 insertions(+), 28 deletions(-) diff --git a/cmd/drone-acr/main.go b/cmd/drone-acr/main.go index 62e65c5..6a7f097 100644 --- a/cmd/drone-acr/main.go +++ b/cmd/drone-acr/main.go @@ -7,6 +7,9 @@ import ( "strings" "github.com/joho/godotenv" + "github.com/sirupsen/logrus" + + docker "github.com/drone-plugins/drone-docker" ) func main() { @@ -40,12 +43,12 @@ func main() { os.Setenv("DOCKER_PASSWORD", password) // invoke the base docker plugin binary - cmd := exec.Command("drone-docker") + cmd := exec.Command(docker.GetDroneDockerExecCmd()) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr err := cmd.Run() if err != nil { - os.Exit(1) + logrus.Fatal(err) } } diff --git a/cmd/drone-docker/main.go b/cmd/drone-docker/main.go index 3385bb8..b76cd6b 100644 --- a/cmd/drone-docker/main.go +++ b/cmd/drone-docker/main.go @@ -2,6 +2,7 @@ package main import ( "os" + "runtime" "github.com/joho/godotenv" "github.com/sirupsen/logrus" @@ -267,26 +268,26 @@ func run(c *cli.Context) error { Config: c.String("docker.config"), }, Build: docker.Build{ - Remote: c.String("remote.url"), - Name: c.String("commit.sha"), - Dockerfile: c.String("dockerfile"), - Context: c.String("context"), - Tags: c.StringSlice("tags"), - Args: c.StringSlice("args"), - ArgsEnv: c.StringSlice("args-from-env"), - Target: c.String("target"), - Squash: c.Bool("squash"), - Pull: c.BoolT("pull-image"), - CacheFrom: c.StringSlice("cache-from"), - Compress: c.Bool("compress"), - Repo: c.String("repo"), - Labels: c.StringSlice("custom-labels"), - LabelSchema: c.StringSlice("label-schema"), - AutoLabel: c.BoolT("auto-label"), - Link: c.String("link"), - NoCache: c.Bool("no-cache"), - AddHost: c.StringSlice("add-host"), - Quiet: c.Bool("quiet"), + Remote: c.String("remote.url"), + Name: c.String("commit.sha"), + Dockerfile: c.String("dockerfile"), + Context: c.String("context"), + Tags: c.StringSlice("tags"), + Args: c.StringSlice("args"), + ArgsEnv: c.StringSlice("args-from-env"), + Target: c.String("target"), + Squash: c.Bool("squash"), + Pull: c.BoolT("pull-image"), + CacheFrom: c.StringSlice("cache-from"), + Compress: c.Bool("compress"), + Repo: c.String("repo"), + Labels: c.StringSlice("custom-labels"), + LabelSchema: c.StringSlice("label-schema"), + AutoLabel: c.BoolT("auto-label"), + Link: c.String("link"), + NoCache: c.Bool("no-cache"), + AddHost: c.StringSlice("add-host"), + Quiet: c.Bool("quiet"), }, Daemon: docker.Daemon{ Registry: c.String("docker.registry"), @@ -327,3 +328,11 @@ func run(c *cli.Context) error { return plugin.Exec() } + +func GetExecCmd() string { + if runtime.GOOS == "windows" { + return "C:/bin/drone-docker.exe" + } + + return "drone-docker" +} diff --git a/cmd/drone-ecr/main.go b/cmd/drone-ecr/main.go index 07450c3..f91e4b5 100644 --- a/cmd/drone-ecr/main.go +++ b/cmd/drone-ecr/main.go @@ -11,12 +11,15 @@ import ( "strings" "github.com/joho/godotenv" + "github.com/sirupsen/logrus" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/aws/credentials/stscreds" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/ecr" + + docker "github.com/drone-plugins/drone-docker" ) const defaultRegion = "us-east-1" @@ -110,11 +113,11 @@ func main() { os.Setenv("DOCKER_PASSWORD", password) // invoke the base docker plugin binary - cmd := exec.Command("drone-docker") + cmd := exec.Command(docker.GetDroneDockerExecCmd()) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr if err = cmd.Run(); err != nil { - os.Exit(1) + logrus.Fatal(err) } } diff --git a/cmd/drone-gcr/main.go b/cmd/drone-gcr/main.go index addddcb..92ecd9c 100644 --- a/cmd/drone-gcr/main.go +++ b/cmd/drone-gcr/main.go @@ -8,6 +8,9 @@ import ( "strings" "github.com/joho/godotenv" + "github.com/sirupsen/logrus" + + docker "github.com/drone-plugins/drone-docker" ) // gcr default username @@ -54,12 +57,12 @@ func main() { os.Setenv("DOCKER_PASSWORD", password) // invoke the base docker plugin binary - cmd := exec.Command("drone-docker") + cmd := exec.Command(docker.GetDroneDockerExecCmd()) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr err = cmd.Run() if err != nil { - os.Exit(1) + logrus.Fatal(err) } } diff --git a/docker.go b/docker.go index 9effe4c..5de6d58 100644 --- a/docker.go +++ b/docker.go @@ -6,6 +6,7 @@ import ( "os" "os/exec" "path/filepath" + "runtime" "strings" "time" ) @@ -416,3 +417,11 @@ func commandRmi(tag string) *exec.Cmd { func trace(cmd *exec.Cmd) { fmt.Fprintf(os.Stdout, "+ %s\n", strings.Join(cmd.Args, " ")) } + +func GetDroneDockerExecCmd() string { + if runtime.GOOS == "windows" { + return "C:/bin/drone-docker.exe" + } + + return "drone-docker" +} diff --git a/scripts/windows/latest.ps1 b/scripts/windows/latest.ps1 index 8663c63..870e255 100644 --- a/scripts/windows/latest.ps1 +++ b/scripts/windows/latest.ps1 @@ -20,7 +20,7 @@ echo $env:VERSION echo $env:REGISTRY # build the binary -Write-Host "+ go build -o release/windows/amd64/drone-${env:REGISTRY}.exe"; +Write-Host "+ go build -o release/windows/amd64/drone-${env:REGISTRY}.exe ./cmd/drone-${env:REGISTRY}"; go build -o release/windows/amd64/drone-${env:REGISTRY}.exe ./cmd/drone-${env:REGISTRY} # build and publish the docker image diff --git a/scripts/windows/tag.ps1 b/scripts/windows/tag.ps1 index d1cac5b..65c32a6 100644 --- a/scripts/windows/tag.ps1 +++ b/scripts/windows/tag.ps1 @@ -30,7 +30,7 @@ echo $env:GOARCH echo $env:VERSION # build the binary -Write-Host "+ go build -o release/windows/amd64/drone-${env:REGISTRY}.exe" +Write-Host "+ go build -o release/windows/amd64/drone-${env:REGISTRY}.exe ./cmd/drone-${env:REGISTRY}" go build -o release/windows/amd64/drone-${env:REGISTRY}.exe ./cmd/drone-${env:REGISTRY} # authenticate with the docker registry