Skip to content

Commit 6de25a5

Browse files
fix: make node tool non volatile (#2372)
* fix: make node tool non volatile Currently downgrading node via setup-node can break later actions * fix it and lookup on startup * fix problems --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent 2ad5ff7 commit 6de25a5

9 files changed

+67
-16
lines changed

pkg/runner/action.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ func runActionImpl(step actionStep, actionDir string, remoteAction *remoteAction
179179
if err := maybeCopyToActionDir(ctx, step, actionDir, actionPath, containerActionDir); err != nil {
180180
return err
181181
}
182-
containerArgs := []string{"node", path.Join(containerActionDir, action.Runs.Main)}
182+
containerArgs := []string{rc.GetNodeToolFullPath(ctx), path.Join(containerActionDir, action.Runs.Main)}
183183
logger.Debugf("executing remote job container: %s", containerArgs)
184184

185185
rc.ApplyExtraPath(ctx, step.getEnv())
@@ -533,7 +533,7 @@ func runPreStep(step actionStep) common.Executor {
533533
return err
534534
}
535535

536-
containerArgs := []string{"node", path.Join(containerActionDir, action.Runs.Pre)}
536+
containerArgs := []string{rc.GetNodeToolFullPath(ctx), path.Join(containerActionDir, action.Runs.Pre)}
537537
logger.Debugf("executing remote job container: %s", containerArgs)
538538

539539
rc.ApplyExtraPath(ctx, step.getEnv())
@@ -627,7 +627,7 @@ func runPostStep(step actionStep) common.Executor {
627627
populateEnvsFromSavedState(step.getEnv(), step, rc)
628628
populateEnvsFromInput(ctx, step.getEnv(), step.getActionModel(), rc)
629629

630-
containerArgs := []string{"node", path.Join(containerActionDir, action.Runs.Post)}
630+
containerArgs := []string{rc.GetNodeToolFullPath(ctx), path.Join(containerActionDir, action.Runs.Post)}
631631
logger.Debugf("executing remote job container: %s", containerArgs)
632632

633633
rc.ApplyExtraPath(ctx, step.getEnv())

pkg/runner/action_composite.go

+11-10
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,17 @@ func newCompositeRunContext(ctx context.Context, parent *RunContext, step action
6464
},
6565
},
6666
},
67-
Config: &configCopy,
68-
StepResults: map[string]*model.StepResult{},
69-
JobContainer: parent.JobContainer,
70-
ActionPath: actionPath,
71-
Env: env,
72-
GlobalEnv: parent.GlobalEnv,
73-
Masks: parent.Masks,
74-
ExtraPath: parent.ExtraPath,
75-
Parent: parent,
76-
EventJSON: parent.EventJSON,
67+
Config: &configCopy,
68+
StepResults: map[string]*model.StepResult{},
69+
JobContainer: parent.JobContainer,
70+
ActionPath: actionPath,
71+
Env: env,
72+
GlobalEnv: parent.GlobalEnv,
73+
Masks: parent.Masks,
74+
ExtraPath: parent.ExtraPath,
75+
Parent: parent,
76+
EventJSON: parent.EventJSON,
77+
nodeToolFullPath: parent.nodeToolFullPath,
7778
}
7879
compositerc.ExprEval = compositerc.NewExpressionEvaluator(ctx)
7980

pkg/runner/action_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ func TestActionRunner(t *testing.T) {
164164
},
165165
},
166166
},
167+
nodeToolFullPath: "node",
167168
},
168169
action: &model.Action{
169170
Inputs: map[string]model.Input{
@@ -208,6 +209,7 @@ func TestActionRunner(t *testing.T) {
208209
"name": "state value",
209210
},
210211
},
212+
nodeToolFullPath: "node",
211213
},
212214
action: &model.Action{
213215
Runs: model.ActionRuns{

pkg/runner/expression.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ func getHashFilesFunction(ctx context.Context, rc *RunContext) func(v []reflect.
196196
Mode: 0o644,
197197
Body: hashfiles,
198198
}).
199-
Then(rc.execJobContainer([]string{"node", path.Join(rc.JobContainer.GetActPath(), name)},
199+
Then(rc.execJobContainer([]string{rc.GetNodeToolFullPath(ctx), path.Join(rc.JobContainer.GetActPath(), name)},
200200
env, "", "")).
201201
Finally(func(context.Context) error {
202202
rc.JobContainer.ReplaceLogWriter(stdout, stderr)

pkg/runner/job_executor.go

+1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ func newJobExecutor(info jobInfo, sf stepFactory, rc *RunContext) common.Executo
116116
})
117117

118118
pipeline := make([]common.Executor, 0)
119+
pipeline = append(pipeline, rc.InitializeNodeTool())
119120
pipeline = append(pipeline, preSteps...)
120121
pipeline = append(pipeline, steps...)
121122

pkg/runner/job_executor_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,8 @@ func TestNewJobExecutor(t *testing.T) {
257257
},
258258
},
259259
},
260-
Config: &Config{},
260+
Config: &Config{},
261+
nodeToolFullPath: "node",
261262
}
262263
rc.ExprEval = rc.NewExpressionEvaluator(ctx)
263264
executorOrder := make([]string, 0)

pkg/runner/run_context.go

+44
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package runner
33
import (
44
"archive/tar"
55
"bufio"
6+
"bytes"
67
"context"
78
"crypto/rand"
89
"crypto/sha256"
@@ -50,6 +51,7 @@ type RunContext struct {
5051
Masks []string
5152
cleanUpJobContainer common.Executor
5253
caller *caller // job calling this RunContext (reusable workflows)
54+
nodeToolFullPath string
5355
}
5456

5557
func (rc *RunContext) AddMask(mask string) {
@@ -432,6 +434,48 @@ func (rc *RunContext) execJobContainer(cmd []string, env map[string]string, user
432434
}
433435
}
434436

437+
func (rc *RunContext) InitializeNodeTool() common.Executor {
438+
return func(ctx context.Context) error {
439+
rc.GetNodeToolFullPath(ctx)
440+
return nil
441+
}
442+
}
443+
444+
func (rc *RunContext) GetNodeToolFullPath(ctx context.Context) string {
445+
if rc.nodeToolFullPath == "" {
446+
timeed, cancel := context.WithTimeout(ctx, time.Minute)
447+
defer cancel()
448+
path := rc.JobContainer.GetPathVariableName()
449+
cenv := map[string]string{}
450+
var cpath string
451+
if err := rc.JobContainer.UpdateFromImageEnv(&cenv)(ctx); err == nil {
452+
if p, ok := cenv[path]; ok {
453+
cpath = p
454+
}
455+
}
456+
if len(cpath) == 0 {
457+
cpath = rc.JobContainer.DefaultPathVariable()
458+
}
459+
cenv[path] = cpath
460+
hout := &bytes.Buffer{}
461+
herr := &bytes.Buffer{}
462+
stdout, stderr := rc.JobContainer.ReplaceLogWriter(hout, herr)
463+
err := rc.execJobContainer([]string{"node", "--no-warnings", "-e", "console.log(process.execPath)"},
464+
cenv, "", "").
465+
Finally(func(context.Context) error {
466+
rc.JobContainer.ReplaceLogWriter(stdout, stderr)
467+
return nil
468+
})(timeed)
469+
rawStr := strings.Trim(hout.String(), "\r\n")
470+
if err == nil && !strings.ContainsAny(rawStr, "\r\n") {
471+
rc.nodeToolFullPath = rawStr
472+
} else {
473+
rc.nodeToolFullPath = "node"
474+
}
475+
}
476+
return rc.nodeToolFullPath
477+
}
478+
435479
func (rc *RunContext) ApplyExtraPath(ctx context.Context, env *map[string]string) {
436480
if rc.ExtraPath != nil && len(rc.ExtraPath) > 0 {
437481
path := rc.JobContainer.GetPathVariableName()

pkg/runner/step_action_local_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,8 @@ func TestStepActionLocalPost(t *testing.T) {
249249
},
250250
},
251251
},
252-
StepResults: tt.initialStepResults,
252+
StepResults: tt.initialStepResults,
253+
nodeToolFullPath: "node",
253254
},
254255
Step: tt.stepModel,
255256
action: tt.actionModel,

pkg/runner/step_action_remote_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,7 @@ func TestStepActionRemotePost(t *testing.T) {
573573
},
574574
StepResults: tt.initialStepResults,
575575
IntraActionState: tt.IntraActionState,
576+
nodeToolFullPath: "node",
576577
},
577578
Step: tt.stepModel,
578579
action: tt.actionModel,

0 commit comments

Comments
 (0)