Skip to content

Commit 001de45

Browse files
Merge pull request #390 from rutvijmehta-harness/unique_name
fix: Use unique build name for build and tag
2 parents 6cf4d8e + bd51fe0 commit 001de45

File tree

6 files changed

+33
-12
lines changed

6 files changed

+33
-12
lines changed

card.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
)
1919

2020
func (p Plugin) writeCard() error {
21-
cmd := exec.Command(dockerExe, "inspect", p.Build.Name)
21+
cmd := exec.Command(dockerExe, "inspect", p.Build.TempTag)
2222
data, err := cmd.CombinedOutput()
2323
if err != nil {
2424
return err

cmd/drone-docker/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package main
33
import (
44
"os"
55
"runtime"
6+
"strings"
67

8+
"github.com/dchest/uniuri"
79
"github.com/joho/godotenv"
810
"github.com/sirupsen/logrus"
911
"github.com/urfave/cli"
@@ -318,6 +320,7 @@ func run(c *cli.Context) error {
318320
Build: docker.Build{
319321
Remote: c.String("remote.url"),
320322
Name: c.String("commit.sha"),
323+
TempTag: generateTempTag(),
321324
Dockerfile: c.String("dockerfile"),
322325
Context: c.String("context"),
323326
Tags: c.StringSlice("tags"),
@@ -383,6 +386,10 @@ func run(c *cli.Context) error {
383386
return plugin.Exec()
384387
}
385388

389+
func generateTempTag() string {
390+
return strings.ToLower(uniuri.New())
391+
}
392+
386393
func GetExecCmd() string {
387394
if runtime.GOOS == "windows" {
388395
return "C:/bin/drone-docker.exe"

docker.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type (
4545
Build struct {
4646
Remote string // Git remote URL
4747
Name string // Docker build using default named tag
48+
TempTag string // Temporary tag used during docker build
4849
Dockerfile string // Docker build Dockerfile
4950
Context string // Docker build context
5051
Tags []string // Docker build tags
@@ -229,7 +230,7 @@ func (p Plugin) Exec() error {
229230
}
230231

231232
if p.ArtifactFile != "" {
232-
if digest, err := getDigest(p.Build.Name); err == nil {
233+
if digest, err := getDigest(p.Build.TempTag); err == nil {
233234
if err = drone.WritePluginArtifactFile(p.Daemon.RegistryType, p.ArtifactFile, p.Daemon.Registry, p.Build.Repo, digest, p.Build.Tags); err != nil {
234235
fmt.Printf("failed to write plugin artifact file at path: %s with error: %s\n", p.ArtifactFile, err)
235236
}
@@ -243,8 +244,8 @@ func (p Plugin) Exec() error {
243244
// clear the slice
244245
cmds = nil
245246

246-
cmds = append(cmds, commandRmi(p.Build.Name)) // docker rmi
247-
cmds = append(cmds, commandPrune()) // docker system prune -f
247+
cmds = append(cmds, commandRmi(p.Build.TempTag)) // docker rmi
248+
cmds = append(cmds, commandPrune()) // docker system prune -f
248249

249250
for _, cmd := range cmds {
250251
cmd.Stdout = os.Stdout
@@ -304,7 +305,7 @@ func commandBuild(build Build) *exec.Cmd {
304305
"build",
305306
"--rm=true",
306307
"-f", build.Dockerfile,
307-
"-t", build.Name,
308+
"-t", build.TempTag,
308309
}
309310

310311
args = append(args, build.Context)
@@ -463,7 +464,7 @@ func hasProxyBuildArg(build *Build, key string) bool {
463464
// helper function to create the docker tag command.
464465
func commandTag(build Build, tag string) *exec.Cmd {
465466
var (
466-
source = build.Name
467+
source = build.TempTag
467468
target = fmt.Sprintf("%s:%s", build.Repo, tag)
468469
)
469470
return exec.Command(

docker_test.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ package docker
33
import (
44
"os/exec"
55
"reflect"
6+
"strings"
67
"testing"
8+
9+
"github.com/dchest/uniuri"
710
)
811

912
func TestCommandBuild(t *testing.T) {
13+
tempTag := strings.ToLower(uniuri.New())
1014
tcs := []struct {
1115
name string
1216
build Build
@@ -16,6 +20,7 @@ func TestCommandBuild(t *testing.T) {
1620
name: "secret from env var",
1721
build: Build{
1822
Name: "plugins/drone-docker:latest",
23+
TempTag: tempTag,
1924
Dockerfile: "Dockerfile",
2025
Context: ".",
2126
SecretEnvs: []string{
@@ -29,7 +34,7 @@ func TestCommandBuild(t *testing.T) {
2934
"-f",
3035
"Dockerfile",
3136
"-t",
32-
"plugins/drone-docker:latest",
37+
tempTag,
3338
".",
3439
"--secret id=foo_secret,env=FOO_SECRET_ENV_VAR",
3540
),
@@ -38,6 +43,7 @@ func TestCommandBuild(t *testing.T) {
3843
name: "secret from file",
3944
build: Build{
4045
Name: "plugins/drone-docker:latest",
46+
TempTag: tempTag,
4147
Dockerfile: "Dockerfile",
4248
Context: ".",
4349
SecretFiles: []string{
@@ -51,7 +57,7 @@ func TestCommandBuild(t *testing.T) {
5157
"-f",
5258
"Dockerfile",
5359
"-t",
54-
"plugins/drone-docker:latest",
60+
tempTag,
5561
".",
5662
"--secret id=foo_secret,src=/path/to/foo_secret",
5763
),
@@ -60,6 +66,7 @@ func TestCommandBuild(t *testing.T) {
6066
name: "multiple mixed secrets",
6167
build: Build{
6268
Name: "plugins/drone-docker:latest",
69+
TempTag: tempTag,
6370
Dockerfile: "Dockerfile",
6471
Context: ".",
6572
SecretEnvs: []string{
@@ -78,7 +85,7 @@ func TestCommandBuild(t *testing.T) {
7885
"-f",
7986
"Dockerfile",
8087
"-t",
81-
"plugins/drone-docker:latest",
88+
tempTag,
8289
".",
8390
"--secret id=foo_secret,env=FOO_SECRET_ENV_VAR",
8491
"--secret id=bar_secret,env=BAR_SECRET_ENV_VAR",
@@ -90,6 +97,7 @@ func TestCommandBuild(t *testing.T) {
9097
name: "invalid mixed secrets",
9198
build: Build{
9299
Name: "plugins/drone-docker:latest",
100+
TempTag: tempTag,
93101
Dockerfile: "Dockerfile",
94102
Context: ".",
95103
SecretEnvs: []string{
@@ -110,14 +118,15 @@ func TestCommandBuild(t *testing.T) {
110118
"-f",
111119
"Dockerfile",
112120
"-t",
113-
"plugins/drone-docker:latest",
121+
tempTag,
114122
".",
115123
),
116124
},
117125
{
118126
name: "platform argument",
119127
build: Build{
120128
Name: "plugins/drone-docker:latest",
129+
TempTag: tempTag,
121130
Dockerfile: "Dockerfile",
122131
Context: ".",
123132
Platform: "test/platform",
@@ -129,7 +138,7 @@ func TestCommandBuild(t *testing.T) {
129138
"-f",
130139
"Dockerfile",
131140
"-t",
132-
"plugins/drone-docker:latest",
141+
tempTag,
133142
".",
134143
"--platform",
135144
"test/platform",
@@ -139,6 +148,7 @@ func TestCommandBuild(t *testing.T) {
139148
name: "ssh agent",
140149
build: Build{
141150
Name: "plugins/drone-docker:latest",
151+
TempTag: tempTag,
142152
Dockerfile: "Dockerfile",
143153
Context: ".",
144154
SSHKeyPath: "id_rsa=/root/.ssh/id_rsa",
@@ -150,7 +160,7 @@ func TestCommandBuild(t *testing.T) {
150160
"-f",
151161
"Dockerfile",
152162
"-t",
153-
"plugins/drone-docker:latest",
163+
tempTag,
154164
".",
155165
"--ssh id_rsa=/root/.ssh/id_rsa",
156166
),

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require (
1313

1414
require (
1515
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
16+
github.com/dchest/uniuri v1.2.0 // indirect
1617
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af // indirect
1718
github.com/russross/blackfriday/v2 v2.1.0 // indirect
1819
golang.org/x/sys v0.0.0-20220731174439-a90be440212d // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t
1111
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1212
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
1313
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
14+
github.com/dchest/uniuri v1.2.0 h1:koIcOUdrTIivZgSLhHQvKgqdWZq5d7KdMEWF1Ud6+5g=
15+
github.com/dchest/uniuri v1.2.0/go.mod h1:fSzm4SLHzNZvWLvWJew423PhAzkpNQYq+uNLq4kxhkY=
1416
github.com/drone-plugins/drone-plugin-lib v0.4.1 h1:47rZlmcMpr1hSp+6Gl+1Z4t+efi/gMQU3lxukC1Yg64=
1517
github.com/drone-plugins/drone-plugin-lib v0.4.1/go.mod h1:KwCu92jFjHV3xv2hu5Qg/8zBNvGwbhoJDQw/EwnTvoM=
1618
github.com/drone/drone-go v1.7.1 h1:ZX+3Rs8YHUSUQ5mkuMLmm1zr1ttiiE2YGNxF3AnyDKw=

0 commit comments

Comments
 (0)