Skip to content

Commit e3e7712

Browse files
authored
fix/batch-changes: ignore per-job env vars in cache key (#1326)
1 parent ed8850d commit e3e7712

3 files changed

Lines changed: 46 additions & 0 deletions

File tree

lib/batches/execution/cache/cache.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ func (key CacheKey) mountsMetadata() ([]MountMetadata, error) {
4747
return nil, nil
4848
}
4949

50+
// perRunEnvVars resolve to per-job or per-executor values that change on
51+
// every dequeue and must be stripped from cache keys. Mirrored in
52+
// sourcegraph/sourcegraph/lib/batches/execution/cache/cache.go.
53+
var perRunEnvVars = []string{
54+
"SRC_EXECUTOR_JOB_TOKEN",
55+
"SRC_EXECUTOR_JOB_ID",
56+
"SRC_EXECUTOR_NAME",
57+
}
58+
5059
// resolveStepsEnvironment returns a slice of environments for each of the steps,
5160
// containing only the env vars that are actually used.
5261
func resolveStepsEnvironment(globalEnv []string, steps []batches.Step) ([]map[string]string, error) {
@@ -64,6 +73,9 @@ func resolveStepsEnvironment(globalEnv []string, steps []batches.Step) ([]map[st
6473
if err != nil {
6574
return nil, errors.Wrapf(err, "resolving environment for step %d", i)
6675
}
76+
for _, name := range perRunEnvVars {
77+
delete(env, name)
78+
}
6779
envs[i] = env
6880
}
6981
return envs, nil
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package cache
2+
3+
import (
4+
"encoding/json"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
9+
"github.com/sourcegraph/sourcegraph/lib/batches"
10+
"github.com/sourcegraph/sourcegraph/lib/batches/env"
11+
)
12+
13+
func TestKeyer_Key_PerRunEnvVarsIgnored(t *testing.T) {
14+
var stepEnv env.Environment
15+
require.NoError(t, json.Unmarshal(
16+
[]byte(`["SRC_EXECUTOR_JOB_TOKEN", "SRC_EXECUTOR_JOB_ID", "SRC_EXECUTOR_NAME"]`),
17+
&stepEnv,
18+
))
19+
step := batches.Step{Run: "foo", Env: stepEnv}
20+
repo := batches.Repository{ID: "r", Name: "r"}
21+
22+
unset, err := (&CacheKey{Repository: repo, Steps: []batches.Step{step}, StepIndex: 0}).Key()
23+
require.NoError(t, err)
24+
resolved, err := (&CacheKey{Repository: repo, Steps: []batches.Step{step}, StepIndex: 0, GlobalEnv: []string{
25+
"SRC_EXECUTOR_JOB_TOKEN=tok",
26+
"SRC_EXECUTOR_JOB_ID=42",
27+
"SRC_EXECUTOR_NAME=executor-abc",
28+
}}).Key()
29+
require.NoError(t, err)
30+
require.Equal(t, unset, resolved)
31+
}

lib/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ require (
2222
github.com/sourcegraph/conc v0.3.0
2323
github.com/sourcegraph/go-diff v0.7.0
2424
github.com/sourcegraph/log v0.0.0-20250923023806-517b6960b55b
25+
github.com/stretchr/testify v1.11.1
2526
github.com/urfave/cli/v3 v3.8.0
2627
github.com/xeipuuv/gojsonschema v1.2.0
2728
github.com/xlab/treeprint v1.2.0
@@ -46,6 +47,7 @@ require (
4647
github.com/charmbracelet/x/term v0.2.1 // indirect
4748
github.com/clipperhouse/uax29/v2 v2.2.0 // indirect
4849
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b // indirect
50+
github.com/davecgh/go-spew v1.1.1 // indirect
4951
github.com/dlclark/regexp2 v1.11.0 // indirect
5052
github.com/fatih/color v1.18.0 // indirect
5153
github.com/getsentry/sentry-go v0.27.0 // indirect
@@ -64,6 +66,7 @@ require (
6466
github.com/microcosm-cc/bluemonday v1.0.27 // indirect
6567
github.com/muesli/reflow v0.3.0 // indirect
6668
github.com/pkg/errors v0.9.1 // indirect
69+
github.com/pmezard/go-difflib v1.0.0 // indirect
6770
github.com/rivo/uniseg v0.4.7 // indirect
6871
github.com/rogpeppe/go-internal v1.14.1 // indirect
6972
github.com/sourcegraph/beaut v0.0.0-20240611013027-627e4c25335a // indirect

0 commit comments

Comments
 (0)