Skip to content

Commit 0dd031e

Browse files
vdemeestertekton-robot
authored andcommitted
Fix git-init failure with git ssh creds 🔑
In case of a private git resources that uses `ssh` keys, `git` wouldn't find the correct `.ssh/known_hosts`. This only happens in case of the user of the container is `root`. We now treat use `root` as a special case. It should now work for any user — being root or not. Signed-off-by: Vincent Demeester <[email protected]>
1 parent 639b300 commit 0dd031e

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

‎pkg/git/git.go‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121
"os"
2222
"os/exec"
23+
"os/user"
2324
"strings"
2425

2526
homedir "github.com/mitchellh/go-homedir"
@@ -48,10 +49,19 @@ func Fetch(logger *zap.SugaredLogger, revision, path, url string) error {
4849
return err
4950
}
5051
homeenv := os.Getenv("HOME")
51-
if homeenv != "" && homeenv != homepath {
52+
u, err := user.Current()
53+
if err != nil {
54+
return err
55+
}
56+
if u.Name == "root" {
57+
if err := os.Symlink(homeenv+"/.ssh", "/root/.ssh"); err != nil {
58+
// Only do a warning, in case we don't have a real home
59+
// directory writable in our image
60+
logger.Warnf("Unexpected error: creating symlink: %v", err)
61+
}
62+
} else if homeenv != "" && homeenv != homepath {
5263
if _, err := os.Stat(homepath + "/.ssh"); os.IsNotExist(err) {
53-
err = os.Symlink(homeenv+"/.ssh", homepath+"/.ssh")
54-
if err != nil {
64+
if err := os.Symlink(homeenv+"/.ssh", homepath+"/.ssh"); err != nil {
5565
// Only do a warning, in case we don't have a real home
5666
// directory writable in our image
5767
logger.Warnf("Unexpected error: creating symlink: %v", err)

0 commit comments

Comments
 (0)