Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions card.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 22 additions & 10 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -224,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)
}
Expand All @@ -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(buildName)) // docker rmi
cmds = append(cmds, commandPrune()) // docker system prune -f

for _, cmd := range cmds {
cmd.Stdout = os.Stdout
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -582,3 +583,14 @@ func getDigest(buildName string) (string, error) {
}
return "", errors.New("unable to fetch digest")
}

func getUniqueBuildName(repo, buildName string) string {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if build is triggered for a run without git clone?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved away from this approach. We now generate a temporary tag using a random 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)
}
2 changes: 1 addition & 1 deletion docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down