From 3b4080d54e312677d728ea22180c69e7b2118834 Mon Sep 17 00:00:00 2001 From: Robin IT Date: Wed, 17 Apr 2019 23:26:33 +0200 Subject: [PATCH] Alternative fix for #229, random temp ID Closes #229 --- cmd/drone-docker/main.go | 13 ++++++++++++- docker.go | 3 ++- go.mod | 1 + go.sum | 2 ++ 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/cmd/drone-docker/main.go b/cmd/drone-docker/main.go index 712ace4c..595b07cc 100644 --- a/cmd/drone-docker/main.go +++ b/cmd/drone-docker/main.go @@ -3,6 +3,7 @@ package main import ( "os" + "github.com/dchest/uniuri" "github.com/joho/godotenv" "github.com/sirupsen/logrus" "github.com/urfave/cli" @@ -11,7 +12,8 @@ import ( ) var ( - version = "unknown" + version = "unknown" + hexChars = []byte("abcdef0123456789") ) func main() { @@ -242,6 +244,7 @@ func run(c *cli.Context) error { }, Build: docker.Build{ Remote: c.String("remote.url"), + Ref: c.String("commit.sha"), Name: c.String("commit.sha"), Dockerfile: c.String("dockerfile"), Context: c.String("context"), @@ -290,5 +293,13 @@ func run(c *cli.Context) error { } } + // Make sure we always have a name when commit.sha is not available. + // See https://github.com/drone-plugins/drone-docker/issues/229 + if plugin.Build.Name == "" { + // Use hex encoding as docker requires lowercase. + // Note: we can't use 64 characters as it conflicts with sha256 image hashes. + plugin.Build.Name = uniuri.NewLenChars(40, hexChars) + } + return plugin.Exec() } diff --git a/docker.go b/docker.go index e6e53862..9d7ed758 100644 --- a/docker.go +++ b/docker.go @@ -38,6 +38,7 @@ type ( // Build defines Docker build parameters. Build struct { Remote string // Git remote URL + Ref string // Git commit hash Name string // Docker build using default named tag Dockerfile string // Docker build Dockerfile Context string // Docker build context @@ -237,7 +238,7 @@ func commandBuild(build Build) *exec.Cmd { labelSchema := []string{ "schema-version=1.0", fmt.Sprintf("build-date=%s", time.Now().Format(time.RFC3339)), - fmt.Sprintf("vcs-ref=%s", build.Name), + fmt.Sprintf("vcs-ref=%s", build.Ref), fmt.Sprintf("vcs-url=%s", build.Remote), } diff --git a/go.mod b/go.mod index 9f5a2f23..2ab3993c 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/drone-plugins/drone-docker require ( github.com/aws/aws-sdk-go v1.16.15 github.com/coreos/go-semver v0.2.0 + github.com/dchest/uniuri v0.0.0-20160212164326-8902c56451e9 github.com/joho/godotenv v1.3.0 github.com/sirupsen/logrus v1.3.0 github.com/urfave/cli v1.20.0 diff --git a/go.sum b/go.sum index 258e8edd..413b60cf 100644 --- a/go.sum +++ b/go.sum @@ -6,6 +6,8 @@ github.com/coreos/go-semver v0.2.0 h1:3Jm3tLmsgAYcjC+4Up7hJrFBPr+n7rAqYeSw/SZazu github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= 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 v0.0.0-20160212164326-8902c56451e9 h1:74lLNRzvsdIlkTgfDSMuaPjBr4cf6k7pwQQANm/yLKU= +github.com/dchest/uniuri v0.0.0-20160212164326-8902c56451e9/go.mod h1:GgB8SF9nRG+GqaDtLcwJZsQFhcogVCJ79j4EdT0c2V4= github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM= github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=