From 49c16053ec76ccda2716a4b861bf2f9d6b25f5b9 Mon Sep 17 00:00:00 2001 From: Rutvij Mehta Date: Mon, 15 May 2023 09:00:35 -0700 Subject: [PATCH 1/8] fix: Use unique build name for build and tag --- docker.go | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/docker.go b/docker.go index 508545d..6e5b854 100644 --- a/docker.go +++ b/docker.go @@ -195,10 +195,11 @@ func (p Plugin) Exec() error { } } - cmds = append(cmds, commandBuild(p.Build)) // docker build + buildName := getUniqueBuildName(p.Build.Repo, p.Build.Name) + cmds = append(cmds, commandBuild(p.Build, buildName)) // docker build for _, tag := range p.Build.Tags { - cmds = append(cmds, commandTag(p.Build, tag)) // docker tag + cmds = append(cmds, commandTag(p.Build, tag, buildName)) // docker tag if !p.Dryrun { cmds = append(cmds, commandPush(p.Build, tag)) // docker push @@ -299,12 +300,12 @@ func commandInfo() *exec.Cmd { } // helper function to create the docker build command. -func commandBuild(build Build) *exec.Cmd { +func commandBuild(build Build, buildName string) *exec.Cmd { args := []string{ "build", "--rm=true", "-f", build.Dockerfile, - "-t", build.Name, + "-t", buildName, } args = append(args, build.Context) @@ -461,9 +462,9 @@ func hasProxyBuildArg(build *Build, key string) bool { } // helper function to create the docker tag command. -func commandTag(build Build, tag string) *exec.Cmd { +func commandTag(build Build, tag, buildName string) *exec.Cmd { var ( - source = build.Name + source = buildName target = fmt.Sprintf("%s:%s", build.Repo, tag) ) return exec.Command( @@ -582,3 +583,14 @@ func getDigest(buildName string) (string, error) { } return "", errors.New("unable to fetch digest") } + +func getUniqueBuildName(repo, buildName string) string { + var shortenCommitSHA string + if len(buildName) <= 8 { + shortenCommitSHA = buildName + } else { + shortenCommitSHA = buildName[:8] + } + imageName := repo[strings.LastIndex(repo, "/")+1:] + return fmt.Sprintf("%s-%s", imageName, shortenCommitSHA) +} From 06e2f3535fe72425be8b72e2b15c7b384d9569de Mon Sep 17 00:00:00 2001 From: Rutvij Mehta Date: Mon, 15 May 2023 09:14:38 -0700 Subject: [PATCH 2/8] Fix UT --- docker_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker_test.go b/docker_test.go index cba894e..5b3bccb 100644 --- a/docker_test.go +++ b/docker_test.go @@ -161,7 +161,7 @@ func TestCommandBuild(t *testing.T) { tc := tc t.Run(tc.name, func(t *testing.T) { - cmd := commandBuild(tc.build) + cmd := commandBuild(tc.build, "plugins/drone-docker:latest") if !reflect.DeepEqual(cmd.String(), tc.want.String()) { t.Errorf("Got cmd %v, want %v", cmd, tc.want) From adb2b6c2c07a431d42b41ec6d12ff998eced1431 Mon Sep 17 00:00:00 2001 From: Rutvij Mehta Date: Mon, 15 May 2023 14:06:35 -0700 Subject: [PATCH 3/8] Use unique build name for build and tag --- card.go | 4 ++-- docker.go | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/card.go b/card.go index b9fea72..3bff423 100644 --- a/card.go +++ b/card.go @@ -17,8 +17,8 @@ import ( "github.com/inhies/go-bytesize" ) -func (p Plugin) writeCard() error { - cmd := exec.Command(dockerExe, "inspect", p.Build.Name) +func (p Plugin) writeCard(buildName string) error { + cmd := exec.Command(dockerExe, "inspect", buildName) data, err := cmd.CombinedOutput() if err != nil { return err diff --git a/docker.go b/docker.go index 6e5b854..3965ce8 100644 --- a/docker.go +++ b/docker.go @@ -225,12 +225,12 @@ func (p Plugin) Exec() error { } // output the adaptive card - if err := p.writeCard(); err != nil { + if err := p.writeCard(buildName); err != nil { fmt.Printf("Could not create adaptive card. %s\n", err) } if p.ArtifactFile != "" { - if digest, err := getDigest(p.Build.Name); err == nil { + if digest, err := getDigest(buildName); 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) } @@ -244,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(buildName)) // docker rmi + cmds = append(cmds, commandPrune()) // docker system prune -f for _, cmd := range cmds { cmd.Stdout = os.Stdout From 490a50eae032dc49f90ae141b30056df018d195d Mon Sep 17 00:00:00 2001 From: Rutvij Mehta Date: Tue, 16 May 2023 01:20:10 -0700 Subject: [PATCH 4/8] Use random string as temporary tag --- card.go | 4 ++-- cmd/drone-docker/main.go | 18 ++++++++++++++++++ docker.go | 22 +++++++++++----------- docker_test.go | 20 +++++++++++++------- 4 files changed, 44 insertions(+), 20 deletions(-) diff --git a/card.go b/card.go index 3bff423..0a66a43 100644 --- a/card.go +++ b/card.go @@ -17,8 +17,8 @@ import ( "github.com/inhies/go-bytesize" ) -func (p Plugin) writeCard(buildName string) error { - cmd := exec.Command(dockerExe, "inspect", buildName) +func (p Plugin) writeCard() error { + 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 bd1ace4..7270318 100644 --- a/cmd/drone-docker/main.go +++ b/cmd/drone-docker/main.go @@ -1,8 +1,10 @@ package main import ( + "math/rand" "os" "runtime" + "time" "github.com/joho/godotenv" "github.com/sirupsen/logrus" @@ -14,6 +16,7 @@ import ( var ( version = "unknown" + charset = []byte("abcdefghijklmnopqrstuvwxyz") ) func main() { @@ -318,6 +321,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 +387,20 @@ func run(c *cli.Context) error { return plugin.Exec() } +func generateTempTag() string { + tagLength := 8 + return randomString(tagLength) +} + +func randomString(n int) string { + rand.Seed(time.Now().UTC().UnixNano()) + b := make([]byte, n) + for i := range b { + b[i] = charset[rand.Intn(len(charset))] + } + return string(b) +} + func GetExecCmd() string { if runtime.GOOS == "windows" { return "C:/bin/drone-docker.exe" diff --git a/docker.go b/docker.go index 3965ce8..4e515cd 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 @@ -195,11 +196,10 @@ func (p Plugin) Exec() error { } } - buildName := getUniqueBuildName(p.Build.Repo, p.Build.Name) - cmds = append(cmds, commandBuild(p.Build, buildName)) // docker build + cmds = append(cmds, commandBuild(p.Build)) // docker build for _, tag := range p.Build.Tags { - cmds = append(cmds, commandTag(p.Build, tag, buildName)) // docker tag + cmds = append(cmds, commandTag(p.Build, tag)) // docker tag if !p.Dryrun { cmds = append(cmds, commandPush(p.Build, tag)) // docker push @@ -225,12 +225,12 @@ func (p Plugin) Exec() error { } // output the adaptive card - if err := p.writeCard(buildName); err != nil { + if err := p.writeCard(); err != nil { fmt.Printf("Could not create adaptive card. %s\n", err) } if p.ArtifactFile != "" { - if digest, err := getDigest(buildName); 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) } @@ -244,8 +244,8 @@ func (p Plugin) Exec() error { // clear the slice cmds = nil - cmds = append(cmds, commandRmi(buildName)) // 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 @@ -300,12 +300,12 @@ func commandInfo() *exec.Cmd { } // helper function to create the docker build command. -func commandBuild(build Build, buildName string) *exec.Cmd { +func commandBuild(build Build) *exec.Cmd { args := []string{ "build", "--rm=true", "-f", build.Dockerfile, - "-t", buildName, + "-t", build.TempTag, } args = append(args, build.Context) @@ -462,9 +462,9 @@ func hasProxyBuildArg(build *Build, key string) bool { } // helper function to create the docker tag command. -func commandTag(build Build, tag, buildName string) *exec.Cmd { +func commandTag(build Build, tag string) *exec.Cmd { var ( - source = buildName + 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 5b3bccb..86a08a9 100644 --- a/docker_test.go +++ b/docker_test.go @@ -16,6 +16,7 @@ func TestCommandBuild(t *testing.T) { name: "secret from env var", build: Build{ Name: "plugins/drone-docker:latest", + TempTag: "abcdefgh", Dockerfile: "Dockerfile", Context: ".", SecretEnvs: []string{ @@ -29,7 +30,7 @@ func TestCommandBuild(t *testing.T) { "-f", "Dockerfile", "-t", - "plugins/drone-docker:latest", + "abcdefgh", ".", "--secret id=foo_secret,env=FOO_SECRET_ENV_VAR", ), @@ -38,6 +39,7 @@ func TestCommandBuild(t *testing.T) { name: "secret from file", build: Build{ Name: "plugins/drone-docker:latest", + TempTag: "abcdefgh", Dockerfile: "Dockerfile", Context: ".", SecretFiles: []string{ @@ -51,7 +53,7 @@ func TestCommandBuild(t *testing.T) { "-f", "Dockerfile", "-t", - "plugins/drone-docker:latest", + "abcdefgh", ".", "--secret id=foo_secret,src=/path/to/foo_secret", ), @@ -60,6 +62,7 @@ func TestCommandBuild(t *testing.T) { name: "multiple mixed secrets", build: Build{ Name: "plugins/drone-docker:latest", + TempTag: "abcdefgh", Dockerfile: "Dockerfile", Context: ".", SecretEnvs: []string{ @@ -78,7 +81,7 @@ func TestCommandBuild(t *testing.T) { "-f", "Dockerfile", "-t", - "plugins/drone-docker:latest", + "abcdefgh", ".", "--secret id=foo_secret,env=FOO_SECRET_ENV_VAR", "--secret id=bar_secret,env=BAR_SECRET_ENV_VAR", @@ -90,6 +93,7 @@ func TestCommandBuild(t *testing.T) { name: "invalid mixed secrets", build: Build{ Name: "plugins/drone-docker:latest", + TempTag: "abcdefgh", Dockerfile: "Dockerfile", Context: ".", SecretEnvs: []string{ @@ -110,7 +114,7 @@ func TestCommandBuild(t *testing.T) { "-f", "Dockerfile", "-t", - "plugins/drone-docker:latest", + "abcdefgh", ".", ), }, @@ -118,6 +122,7 @@ func TestCommandBuild(t *testing.T) { name: "platform argument", build: Build{ Name: "plugins/drone-docker:latest", + TempTag: "abcdefgh", Dockerfile: "Dockerfile", Context: ".", Platform: "test/platform", @@ -129,7 +134,7 @@ func TestCommandBuild(t *testing.T) { "-f", "Dockerfile", "-t", - "plugins/drone-docker:latest", + "abcdefgh", ".", "--platform", "test/platform", @@ -139,6 +144,7 @@ func TestCommandBuild(t *testing.T) { name: "ssh agent", build: Build{ Name: "plugins/drone-docker:latest", + TempTag: "abcdefgh", Dockerfile: "Dockerfile", Context: ".", SSHKeyPath: "id_rsa=/root/.ssh/id_rsa", @@ -150,7 +156,7 @@ func TestCommandBuild(t *testing.T) { "-f", "Dockerfile", "-t", - "plugins/drone-docker:latest", + "abcdefgh", ".", "--ssh id_rsa=/root/.ssh/id_rsa", ), @@ -161,7 +167,7 @@ func TestCommandBuild(t *testing.T) { tc := tc t.Run(tc.name, func(t *testing.T) { - cmd := commandBuild(tc.build, "plugins/drone-docker:latest") + cmd := commandBuild(tc.build) if !reflect.DeepEqual(cmd.String(), tc.want.String()) { t.Errorf("Got cmd %v, want %v", cmd, tc.want) From 47e09cf885ddaa8a40fb712bea1cf167eceb939d Mon Sep 17 00:00:00 2001 From: Rutvij Mehta Date: Tue, 16 May 2023 01:25:04 -0700 Subject: [PATCH 5/8] Use random string as temporary tag --- docker.go | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/docker.go b/docker.go index 4e515cd..210f6df 100644 --- a/docker.go +++ b/docker.go @@ -583,14 +583,3 @@ func getDigest(buildName string) (string, error) { } return "", errors.New("unable to fetch digest") } - -func getUniqueBuildName(repo, buildName string) string { - var shortenCommitSHA string - if len(buildName) <= 8 { - shortenCommitSHA = buildName - } else { - shortenCommitSHA = buildName[:8] - } - imageName := repo[strings.LastIndex(repo, "/")+1:] - return fmt.Sprintf("%s-%s", imageName, shortenCommitSHA) -} From a38298c4f2d7cae95c5781a99dbdd1476b645b60 Mon Sep 17 00:00:00 2001 From: Rutvij Mehta Date: Tue, 16 May 2023 10:14:18 -0700 Subject: [PATCH 6/8] Use uniuri to generate temporary tag --- cmd/drone-docker/main.go | 22 +++++----------------- docker_test.go | 27 +++++++++++++++------------ go.mod | 1 + go.sum | 2 ++ 4 files changed, 23 insertions(+), 29 deletions(-) diff --git a/cmd/drone-docker/main.go b/cmd/drone-docker/main.go index 7270318..040b18a 100644 --- a/cmd/drone-docker/main.go +++ b/cmd/drone-docker/main.go @@ -1,22 +1,20 @@ package main import ( - "math/rand" "os" "runtime" - "time" + "strings" + "github.com/dchest/uniuri" + docker "github.com/drone-plugins/drone-docker" + "github.com/drone-plugins/drone-plugin-lib/drone" "github.com/joho/godotenv" "github.com/sirupsen/logrus" "github.com/urfave/cli" - - docker "github.com/drone-plugins/drone-docker" - "github.com/drone-plugins/drone-plugin-lib/drone" ) var ( version = "unknown" - charset = []byte("abcdefghijklmnopqrstuvwxyz") ) func main() { @@ -388,17 +386,7 @@ func run(c *cli.Context) error { } func generateTempTag() string { - tagLength := 8 - return randomString(tagLength) -} - -func randomString(n int) string { - rand.Seed(time.Now().UTC().UnixNano()) - b := make([]byte, n) - for i := range b { - b[i] = charset[rand.Intn(len(charset))] - } - return string(b) + return strings.ToLower(uniuri.New()) } func GetExecCmd() string { diff --git a/docker_test.go b/docker_test.go index 86a08a9..1f454a0 100644 --- a/docker_test.go +++ b/docker_test.go @@ -1,12 +1,15 @@ package docker import ( + "github.com/dchest/uniuri" "os/exec" "reflect" + "strings" "testing" ) func TestCommandBuild(t *testing.T) { + tempTag := strings.ToLower(uniuri.New()) tcs := []struct { name string build Build @@ -16,7 +19,7 @@ func TestCommandBuild(t *testing.T) { name: "secret from env var", build: Build{ Name: "plugins/drone-docker:latest", - TempTag: "abcdefgh", + TempTag: tempTag, Dockerfile: "Dockerfile", Context: ".", SecretEnvs: []string{ @@ -30,7 +33,7 @@ func TestCommandBuild(t *testing.T) { "-f", "Dockerfile", "-t", - "abcdefgh", + tempTag, ".", "--secret id=foo_secret,env=FOO_SECRET_ENV_VAR", ), @@ -39,7 +42,7 @@ func TestCommandBuild(t *testing.T) { name: "secret from file", build: Build{ Name: "plugins/drone-docker:latest", - TempTag: "abcdefgh", + TempTag: tempTag, Dockerfile: "Dockerfile", Context: ".", SecretFiles: []string{ @@ -53,7 +56,7 @@ func TestCommandBuild(t *testing.T) { "-f", "Dockerfile", "-t", - "abcdefgh", + tempTag, ".", "--secret id=foo_secret,src=/path/to/foo_secret", ), @@ -62,7 +65,7 @@ func TestCommandBuild(t *testing.T) { name: "multiple mixed secrets", build: Build{ Name: "plugins/drone-docker:latest", - TempTag: "abcdefgh", + TempTag: tempTag, Dockerfile: "Dockerfile", Context: ".", SecretEnvs: []string{ @@ -81,7 +84,7 @@ func TestCommandBuild(t *testing.T) { "-f", "Dockerfile", "-t", - "abcdefgh", + tempTag, ".", "--secret id=foo_secret,env=FOO_SECRET_ENV_VAR", "--secret id=bar_secret,env=BAR_SECRET_ENV_VAR", @@ -93,7 +96,7 @@ func TestCommandBuild(t *testing.T) { name: "invalid mixed secrets", build: Build{ Name: "plugins/drone-docker:latest", - TempTag: "abcdefgh", + TempTag: tempTag, Dockerfile: "Dockerfile", Context: ".", SecretEnvs: []string{ @@ -114,7 +117,7 @@ func TestCommandBuild(t *testing.T) { "-f", "Dockerfile", "-t", - "abcdefgh", + tempTag, ".", ), }, @@ -122,7 +125,7 @@ func TestCommandBuild(t *testing.T) { name: "platform argument", build: Build{ Name: "plugins/drone-docker:latest", - TempTag: "abcdefgh", + TempTag: tempTag, Dockerfile: "Dockerfile", Context: ".", Platform: "test/platform", @@ -134,7 +137,7 @@ func TestCommandBuild(t *testing.T) { "-f", "Dockerfile", "-t", - "abcdefgh", + tempTag, ".", "--platform", "test/platform", @@ -144,7 +147,7 @@ func TestCommandBuild(t *testing.T) { name: "ssh agent", build: Build{ Name: "plugins/drone-docker:latest", - TempTag: "abcdefgh", + TempTag: tempTag, Dockerfile: "Dockerfile", Context: ".", SSHKeyPath: "id_rsa=/root/.ssh/id_rsa", @@ -156,7 +159,7 @@ func TestCommandBuild(t *testing.T) { "-f", "Dockerfile", "-t", - "abcdefgh", + tempTag, ".", "--ssh id_rsa=/root/.ssh/id_rsa", ), diff --git a/go.mod b/go.mod index 0e09484..312568d 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 bbbe072..fbd17b5 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= From 42680c34f270b66cfe3b10ff18d79bea24fa2b58 Mon Sep 17 00:00:00 2001 From: Rutvij Mehta Date: Tue, 16 May 2023 10:17:29 -0700 Subject: [PATCH 7/8] Format imports --- cmd/drone-docker/main.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cmd/drone-docker/main.go b/cmd/drone-docker/main.go index 040b18a..89dfbc8 100644 --- a/cmd/drone-docker/main.go +++ b/cmd/drone-docker/main.go @@ -6,11 +6,12 @@ import ( "strings" "github.com/dchest/uniuri" - docker "github.com/drone-plugins/drone-docker" - "github.com/drone-plugins/drone-plugin-lib/drone" "github.com/joho/godotenv" "github.com/sirupsen/logrus" "github.com/urfave/cli" + + docker "github.com/drone-plugins/drone-docker" + "github.com/drone-plugins/drone-plugin-lib/drone" ) var ( From bd51fe001238a03316c91170f343c0b1e17e1e6d Mon Sep 17 00:00:00 2001 From: Rutvij Mehta Date: Tue, 16 May 2023 10:18:21 -0700 Subject: [PATCH 8/8] Format imports --- docker_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docker_test.go b/docker_test.go index 1f454a0..0fe7028 100644 --- a/docker_test.go +++ b/docker_test.go @@ -1,11 +1,12 @@ package docker import ( - "github.com/dchest/uniuri" "os/exec" "reflect" "strings" "testing" + + "github.com/dchest/uniuri" ) func TestCommandBuild(t *testing.T) {