Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion card.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions cmd/drone-docker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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"
Expand Down
11 changes: 6 additions & 5 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
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(p.Build.TempTag)) // docker rmi
cmds = append(cmds, commandPrune()) // docker system prune -f

for _, cmd := range cmds {
cmd.Stdout = os.Stdout
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand Down
22 changes: 16 additions & 6 deletions docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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{
Expand All @@ -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",
),
Expand All @@ -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{
Expand All @@ -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",
),
Expand All @@ -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{
Expand All @@ -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",
Expand All @@ -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{
Expand All @@ -110,14 +118,15 @@ func TestCommandBuild(t *testing.T) {
"-f",
"Dockerfile",
"-t",
"plugins/drone-docker:latest",
tempTag,
".",
),
},
{
name: "platform argument",
build: Build{
Name: "plugins/drone-docker:latest",
TempTag: tempTag,
Dockerfile: "Dockerfile",
Context: ".",
Platform: "test/platform",
Expand All @@ -129,7 +138,7 @@ func TestCommandBuild(t *testing.T) {
"-f",
"Dockerfile",
"-t",
"plugins/drone-docker:latest",
tempTag,
".",
"--platform",
"test/platform",
Expand All @@ -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",
Expand All @@ -150,7 +160,7 @@ func TestCommandBuild(t *testing.T) {
"-f",
"Dockerfile",
"-t",
"plugins/drone-docker:latest",
tempTag,
".",
"--ssh id_rsa=/root/.ssh/id_rsa",
),
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down