diff --git a/engine/worker/builtin.go b/engine/worker/builtin.go index ccd3bad1af..ee99040b80 100644 --- a/engine/worker/builtin.go +++ b/engine/worker/builtin.go @@ -67,6 +67,23 @@ func (w *currentWorker) runBuiltin(ctx context.Context, a *sdk.Action, buildID i return f(w)(ctx, a, buildID, params, secrets, sendLog) } +func cdsEnvVartoENV(p sdk.Parameter) []string { + var env []string + if !strings.HasPrefix(p.Name, "cds.env.") { + return nil + } + + pName := strings.TrimPrefix(p.Name, "cds.env.") + + envName := strings.Replace(pName, ".", "_", -1) + envName = strings.Replace(envName, "-", "_", -1) + env = append(env, fmt.Sprintf("CDS_ENV_%s=%s", strings.ToUpper(envName), p.Value)) // CDS_ENV_MYSTRINGVARIABLE + env = append(env, fmt.Sprintf("CDS_ENV_%s=%s", pName, p.Value)) //CDS_ENV_MyStringVariable + env = append(env, fmt.Sprintf("%s=%s", pName, p.Value)) // MyStringVariable + env = append(env, fmt.Sprintf("%s=%s", strings.ToUpper(envName), p.Value)) // MYSTRINGVARIABLE + return env +} + func (w *currentWorker) runGRPCPlugin(ctx context.Context, a *sdk.Action, buildID int64, params *[]sdk.Parameter, stepOrder int, sendLog LoggerFunc) sdk.Result { log.Debug("runGRPCPlugin> Begin buildID:%d stepOrder:%d", buildID, stepOrder) defer func() { @@ -90,6 +107,7 @@ func (w *currentWorker) runGRPCPlugin(ctx context.Context, a *sdk.Action, buildI if p.Type == sdk.KeyParameter && !strings.HasSuffix(p.Name, ".pub") { continue } + envs = append(envs, cdsEnvVartoENV(p)...) envName := strings.Replace(p.Name, ".", "_", -1) envName = strings.ToUpper(envName) envs = append(envs, fmt.Sprintf("%s=%s", envName, p.Value)) diff --git a/engine/worker/builtin_script.go b/engine/worker/builtin_script.go index 75384afc68..0ca8dffb7a 100644 --- a/engine/worker/builtin_script.go +++ b/engine/worker/builtin_script.go @@ -155,6 +155,9 @@ func runScriptAction(w *currentWorker) BuiltInAction { if p.Type == sdk.KeyParameter && !strings.HasSuffix(p.Name, ".pub") { continue } + + cmd.Env = append(cmd.Env, cdsEnvVartoENV(p)...) + envName := strings.Replace(p.Name, ".", "_", -1) envName = strings.Replace(envName, "-", "_", -1) envName = strings.ToUpper(envName) diff --git a/engine/worker/builtin_test.go b/engine/worker/builtin_test.go new file mode 100644 index 0000000000..9dd820e6dd --- /dev/null +++ b/engine/worker/builtin_test.go @@ -0,0 +1,63 @@ +package main + +import ( + "reflect" + "testing" + + "github.com/ovh/cds/sdk" +) + +func Test_cdsEnvVartoENV(t *testing.T) { + type args struct { + p sdk.Parameter + } + tests := []struct { + name string + args sdk.Parameter + want []string + }{ + { + args: sdk.Parameter{ + Name: "cds.env.MyStringVariable", + Value: "value", + }, + want: []string{ + "CDS_ENV_MYSTRINGVARIABLE=value", + "CDS_ENV_MyStringVariable=value", + "MyStringVariable=value", + "MYSTRINGVARIABLE=value", + }, + }, + { + args: sdk.Parameter{ + Name: "cds.env.My.String.Variable", + Value: "value", + }, + want: []string{ + "CDS_ENV_MY_STRING_VARIABLE=value", + "CDS_ENV_My.String.Variable=value", + "My.String.Variable=value", + "MY_STRING_VARIABLE=value", + }, + }, + { + args: sdk.Parameter{ + Name: "cds.env.My-String-Variable", + Value: "value", + }, + want: []string{ + "CDS_ENV_MY_STRING_VARIABLE=value", + "CDS_ENV_My-String-Variable=value", + "My-String-Variable=value", + "MY_STRING_VARIABLE=value", + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := cdsEnvVartoENV(tt.args); !reflect.DeepEqual(got, tt.want) { + t.Errorf("cdsEnvVartoENV() = %v, want %v", got, tt.want) + } + }) + } +}