enhance: add runner.tool_cache_mode and default it to none (#1171)

The current shared tools cache is not concurrency-safe, e.g. multiple jobs can write and corrupt it, for example `setup-go` with explicit go version under concurrency reliably corrupts the tool cache and fails all jobs.

This adds a new `runner.tool_cache_mode` (and `--tool-cache-mode` exec option) option which defaults to unshared tools cache:

- `none` mounts nothing, so a job uses what its image ships there and discards what it installs
- `shared` keeps the single volume every job reuses, and warns when `runner.capacity` is above 1

Under `none` effective tool cache can only come from the image or host, which is the same as it is on GitHub Actions which ships many preinstalled tools in its fat VM images.

Co-authored-by: bircni <bircni@icloud.com>
Reviewed-on: https://gitea.com/gitea/runner/pulls/1171
Reviewed-by: bircni <bircni@icloud.com>
Co-authored-by: silverwind <me@silverwind.io>
This commit is contained in:
silverwind
2026-08-18 20:16:11 +00:00
committed by silverwind
parent be90c01468
commit 3f70822458
10 changed files with 147 additions and 13 deletions
+18 -1
View File
@@ -13,6 +13,7 @@ import (
"maps"
"os"
"path/filepath"
"slices"
"strconv"
"strings"
"time"
@@ -68,6 +69,15 @@ type executeArgs struct {
cacheHandler *artifactcache.Handler
network string
githubInstance string
toolCacheMode string
}
// sharedToolCache reports whether mode mounts one tool cache for every job.
func sharedToolCache(mode string) (bool, error) {
if !slices.Contains(config.ToolCacheModes, mode) {
return false, fmt.Errorf("invalid --tool-cache-mode %q: must be one of %q", mode, config.ToolCacheModes)
}
return mode == config.ToolCacheModeShared, nil
}
// WorkflowsPath returns path to workflow file(s)
@@ -427,6 +437,11 @@ func runExec(ctx context.Context, execArgs *executeArgs) func(cmd *cobra.Command
proxyEnv := run.JobProxyEnv(env, env["ACTIONS_CACHE_URL"], nil)
maps.Copy(env, proxyEnv)
shared, err := sharedToolCache(execArgs.toolCacheMode)
if err != nil {
return err
}
// run the plan
config := &runner.Config{
Workdir: execArgs.Workdir(),
@@ -467,7 +482,8 @@ func runExec(ctx context.Context, execArgs *executeArgs) func(cmd *cobra.Command
PlatformPicker: func(_ []string) string {
return execArgs.image
},
ValidVolumes: []string{"**"}, // All volumes are allowed for `exec` command
ValidVolumes: []string{"**"}, // All volumes are allowed for `exec` command
SharedToolCache: shared,
}
config.Env["ACT_EXEC"] = "true"
@@ -543,6 +559,7 @@ func loadExecCmd(ctx context.Context) *cobra.Command {
execCmd.PersistentFlags().BoolVarP(&execArg.debug, "debug", "d", false, "enable debug log")
execCmd.PersistentFlags().BoolVarP(&execArg.dryrun, "dryrun", "n", false, "dryrun mode")
execCmd.PersistentFlags().StringVarP(&execArg.image, "image", "i", "docker.gitea.com/runner-images:ubuntu-latest", "Docker image to use. Use \"-self-hosted\" to run directly on the host.")
execCmd.PersistentFlags().StringVarP(&execArg.toolCacheMode, "tool-cache-mode", "", config.ToolCacheModeNone, "What to mount at RUNNER_TOOL_CACHE: none, or shared to reuse one tool cache across runs")
execCmd.PersistentFlags().StringVarP(&execArg.network, "network", "", "", "Specify the network to which the container will connect")
execCmd.PersistentFlags().StringVarP(&execArg.githubInstance, "gitea-instance", "", "", "Gitea instance to use.")
+15
View File
@@ -12,6 +12,8 @@ import (
"strings"
"testing"
"gitea.com/gitea/runner/internal/pkg/config"
"gitea.dev/actionslib/pkg/model"
"github.com/stretchr/testify/require"
"go.yaml.in/yaml/v4"
@@ -28,6 +30,19 @@ func TestExecuteArgsResolve(t *testing.T) {
require.Equal(t, abs, args.resolve(abs))
}
func TestSharedToolCache(t *testing.T) {
shared, err := sharedToolCache(config.ToolCacheModeShared)
require.NoError(t, err)
require.True(t, shared)
shared, err = sharedToolCache(config.ToolCacheModeNone)
require.NoError(t, err)
require.False(t, shared)
_, err = sharedToolCache("everyone")
require.ErrorContains(t, err, "tool-cache-mode")
}
func TestExecuteArgsPaths(t *testing.T) {
workdir := t.TempDir()
args := &executeArgs{