Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/batches/execution/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ func (key CacheKey) mountsMetadata() ([]MountMetadata, error) {
return nil, nil
}

// perRunEnvVars resolve to per-job or per-executor values that change on
// every dequeue and must be stripped from cache keys. Mirrored in
// sourcegraph/sourcegraph/lib/batches/execution/cache/cache.go.
var perRunEnvVars = []string{
Comment on lines +50 to +53
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for docstring!

"SRC_EXECUTOR_JOB_TOKEN",
"SRC_EXECUTOR_JOB_ID",
"SRC_EXECUTOR_NAME",
}

// resolveStepsEnvironment returns a slice of environments for each of the steps,
// containing only the env vars that are actually used.
func resolveStepsEnvironment(globalEnv []string, steps []batches.Step) ([]map[string]string, error) {
Expand All @@ -64,6 +73,9 @@ func resolveStepsEnvironment(globalEnv []string, steps []batches.Step) ([]map[st
if err != nil {
return nil, errors.Wrapf(err, "resolving environment for step %d", i)
}
for _, name := range perRunEnvVars {
delete(env, name)
}
envs[i] = env
}
return envs, nil
Expand Down
31 changes: 31 additions & 0 deletions lib/batches/execution/cache/cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package cache

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/require"

"github.com/sourcegraph/sourcegraph/lib/batches"
"github.com/sourcegraph/sourcegraph/lib/batches/env"
)

func TestKeyer_Key_PerRunEnvVarsIgnored(t *testing.T) {
var stepEnv env.Environment
require.NoError(t, json.Unmarshal(
[]byte(`["SRC_EXECUTOR_JOB_TOKEN", "SRC_EXECUTOR_JOB_ID", "SRC_EXECUTOR_NAME"]`),
&stepEnv,
))
step := batches.Step{Run: "foo", Env: stepEnv}
repo := batches.Repository{ID: "r", Name: "r"}

unset, err := (&CacheKey{Repository: repo, Steps: []batches.Step{step}, StepIndex: 0}).Key()
require.NoError(t, err)
resolved, err := (&CacheKey{Repository: repo, Steps: []batches.Step{step}, StepIndex: 0, GlobalEnv: []string{
"SRC_EXECUTOR_JOB_TOKEN=tok",
"SRC_EXECUTOR_JOB_ID=42",
"SRC_EXECUTOR_NAME=executor-abc",
}}).Key()
require.NoError(t, err)
require.Equal(t, unset, resolved)
}
3 changes: 3 additions & 0 deletions lib/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ require (
github.com/sourcegraph/conc v0.3.0
github.com/sourcegraph/go-diff v0.7.0
github.com/sourcegraph/log v0.0.0-20250923023806-517b6960b55b
github.com/stretchr/testify v1.11.1
github.com/urfave/cli/v3 v3.8.0
github.com/xeipuuv/gojsonschema v1.2.0
github.com/xlab/treeprint v1.2.0
Expand All @@ -46,6 +47,7 @@ require (
github.com/charmbracelet/x/term v0.2.1 // indirect
github.com/clipperhouse/uax29/v2 v2.2.0 // indirect
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dlclark/regexp2 v1.11.0 // indirect
github.com/fatih/color v1.18.0 // indirect
github.com/getsentry/sentry-go v0.27.0 // indirect
Expand All @@ -64,6 +66,7 @@ require (
github.com/microcosm-cc/bluemonday v1.0.27 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/rogpeppe/go-internal v1.14.1 // indirect
github.com/sourcegraph/beaut v0.0.0-20240611013027-627e4c25335a // indirect
Expand Down
Loading