Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alternative fix for #229, random temp ID #231

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
13 changes: 12 additions & 1 deletion cmd/drone-docker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"os"

"github.com/dchest/uniuri"
"github.com/joho/godotenv"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
Expand All @@ -11,7 +12,8 @@ import (
)

var (
version = "unknown"
version = "unknown"
hexChars = []byte("abcdef0123456789")
)

func main() {
Expand Down Expand Up @@ -242,6 +244,7 @@ func run(c *cli.Context) error {
},
Build: docker.Build{
Remote: c.String("remote.url"),
Ref: c.String("commit.sha"),
Copy link
Member

Choose a reason for hiding this comment

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

the ref should be set to c.String("commit.ref")

Copy link
Author

Choose a reason for hiding this comment

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

The goal here was to not pollute the org.label-schema.vcs-ref label with the random ID, so I copied "Name" which was the sha hash. If the term is confusing, maybe it should be called CommitSha?

Copy link
Member

@bradrydzewski bradrydzewski Apr 17, 2019

Choose a reason for hiding this comment

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

the ref is not a random string, it is a git ref, for example refs/heads/master. I think the overall change is fine, but the ref should use the actual ref value.

Copy link
Author

Choose a reason for hiding this comment

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

That's true, however it would break existing behavior as this label contained the commit sha before the change by using the Name value.

Copy link
Member

Choose a reason for hiding this comment

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

I see, org.label-schema.vcs-ref is supposed to be the commit sha. I recommend removing this and sending a separate pull request. This requires additional discussion. We can merge the random Name in the short term.

Copy link
Member

Choose a reason for hiding this comment

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

@techknowlogick that would be awesome! Last I checked Gitea did not send this information in the payload. See https://github.com/drone/go-scm/blob/master/scm/driver/gitea/testdata/webhooks/tag_create.json

Copy link
Contributor

Choose a reason for hiding this comment

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

Just tested and there is a key in the webhook response labled: sha. Let me send a PR to go-scm to add this information.

Copy link
Contributor

Choose a reason for hiding this comment

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

PR is here: drone/go-scm#22

Copy link
Author

@Beanow Beanow Apr 19, 2019

Choose a reason for hiding this comment

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

Thank you for a great fix there @techknowlogick.
I had a dig to see if the same would apply to gogs, but unfortunately it seems there's no sha here.
https://github.com/gogs/go-gogs-client/blob/5898069c37e1044229e3853adba654d61e45816b/repo_hook.go#L114

Example tag payload
{
    "ref": "v2.4.3",
    "ref_type": "tag",
    "default_branch": "master",
    "repository": {
        "id": 1,
        "owner": {
            "id": 1,
            "username": "example",
            "login": "example",
            "full_name": "",
            "email": "[email protected]",
            "avatar_url": "https://git.example.com/avatars/1"
        },
        "name": "hello-cicd",
        "full_name": "example/hello-cicd",
        "description": "",
        "private": true,
        "fork": false,
        "parent": null,
        "empty": false,
        "mirror": false,
        "size": 1300480,
        "html_url": "https://git.example.com/example/hello-cicd",
        "ssh_url": "ssh://[email protected]/example/hello-cicd.git",
        "clone_url": "https://git.example.com/example/hello-cicd.git",
        "website": "",
        "stars_count": 0,
        "forks_count": 0,
        "watchers_count": 1,
        "open_issues_count": 0,
        "default_branch": "master",
        "created_at": "2018-04-17T20:45:00Z",
        "updated_at": "2019-04-19T09:20:56Z"
    },
    "sender": {
        "id": 1,
        "username": "example",
        "login": "example",
        "full_name": "",
        "email": "[email protected]",
        "avatar_url": "https://git.example.com/avatars/1"
    }
}

vs gittea's counterpart here
https://github.com/go-gitea/go-sdk/blob/master/gitea/hook.go#L196

Copy link
Author

Choose a reason for hiding this comment

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

I've opened the PR there, we'll have to see what they feel about this change.
gogs/gogs#5689

Name: c.String("commit.sha"),
Dockerfile: c.String("dockerfile"),
Context: c.String("context"),
Expand Down Expand Up @@ -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)

Choose a reason for hiding this comment

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

40 chars? Why do you want to define such a long random name?

Copy link
Author

Choose a reason for hiding this comment

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

40 matches the keyspace of git's sha1 so it has similar properties in terms of collisions.

}

return plugin.Exec()
}
3 changes: 2 additions & 1 deletion docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down