1
0
Fork 0
mirror of https://code.forgejo.org/forgejo/runner.git synced 2025-09-15 18:57:01 +00:00

fix: allow overriding RUNNER_TOOL_CACHE from the config file (#178)

- rc.getToolCache(ctx) is used to figure out RUNNER_TOOL_CACHE and  returns RUNNER_TOOL_CACHE if it is found in the runner config, e.g.
  ```yaml
  container:
    env:
	  RUNNER_TOOL_CACHE: /srv/toolcache
  ```
- store the value in the new `toolCache` data member for containers,  in the same way it is done for host
- GetRunnerContext for containers return `toolCache` instead of a  hard coded string
- add integration test

Closes forgejo/runner#184

Reviewed-on: https://code.forgejo.org/forgejo/act/pulls/178
Reviewed-by: Michael Kriese <michael.kriese@gmx.de>
Co-authored-by: Earl Warren <contact@earl-warren.org>
Co-committed-by: Earl Warren <contact@earl-warren.org>
This commit is contained in:
Earl Warren 2025-07-13 21:55:02 +00:00 committed by earl-warren
parent d2f668c880
commit 6620cc1d18
9 changed files with 39 additions and 9 deletions

View file

@ -17,6 +17,7 @@ type NewContainerInput struct {
Cmd []string Cmd []string
WorkingDir string WorkingDir string
Env []string Env []string
ToolCache string
Binds []string Binds []string
Mounts map[string]string Mounts map[string]string
Name string Name string

View file

@ -46,6 +46,7 @@ import (
func NewContainer(input *NewContainerInput) ExecutionsEnvironment { func NewContainer(input *NewContainerInput) ExecutionsEnvironment {
cr := new(containerReference) cr := new(containerReference)
cr.input = input cr.input = input
cr.toolCache = input.ToolCache
return cr return cr
} }

View file

@ -10,7 +10,9 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
type LinuxContainerEnvironmentExtensions struct{} type LinuxContainerEnvironmentExtensions struct {
toolCache string
}
// Resolves the equivalent host path inside the container // Resolves the equivalent host path inside the container
// This is required for windows and WSL 2 to translate things like C:\Users\Myproject to /mnt/users/Myproject // This is required for windows and WSL 2 to translate things like C:\Users\Myproject to /mnt/users/Myproject
@ -74,12 +76,12 @@ func (*LinuxContainerEnvironmentExtensions) JoinPathVariable(paths ...string) st
return strings.Join(paths, ":") return strings.Join(paths, ":")
} }
func (*LinuxContainerEnvironmentExtensions) GetRunnerContext(ctx context.Context) map[string]interface{} { func (l *LinuxContainerEnvironmentExtensions) GetRunnerContext(ctx context.Context) map[string]interface{} {
return map[string]interface{}{ return map[string]interface{}{
"os": "Linux", "os": "Linux",
"arch": RunnerArch(ctx), "arch": RunnerArch(ctx),
"temp": "/tmp", "temp": "/tmp",
"tool_cache": "/opt/hostedtoolcache", "tool_cache": l.toolCache,
} }
} }

View file

@ -419,7 +419,7 @@ func newStepContainer(ctx context.Context, step step, image string, cmd []string
envList = append(envList, fmt.Sprintf("%s=%s", k, v)) envList = append(envList, fmt.Sprintf("%s=%s", k, v))
} }
envList = append(envList, fmt.Sprintf("%s=%s", "RUNNER_TOOL_CACHE", "/opt/hostedtoolcache")) envList = append(envList, fmt.Sprintf("%s=%s", "RUNNER_TOOL_CACHE", rc.getToolCache(ctx)))
envList = append(envList, fmt.Sprintf("%s=%s", "RUNNER_OS", "Linux")) envList = append(envList, fmt.Sprintf("%s=%s", "RUNNER_OS", "Linux"))
envList = append(envList, fmt.Sprintf("%s=%s", "RUNNER_ARCH", container.RunnerArch(ctx))) envList = append(envList, fmt.Sprintf("%s=%s", "RUNNER_ARCH", container.RunnerArch(ctx)))
envList = append(envList, fmt.Sprintf("%s=%s", "RUNNER_TEMP", "/tmp")) envList = append(envList, fmt.Sprintf("%s=%s", "RUNNER_TEMP", "/tmp"))
@ -438,6 +438,7 @@ func newStepContainer(ctx context.Context, step step, image string, cmd []string
Password: rc.Config.Secrets["DOCKER_PASSWORD"], Password: rc.Config.Secrets["DOCKER_PASSWORD"],
Name: createSimpleContainerName(rc.jobContainerName(), "STEP-"+stepModel.ID), Name: createSimpleContainerName(rc.jobContainerName(), "STEP-"+stepModel.ID),
Env: envList, Env: envList,
ToolCache: rc.getToolCache(ctx),
Mounts: mounts, Mounts: mounts,
NetworkMode: networkMode, NetworkMode: networkMode,
Binds: binds, Binds: binds,

View file

@ -300,13 +300,12 @@ func (rc *RunContext) startHostEnvironment() common.Executor {
if err := os.MkdirAll(runnerTmp, 0o777); err != nil { if err := os.MkdirAll(runnerTmp, 0o777); err != nil {
return err return err
} }
toolCache := filepath.Join(cacheDir, "tool_cache")
rc.JobContainer = &container.HostEnvironment{ rc.JobContainer = &container.HostEnvironment{
Name: randName, Name: randName,
Root: miscpath, Root: miscpath,
Path: path, Path: path,
TmpDir: runnerTmp, TmpDir: runnerTmp,
ToolCache: toolCache, ToolCache: rc.getToolCache(ctx),
Workdir: rc.Config.Workdir, Workdir: rc.Config.Workdir,
ActPath: actPath, ActPath: actPath,
CleanUp: func() { CleanUp: func() {
@ -419,7 +418,7 @@ func (rc *RunContext) startJobContainer() common.Executor {
envList := make([]string, 0) envList := make([]string, 0)
envList = append(envList, fmt.Sprintf("%s=%s", "RUNNER_TOOL_CACHE", "/opt/hostedtoolcache")) envList = append(envList, fmt.Sprintf("%s=%s", "RUNNER_TOOL_CACHE", rc.getToolCache(ctx)))
envList = append(envList, fmt.Sprintf("%s=%s", "RUNNER_OS", "Linux")) envList = append(envList, fmt.Sprintf("%s=%s", "RUNNER_OS", "Linux"))
envList = append(envList, fmt.Sprintf("%s=%s", "RUNNER_ARCH", container.RunnerArch(ctx))) envList = append(envList, fmt.Sprintf("%s=%s", "RUNNER_ARCH", container.RunnerArch(ctx)))
envList = append(envList, fmt.Sprintf("%s=%s", "RUNNER_TEMP", "/tmp")) envList = append(envList, fmt.Sprintf("%s=%s", "RUNNER_TEMP", "/tmp"))
@ -481,6 +480,7 @@ func (rc *RunContext) startJobContainer() common.Executor {
Password: password, Password: password,
Cmd: interpolatedCmd, Cmd: interpolatedCmd,
Env: envs, Env: envs,
ToolCache: rc.getToolCache(ctx),
Mounts: serviceMounts, Mounts: serviceMounts,
Binds: serviceBinds, Binds: serviceBinds,
Stdout: logWriter, Stdout: logWriter,
@ -542,6 +542,7 @@ func (rc *RunContext) startJobContainer() common.Executor {
Password: password, Password: password,
Name: name, Name: name,
Env: envList, Env: envList,
ToolCache: rc.getToolCache(ctx),
Mounts: mounts, Mounts: mounts,
NetworkMode: networkName, NetworkMode: networkName,
NetworkAliases: []string{rc.Name}, NetworkAliases: []string{rc.Name},
@ -761,6 +762,16 @@ func (rc *RunContext) interpolateOutputs() common.Executor {
} }
} }
func (rc *RunContext) getToolCache(ctx context.Context) string {
if value, ok := rc.Config.Env["RUNNER_TOOL_CACHE"]; ok {
return value
}
if rc.IsHostEnv(ctx) {
return filepath.Join(rc.ActionCacheDir(), "tool_cache")
}
return "/opt/hostedtoolcache"
}
func (rc *RunContext) startContainer() common.Executor { func (rc *RunContext) startContainer() common.Executor {
return func(ctx context.Context) error { return func(ctx context.Context) error {
if rc.IsHostEnv(ctx) { if rc.IsHostEnv(ctx) {

View file

@ -218,7 +218,8 @@ func (j *TestJobFileInfo) runTest(ctx context.Context, t *testing.T, cfg *Config
} }
type TestConfig struct { type TestConfig struct {
LocalRepositories map[string]string `yaml:"local-repositories"` LocalRepositories map[string]string `yaml:"local-repositories,omitempty"`
Env map[string]string `yaml:"env,omitempty"`
} }
func TestRunEvent(t *testing.T) { func TestRunEvent(t *testing.T) {
@ -314,6 +315,7 @@ func TestRunEvent(t *testing.T) {
{workdir, "set-env-step-env-override", "push", "", platforms, secrets}, {workdir, "set-env-step-env-override", "push", "", platforms, secrets},
{workdir, "set-env-new-env-file-per-step", "push", "", platforms, secrets}, {workdir, "set-env-new-env-file-per-step", "push", "", platforms, secrets},
{workdir, "no-panic-on-invalid-composite-action", "push", "jobs failed due to invalid action", platforms, secrets}, {workdir, "no-panic-on-invalid-composite-action", "push", "jobs failed due to invalid action", platforms, secrets},
{workdir, "tool-cache", "push", "", platforms, secrets},
// services // services
{workdir, "services", "push", "", platforms, secrets}, {workdir, "services", "push", "", platforms, secrets},
@ -349,6 +351,7 @@ func TestRunEvent(t *testing.T) {
} }
} }
} }
config.Env = testConfig.Env
} }
table.runTest(ctx, t, config) table.runTest(ctx, t, config)

View file

@ -107,7 +107,7 @@ func (sd *stepDocker) newStepContainer(ctx context.Context, image string, cmd []
envList = append(envList, fmt.Sprintf("%s=%s", k, v)) envList = append(envList, fmt.Sprintf("%s=%s", k, v))
} }
envList = append(envList, fmt.Sprintf("%s=%s", "RUNNER_TOOL_CACHE", "/opt/hostedtoolcache")) envList = append(envList, fmt.Sprintf("%s=%s", "RUNNER_TOOL_CACHE", rc.getToolCache(ctx)))
envList = append(envList, fmt.Sprintf("%s=%s", "RUNNER_OS", "Linux")) envList = append(envList, fmt.Sprintf("%s=%s", "RUNNER_OS", "Linux"))
envList = append(envList, fmt.Sprintf("%s=%s", "RUNNER_ARCH", container.RunnerArch(ctx))) envList = append(envList, fmt.Sprintf("%s=%s", "RUNNER_ARCH", container.RunnerArch(ctx)))
envList = append(envList, fmt.Sprintf("%s=%s", "RUNNER_TEMP", "/tmp")) envList = append(envList, fmt.Sprintf("%s=%s", "RUNNER_TEMP", "/tmp"))
@ -122,6 +122,7 @@ func (sd *stepDocker) newStepContainer(ctx context.Context, image string, cmd []
Password: rc.Config.Secrets["DOCKER_PASSWORD"], Password: rc.Config.Secrets["DOCKER_PASSWORD"],
Name: createSimpleContainerName(rc.jobContainerName(), "STEP-"+step.ID), Name: createSimpleContainerName(rc.jobContainerName(), "STEP-"+step.ID),
Env: envList, Env: envList,
ToolCache: rc.getToolCache(ctx),
Mounts: mounts, Mounts: mounts,
NetworkMode: fmt.Sprintf("container:%s", rc.jobContainerName()), NetworkMode: fmt.Sprintf("container:%s", rc.jobContainerName()),
Binds: binds, Binds: binds,

View file

@ -0,0 +1,2 @@
env:
RUNNER_TOOL_CACHE: /srv/otherlocaltion

View file

@ -0,0 +1,8 @@
name: basic
on: push
jobs:
build:
runs-on: ubuntu-latest
steps:
- run: test "$RUNNER_TOOL_CACHE" = "/srv/otherlocaltion"