Skip to content

Instantly share code, notes, and snippets.

@efontan
Created June 1, 2020 15:31
Show Gist options
  • Save efontan/e8e8818dc0845d3bd7bf1343c984ae7b to your computer and use it in GitHub Desktop.
Save efontan/e8e8818dc0845d3bd7bf1343c984ae7b to your computer and use it in GitHub Desktop.
Clone repository with go-git using SSH Deploy Key
package main
import (
"fmt"
"log"
"os"
"strings"
"github.com/go-git/go-billy/v5/memfs"
gogit "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
"github.com/go-git/go-git/v5/storage/memory"
)
func main() {
key := strings.Replace(os.Getenv("DEPLOY_KEY"), "\\n", "\n", -1)
// Username must be "git" for SSH auth to work, not your real username.
// See https://github.com/src-d/go-git/issues/637
publicKey, err := ssh.NewPublicKeys("git", []byte(key), "")
if err != nil {
log.Fatalf("creating ssh auth method")
}
_, err = gogit.Clone(memory.NewStorage(), memfs.New(), &gogit.CloneOptions{
Auth: publicKey,
URL: "git@github.com:myuser/myproject.git",
Progress: os.Stdout,
ReferenceName: plumbing.ReferenceName("refs/heads/master"),
SingleBranch: true,
})
if err != nil {
log.Fatal(err)
}
fmt.Println("clone success")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment