From eec843a3253fb4d298d691145d1f5d1560d573c1 Mon Sep 17 00:00:00 2001 From: Alex Couture-Beil Date: Thu, 1 Apr 2021 11:08:56 -0700 Subject: [PATCH] include default ssh socket when given an ssh-based git url Signed-off-by: Alex Couture-Beil --- bake/bake.go | 6 +++++- build/ssh.go | 7 +++++++ commands/build.go | 6 +++++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/bake/bake.go b/bake/bake.go index ac7adcd6..474d30d6 100644 --- a/bake/bake.go +++ b/bake/bake.go @@ -534,7 +534,11 @@ func toBuildOpt(t *Target, inp *Input) (*build.Options, error) { } bo.Session = append(bo.Session, secrets) - ssh, err := build.ParseSSHSpecs(t.SSH) + sshSpecs := t.SSH + if len(sshSpecs) == 0 && build.IsGitSSH(contextPath) { + sshSpecs = []string{"default"} + } + ssh, err := build.ParseSSHSpecs(sshSpecs) if err != nil { return nil, err } diff --git a/build/ssh.go b/build/ssh.go index 0e4f5076..0eaeb0c1 100644 --- a/build/ssh.go +++ b/build/ssh.go @@ -5,6 +5,7 @@ import ( "github.com/moby/buildkit/session" "github.com/moby/buildkit/session/sshforward/sshprovider" + "github.com/moby/buildkit/util/gitutil" ) func ParseSSHSpecs(sl []string) (session.Attachable, error) { @@ -29,3 +30,9 @@ func parseSSH(value string) (*sshprovider.AgentConfig, error) { } return &cfg, nil } + +// IsGitSSH returns true if the given repo URL is accessed over ssh +func IsGitSSH(url string) bool { + _, gitProtocol := gitutil.ParseProtocol(url) + return gitProtocol == gitutil.SSHProtocol +} diff --git a/commands/build.go b/commands/build.go index d1d66308..12ef5949 100644 --- a/commands/build.go +++ b/commands/build.go @@ -124,7 +124,11 @@ func runBuild(dockerCli command.Cli, in buildOptions) error { } opts.Session = append(opts.Session, secrets) - ssh, err := build.ParseSSHSpecs(in.ssh) + sshSpecs := in.ssh + if len(sshSpecs) == 0 && build.IsGitSSH(in.contextPath) { + sshSpecs = []string{"default"} + } + ssh, err := build.ParseSSHSpecs(sshSpecs) if err != nil { return err }