Skip to content

Commit

Permalink
fix(worker): use interpolate for action parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
fsamin committed Apr 6, 2020
1 parent e8c1b2a commit 57f4509
Showing 1 changed file with 32 additions and 33 deletions.
65 changes: 32 additions & 33 deletions engine/worker/internal/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,40 @@ import (
"github.com/spf13/afero"

"github.com/ovh/cds/engine/worker/pkg/workerruntime"

"github.com/ovh/cds/sdk"
"github.com/ovh/cds/sdk/interpolate"
"github.com/ovh/cds/sdk/log"
)

func processJobParameter(parameters []sdk.Parameter, secrets []sdk.Variable) {
for i := range parameters {
keepReplacing := true
for keepReplacing {
t := parameters[i].Value
func processJobParameter(parameters []sdk.Parameter, secrets []sdk.Variable) error {
secretParam := sdk.VariablesToParameters("", secrets)
secretMap := sdk.ParametersToMap(secretParam)

for _, p := range parameters {
parameters[i].Value = strings.Replace(parameters[i].Value, "{{."+p.Name+"}}", p.Value, -1)
for i := range parameters {
var err error
var oldValue = parameters[i].Value
var i int
var keepReplacing = true
for keepReplacing && i < 10 {
var paramMap = sdk.ParametersToMap(parameters)
parameters[i].Value, err = interpolate.Do(parameters[i].Value, paramMap)
if err != nil {
return sdk.WrapError(err, "Unable to interpolate job parameters")
}

for _, p := range secrets {
parameters[i].Value = strings.Replace(parameters[i].Value, "{{."+p.Name+"}}", p.Value, -1)
parameters[i].Value, err = interpolate.Do(parameters[i].Value, secretMap)
if err != nil {
return sdk.WrapError(err, "Unable to interpolate job parameters")
}

// If parameters wasn't updated, consider it done
if parameters[i].Value == t {
if parameters[i].Value == oldValue {
keepReplacing = false
}
i++
}
}
return nil
}

// ProcessActionVariables replaces all placeholders inside action recursively using
Expand All @@ -51,29 +59,20 @@ func processJobParameter(parameters []sdk.Parameter, secrets []sdk.Variable) {
func (w *CurrentWorker) processActionVariables(a *sdk.Action, parent *sdk.Action, jobParameters []sdk.Parameter, secrets []sdk.Variable) error {
// replaces placeholder in parameters with ActionBuild variables
// replaces placeholder in parameters with Parent params
secretParam := sdk.VariablesToParameters("", secrets)
secretMap := sdk.ParametersToMap(secretParam)
var parentParamMap = map[string]string{}
if parent != nil {
parentParamMap = sdk.ParametersToMap(parent.Parameters)
}
jobParamMap := sdk.ParametersToMap(jobParameters)
allParams := sdk.ParametersMapMerge(parentParamMap, jobParamMap)
allParams = sdk.ParametersMapMerge(allParams, secretMap)
for i := range a.Parameters {
keepReplacing := true
for keepReplacing {
t := a.Parameters[i].Value

if parent != nil {
for _, p := range parent.Parameters {
a.Parameters[i].Value = strings.Replace(a.Parameters[i].Value, "{{."+p.Name+"}}", p.Value, -1)
}
}

for _, p := range jobParameters {
a.Parameters[i].Value = strings.Replace(a.Parameters[i].Value, "{{."+p.Name+"}}", p.Value, -1)
}

for _, p := range secrets {
a.Parameters[i].Value = strings.Replace(a.Parameters[i].Value, "{{."+p.Name+"}}", p.Value, -1)
}

// If parameters wasn't updated, consider it done
if a.Parameters[i].Value == t {
keepReplacing = false
}
var err error
a.Parameters[i].Value, err = interpolate.Do(a.Parameters[i].Value, allParams)
if err != nil {
return err
}
}

Expand Down

0 comments on commit 57f4509

Please sign in to comment.