Skip to content

Commit 210b016

Browse files
aThorp96tekton-robot
authored andcommitted
fix: ensure git shell-out inherits environment variables
Custom environment variables such as those used to configure SSL cert need to be inherited by git's processes in order to apply. `cmd.Env` it not initialized to include the default env vars for the process. In order to properly inherit the variables you have to use `cmd.Environ()` to append to the default variables.
1 parent b096f8d commit 210b016

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

pkg/resolution/resolver/git/repository.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func (repo *repository) execGit(ctx context.Context, subCmd string, args ...stri
120120
}
121121
}
122122
cmd := repo.executor(ctx, "git", append(configArgs, args...)...)
123-
cmd.Env = append(cmd.Env, env...)
123+
cmd.Env = append(cmd.Environ(), env...)
124124

125125
out, err := cmd.Output()
126126
if err != nil {

pkg/resolution/resolver/git/repository_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"encoding/base64"
2323
"os/exec"
2424
"reflect"
25+
"slices"
2526
"testing"
2627
)
2728

@@ -84,8 +85,15 @@ func TestClone(t *testing.T) {
8485
if !reflect.DeepEqual(cmdParts, expectedCmd) {
8586
t.Fatalf("Expected clone command to be %v but got %v", expectedCmd, cmdParts)
8687
}
87-
if !reflect.DeepEqual(cmd.Env, expectedEnv) {
88-
t.Fatalf("Expected clone command env vars to be %v but got %v", expectedEnv, cmd.Env)
88+
89+
missingEnvVars := []string{}
90+
for _, v := range expectedEnv {
91+
if !slices.Contains(cmd.Environ(), v) {
92+
missingEnvVars = append(missingEnvVars, v)
93+
}
94+
}
95+
if len(missingEnvVars) > 0 {
96+
t.Fatalf("Clone command missing env vars %v. Got: %v", missingEnvVars, cmd.Environ())
8997
}
9098
})
9199
}

0 commit comments

Comments
 (0)