From 2da936121c2c423312296312ad4374fb96010ea7 Mon Sep 17 00:00:00 2001 From: Guiheux Steven Date: Thu, 25 Nov 2021 09:44:41 +0100 Subject: [PATCH] fix(worker): base dir + env variable (#6024) --- engine/worker/internal/run.go | 10 ++++++---- engine/worker/internal/types.go | 11 ++++++++++- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/engine/worker/internal/run.go b/engine/worker/internal/run.go index 2c031b31f2..20015e2e10 100644 --- a/engine/worker/internal/run.go +++ b/engine/worker/internal/run.go @@ -631,10 +631,6 @@ func (w *CurrentWorker) ProcessJob(jobInfo sdk.WorkflowNodeJobRunData) (res sdk. if err := w.setupHooks(ctx, jobInfo, w.basedir, hdFile.Name()); err != nil { return sdk.Result{Status: sdk.StatusFail, Reason: fmt.Sprintf("Error: unable to setup hooks: %v", err)} } - log.Info(ctx, "Executing hooks setup from directory: %s", hdFile.Name()) - if err := w.executeHooksSetup(ctx, w.basedir, hdFile.Name()); err != nil { - return sdk.Result{Status: sdk.StatusFail, Reason: fmt.Sprintf("Error: unable to setup hooks: %v", err)} - } w.currentJob.context = ctx w.currentJob.params = jobInfo.NodeJobRun.Parameters @@ -665,6 +661,11 @@ func (w *CurrentWorker) ProcessJob(jobInfo sdk.WorkflowNodeJobRunData) (res sdk. return sdk.Result{Status: sdk.StatusFail, Reason: fmt.Sprintf("unable to process job %s: %v", jobInfo.NodeJobRun.Job.Action.Name, err)} } + log.Info(ctx, "Executing hooks setup from directory: %s", hdFile.Name()) + if err := w.executeHooksSetup(ctx, w.basedir, hdFile.Name()); err != nil { + return sdk.Result{Status: sdk.StatusFail, Reason: fmt.Sprintf("Error: unable to setup hooks: %v", err)} + } + res = w.runJob(ctx, &jobInfo.NodeJobRun.Job.Action, jobInfo.NodeJobRun.ID, jobInfo.Secrets) if len(res.NewVariables) > 0 { @@ -797,6 +798,7 @@ func (w *CurrentWorker) executeHooksSetup(ctx context.Context, basedir afero.Fs, str := fmt.Sprintf("source %s ; echo '<<>>' ; env", filepath) cmd := exec.Command("bash", "-c", str) + cmd.Env = w.Environ() bs, err := cmd.CombinedOutput() if err != nil { return errors.WithStack(err) diff --git a/engine/worker/internal/types.go b/engine/worker/internal/types.go index eed2695296..62cd619f1f 100644 --- a/engine/worker/internal/types.go +++ b/engine/worker/internal/types.go @@ -2,6 +2,7 @@ package internal import ( "context" + "crypto/md5" "encoding/json" "fmt" "os" @@ -272,7 +273,15 @@ func (wk *CurrentWorker) Environ() []string { newEnv = append(newEnv, "CDS_KEY=********") //We have to let it here for some legacy reason newEnv = append(newEnv, fmt.Sprintf("%s=%d", WorkerServerPort, wk.HTTPPort())) newEnv = append(newEnv, fmt.Sprintf("%s=%s", CDSApiUrl, wk.cfg.APIEndpoint)) - newEnv = append(newEnv, "BASEDIR="+wk.cfg.Basedir) + + if wk.currentJob.wJob != nil { + data := []byte(wk.currentJob.wJob.Job.Job.Action.Name) + suffix := fmt.Sprintf("%x", md5.Sum(data)) + newEnv = append(newEnv, "BASEDIR="+wk.cfg.Basedir+"/"+suffix) + } else { + newEnv = append(newEnv, "BASEDIR="+wk.cfg.Basedir) + } + newEnv = append(newEnv, "HATCHERY_NAME="+wk.cfg.HatcheryName) newEnv = append(newEnv, "HATCHERY_WORKER="+wk.cfg.Name) if wk.cfg.Region != "" {