Skip to content

Commit

Permalink
feat(worker): multiline job variable to one line environment variable (
Browse files Browse the repository at this point in the history
  • Loading branch information
richardlt authored Jan 28, 2022
1 parent c7080c4 commit 59f84be
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
8 changes: 4 additions & 4 deletions engine/worker/internal/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ func (wk *CurrentWorker) Environ() []string {
if v == "" {
continue
}
newEnv = append(newEnv, k+"="+v)
newEnv = append(newEnv, k+"="+sdk.OneLineValue(v))
}

//set up environment variables from pipeline build job parameters
Expand All @@ -313,19 +313,19 @@ func (wk *CurrentWorker) Environ() []string {
envName := strings.Replace(p.Name, ".", "_", -1)
envName = strings.Replace(envName, "-", "_", -1)
envName = strings.ToUpper(envName)
newEnv = append(newEnv, fmt.Sprintf("%s=%s", envName, p.Value))
newEnv = append(newEnv, fmt.Sprintf("%s=%s", envName, sdk.OneLineValue(p.Value)))
}

for _, p := range wk.currentJob.newVariables {
envName := strings.Replace(p.Name, ".", "_", -1)
envName = strings.Replace(envName, "-", "_", -1)
envName = strings.ToUpper(envName)
newEnv = append(newEnv, fmt.Sprintf("%s=%s", envName, p.Value))
newEnv = append(newEnv, fmt.Sprintf("%s=%s", envName, sdk.OneLineValue(p.Value)))
}

//Set env variables from hooks
for k, v := range wk.currentJob.envFromHooks {
newEnv = append(newEnv, k+"="+v)
newEnv = append(newEnv, k+"="+sdk.OneLineValue(v))
}
return newEnv
}
Expand Down
2 changes: 2 additions & 0 deletions sdk/blur_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func TestBlur(t *testing.T) {
`"1234567890`,
"12345",
"123456",
"123\n456",
})
require.NoError(t, err)

Expand All @@ -28,6 +29,7 @@ func TestBlur(t *testing.T) {
require.Equal(t, sdk.PasswordPlaceholder, b.String(`&é'"'"'(§è!çà`))
require.Equal(t, sdk.PasswordPlaceholder, b.String(url.QueryEscape("&é'(§è!çà")))
require.Equal(t, sdk.PasswordPlaceholder, b.String(base64.StdEncoding.EncodeToString([]byte("&é'(§è!çà"))))
require.Equal(t, sdk.PasswordPlaceholder, b.String("123\\n456"))

buf, err := json.Marshal(`"1234567890`)
require.NoError(t, err)
Expand Down
17 changes: 13 additions & 4 deletions sdk/variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,20 @@ func EnvVartoENV(p Parameter) []string {

pName := strings.TrimPrefix(p.Name, "cds.env.")

oneLineValue := OneLineValue(p.Value)

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
env = append(env, fmt.Sprintf("CDS_ENV_%s=%s", strings.ToUpper(envName), oneLineValue)) // CDS_ENV_MYSTRINGVARIABLE
env = append(env, fmt.Sprintf("CDS_ENV_%s=%s", pName, oneLineValue)) //CDS_ENV_MyStringVariable
env = append(env, fmt.Sprintf("%s=%s", pName, oneLineValue)) // MyStringVariable
env = append(env, fmt.Sprintf("%s=%s", strings.ToUpper(envName), oneLineValue)) // MYSTRINGVARIABLE
return env
}

func OneLineValue(v string) string {
if strings.Contains(v, "\n") {
return strings.Replace(v, "\n", "\\n", -1)
}
return v
}
12 changes: 12 additions & 0 deletions sdk/variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ func Test_EnvVartoENV(t *testing.T) {
"MY_STRING_VARIABLE=value",
},
},
{
args: sdk.Parameter{
Name: "cds.env.MyTextVariable",
Value: "one=one\ntwo=two\nthree=three",
},
want: []string{
"CDS_ENV_MYTEXTVARIABLE=one=one\\ntwo=two\\nthree=three",
"CDS_ENV_MyTextVariable=one=one\\ntwo=two\\nthree=three",
"MyTextVariable=one=one\\ntwo=two\\nthree=three",
"MYTEXTVARIABLE=one=one\\ntwo=two\\nthree=three",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 59f84be

Please sign in to comment.