Skip to content

Commit

Permalink
feat(worker): inject environment variables without CDS_ENV prefix (#4073
Browse files Browse the repository at this point in the history
)

* feat(worker): inject environment variables without CDS_ENV prefix

* Update builtin_test.go
  • Loading branch information
fsamin authored and sguiheux committed Mar 25, 2019
1 parent 2bf5ed4 commit ea97b07
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
18 changes: 18 additions & 0 deletions engine/worker/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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))
Expand Down
3 changes: 3 additions & 0 deletions engine/worker/builtin_script.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
60 changes: 60 additions & 0 deletions engine/worker/builtin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package main

import (
"reflect"
"testing"

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

func Test_cdsEnvVartoENV(t *testing.T) {
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)
}
})
}
}

0 comments on commit ea97b07

Please sign in to comment.