diff --git a/card.go b/card.go index b9fea72c..0a66a43a 100644 --- a/card.go +++ b/card.go @@ -18,7 +18,7 @@ import ( ) func (p Plugin) writeCard() error { - cmd := exec.Command(dockerExe, "inspect", p.Build.Name) + cmd := exec.Command(dockerExe, "inspect", p.Build.TempTag) data, err := cmd.CombinedOutput() if err != nil { return err diff --git a/cmd/drone-docker/main.go b/cmd/drone-docker/main.go index bd1ace42..89dfbc8e 100644 --- a/cmd/drone-docker/main.go +++ b/cmd/drone-docker/main.go @@ -3,7 +3,9 @@ package main import ( "os" "runtime" + "strings" + "github.com/dchest/uniuri" "github.com/joho/godotenv" "github.com/sirupsen/logrus" "github.com/urfave/cli" @@ -318,6 +320,7 @@ func run(c *cli.Context) error { Build: docker.Build{ Remote: c.String("remote.url"), Name: c.String("commit.sha"), + TempTag: generateTempTag(), Dockerfile: c.String("dockerfile"), Context: c.String("context"), Tags: c.StringSlice("tags"), @@ -383,6 +386,10 @@ func run(c *cli.Context) error { return plugin.Exec() } +func generateTempTag() string { + return strings.ToLower(uniuri.New()) +} + func GetExecCmd() string { if runtime.GOOS == "windows" { return "C:/bin/drone-docker.exe" diff --git a/docker.go b/docker.go index 508545d7..210f6dfd 100644 --- a/docker.go +++ b/docker.go @@ -45,6 +45,7 @@ type ( Build struct { Remote string // Git remote URL Name string // Docker build using default named tag + TempTag string // Temporary tag used during docker build Dockerfile string // Docker build Dockerfile Context string // Docker build context Tags []string // Docker build tags @@ -229,7 +230,7 @@ func (p Plugin) Exec() error { } if p.ArtifactFile != "" { - if digest, err := getDigest(p.Build.Name); err == nil { + if digest, err := getDigest(p.Build.TempTag); err == nil { if err = drone.WritePluginArtifactFile(p.Daemon.RegistryType, p.ArtifactFile, p.Daemon.Registry, p.Build.Repo, digest, p.Build.Tags); err != nil { fmt.Printf("failed to write plugin artifact file at path: %s with error: %s\n", p.ArtifactFile, err) } @@ -243,8 +244,8 @@ func (p Plugin) Exec() error { // clear the slice cmds = nil - cmds = append(cmds, commandRmi(p.Build.Name)) // docker rmi - cmds = append(cmds, commandPrune()) // docker system prune -f + cmds = append(cmds, commandRmi(p.Build.TempTag)) // docker rmi + cmds = append(cmds, commandPrune()) // docker system prune -f for _, cmd := range cmds { cmd.Stdout = os.Stdout @@ -304,7 +305,7 @@ func commandBuild(build Build) *exec.Cmd { "build", "--rm=true", "-f", build.Dockerfile, - "-t", build.Name, + "-t", build.TempTag, } args = append(args, build.Context) @@ -463,7 +464,7 @@ func hasProxyBuildArg(build *Build, key string) bool { // helper function to create the docker tag command. func commandTag(build Build, tag string) *exec.Cmd { var ( - source = build.Name + source = build.TempTag target = fmt.Sprintf("%s:%s", build.Repo, tag) ) return exec.Command( diff --git a/docker_test.go b/docker_test.go index cba894e9..0fe70289 100644 --- a/docker_test.go +++ b/docker_test.go @@ -3,10 +3,14 @@ package docker import ( "os/exec" "reflect" + "strings" "testing" + + "github.com/dchest/uniuri" ) func TestCommandBuild(t *testing.T) { + tempTag := strings.ToLower(uniuri.New()) tcs := []struct { name string build Build @@ -16,6 +20,7 @@ func TestCommandBuild(t *testing.T) { name: "secret from env var", build: Build{ Name: "plugins/drone-docker:latest", + TempTag: tempTag, Dockerfile: "Dockerfile", Context: ".", SecretEnvs: []string{ @@ -29,7 +34,7 @@ func TestCommandBuild(t *testing.T) { "-f", "Dockerfile", "-t", - "plugins/drone-docker:latest", + tempTag, ".", "--secret id=foo_secret,env=FOO_SECRET_ENV_VAR", ), @@ -38,6 +43,7 @@ func TestCommandBuild(t *testing.T) { name: "secret from file", build: Build{ Name: "plugins/drone-docker:latest", + TempTag: tempTag, Dockerfile: "Dockerfile", Context: ".", SecretFiles: []string{ @@ -51,7 +57,7 @@ func TestCommandBuild(t *testing.T) { "-f", "Dockerfile", "-t", - "plugins/drone-docker:latest", + tempTag, ".", "--secret id=foo_secret,src=/path/to/foo_secret", ), @@ -60,6 +66,7 @@ func TestCommandBuild(t *testing.T) { name: "multiple mixed secrets", build: Build{ Name: "plugins/drone-docker:latest", + TempTag: tempTag, Dockerfile: "Dockerfile", Context: ".", SecretEnvs: []string{ @@ -78,7 +85,7 @@ func TestCommandBuild(t *testing.T) { "-f", "Dockerfile", "-t", - "plugins/drone-docker:latest", + tempTag, ".", "--secret id=foo_secret,env=FOO_SECRET_ENV_VAR", "--secret id=bar_secret,env=BAR_SECRET_ENV_VAR", @@ -90,6 +97,7 @@ func TestCommandBuild(t *testing.T) { name: "invalid mixed secrets", build: Build{ Name: "plugins/drone-docker:latest", + TempTag: tempTag, Dockerfile: "Dockerfile", Context: ".", SecretEnvs: []string{ @@ -110,7 +118,7 @@ func TestCommandBuild(t *testing.T) { "-f", "Dockerfile", "-t", - "plugins/drone-docker:latest", + tempTag, ".", ), }, @@ -118,6 +126,7 @@ func TestCommandBuild(t *testing.T) { name: "platform argument", build: Build{ Name: "plugins/drone-docker:latest", + TempTag: tempTag, Dockerfile: "Dockerfile", Context: ".", Platform: "test/platform", @@ -129,7 +138,7 @@ func TestCommandBuild(t *testing.T) { "-f", "Dockerfile", "-t", - "plugins/drone-docker:latest", + tempTag, ".", "--platform", "test/platform", @@ -139,6 +148,7 @@ func TestCommandBuild(t *testing.T) { name: "ssh agent", build: Build{ Name: "plugins/drone-docker:latest", + TempTag: tempTag, Dockerfile: "Dockerfile", Context: ".", SSHKeyPath: "id_rsa=/root/.ssh/id_rsa", @@ -150,7 +160,7 @@ func TestCommandBuild(t *testing.T) { "-f", "Dockerfile", "-t", - "plugins/drone-docker:latest", + tempTag, ".", "--ssh id_rsa=/root/.ssh/id_rsa", ), diff --git a/go.mod b/go.mod index 0e094840..312568d5 100644 --- a/go.mod +++ b/go.mod @@ -13,6 +13,7 @@ require ( require ( github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect + github.com/dchest/uniuri v1.2.0 // indirect github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect golang.org/x/sys v0.0.0-20220731174439-a90be440212d // indirect diff --git a/go.sum b/go.sum index bbbe0724..fbd17b51 100644 --- a/go.sum +++ b/go.sum @@ -11,6 +11,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dchest/uniuri v1.2.0 h1:koIcOUdrTIivZgSLhHQvKgqdWZq5d7KdMEWF1Ud6+5g= +github.com/dchest/uniuri v1.2.0/go.mod h1:fSzm4SLHzNZvWLvWJew423PhAzkpNQYq+uNLq4kxhkY= github.com/drone-plugins/drone-plugin-lib v0.4.1 h1:47rZlmcMpr1hSp+6Gl+1Z4t+efi/gMQU3lxukC1Yg64= github.com/drone-plugins/drone-plugin-lib v0.4.1/go.mod h1:KwCu92jFjHV3xv2hu5Qg/8zBNvGwbhoJDQw/EwnTvoM= github.com/drone/drone-go v1.7.1 h1:ZX+3Rs8YHUSUQ5mkuMLmm1zr1ttiiE2YGNxF3AnyDKw=