Skip to content

Commit

Permalink
feat(script): allow to pass custom environment variables (#226)
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Bétrancourt <thomas@betrancourt.net>
  • Loading branch information
rclsilver committed Oct 28, 2022
1 parent fac4e9e commit d31b5cb
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 8 deletions.
4 changes: 4 additions & 0 deletions engine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,10 @@ func TestScriptPluginEnvironmentVariables(t *testing.T) {
assert.Equal(t, res.PublicID, environment["UTASK_RESOLUTION_ID"])
assert.NotNil(t, environment["UTASK_STEP_NAME"])
assert.Equal(t, "stepOne", environment["UTASK_STEP_NAME"])
assert.NotNil(t, environment["static_value"])
assert.Equal(t, "foo", environment["static_value"])
assert.NotNil(t, environment["variable_value"])
assert.Equal(t, "bar", environment["variable_value"])
}

func TestBaseBaseConfiguration(t *testing.T) {
Expand Down
9 changes: 8 additions & 1 deletion engine/templates_tests/execScriptWithEnvironment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ name: exec-script-with-environment
description: Executing a shell script that returns environment variables
title_format: "[test] a simple task for script-plugin to verify environment variables"

variables:
- name: foo
value: bar

steps:
stepOne:
description: first step
Expand Down Expand Up @@ -29,4 +33,7 @@ steps:
# In production, `file` will be prefixed by the utask.FScriptsFolder variable ("./scripts" by default)
# You can specify your file's path relative to that location
file_path: "./scripts_tests/env-vars.py"
timeout_seconds: "25"
timeout_seconds: "25"
environment:
static_value: foo
variable_value: '{{ eval `foo` }}'
3 changes: 3 additions & 0 deletions hack/template-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,9 @@
"argv": {
"type": "array"
},
"environment": {
"type": "object"
},
"stdin": {
"type": "string"
},
Expand Down
3 changes: 3 additions & 0 deletions pkg/plugins/builtin/script/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ The step will be considered successful if the script returns exit code 0, otherw
|---|---
| `file_path` | file name under scripts folder
| `argv` | a collection of script argv
| `environment` | a map of environment variables passed to the script
| `timeout` | timeout of the script execution
| `stdin` | inject stdin in your script
| `output_mode` | indicates how to retrieve the output values ; valid values are: `manual-lastline` (default), `disabled`, `manual-delimiters`
Expand Down Expand Up @@ -50,6 +51,8 @@ action:
- "1-10"
- "100"
- "110"
environment:
FOO: '{{eval `foo`}}'
```
## Note
Expand Down
19 changes: 12 additions & 7 deletions pkg/plugins/builtin/script/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,14 @@ type Metadata struct {

// Config is the configuration needed to execute a script
type Config struct {
File string `json:"file_path"`
Argv []string `json:"argv,omitempty"`
Timeout string `json:"timeout,omitempty"`
Stdin string `json:"stdin,omitempty"`
OutputMode string `json:"output_mode"`
OutputManualDelimiters []string `json:"output_manual_delimiters"`
ExitCodesUnrecoverable []string `json:"exit_codes_unrecoverable"`
File string `json:"file_path"`
Argv []string `json:"argv,omitempty"`
Timeout string `json:"timeout,omitempty"`
Stdin string `json:"stdin,omitempty"`
OutputMode string `json:"output_mode"`
OutputManualDelimiters []string `json:"output_manual_delimiters"`
ExitCodesUnrecoverable []string `json:"exit_codes_unrecoverable"`
Environment map[string]interface{} `json:"environment,omitempty"`
}

// ScriptContext is the metadata inherited from the task
Expand Down Expand Up @@ -168,6 +169,10 @@ func exec(stepName string, config interface{}, ctx interface{}) (interface{}, in
cmd.Dir = utask.FScriptsFolder
cmd.Stdin = strings.NewReader(cfg.Stdin)

for k, v := range cfg.Environment {
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", k, v))
}

exitCode := 0
metaError := ""

Expand Down

0 comments on commit d31b5cb

Please sign in to comment.