Skip to content

Commit

Permalink
feat: allow templating in env vars (#114)
Browse files Browse the repository at this point in the history
## Description

This allows templating of env vars in actions.

## Related Issue

Fixes #111 

## Type of change

- [ ] Bug fix (non-breaking change which fixes an issue)
- [X] New feature (non-breaking change which adds functionality)
- [ ] Other (security config, docs update, etc)

## Checklist before merging

- [X] Test, docs, adr added or updated as needed
- [X] [Contributor Guide
Steps](https://github.com/defenseunicorns/maru-runner/blob/main/CONTRIBUTING.md)
followed
  • Loading branch information
Racer159 committed Jun 6, 2024
1 parent b41c3de commit fe71cd0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/pkg/runner/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ func RunAction[T any](action *types.BaseAction[T], envFilePath string, variableC
// Template dir string
cfg.Dir = utils.TemplateString(variableConfig.GetSetVariables(), cfg.Dir)

// Template env strings
for idx := range cfg.Env {
cfg.Env[idx] = utils.TemplateString(variableConfig.GetSetVariables(), cfg.Env[idx])
}

duration := time.Duration(cfg.MaxTotalSeconds) * time.Second
timeout := time.After(duration)

Expand Down
7 changes: 7 additions & 0 deletions src/test/e2e/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,13 @@ func TestTaskRunner(t *testing.T) {
require.Contains(t, stdErr, "SECRET_KEY=not-a-secret")
})

t.Run("test that setting an env var from a variable is processed correctly", func(t *testing.T) {
t.Parallel()
stdOut, stdErr, err := e2e.Maru("run", "env-templating", "--file", "src/test/tasks/tasks.yaml")
require.NoError(t, err, stdOut, stdErr)
require.Contains(t, stdErr, "hello-replaced")
})

t.Run("test that env vars get used for variables that do not have a default set", func(t *testing.T) {
t.Parallel()
os.Setenv("MARU_LOG_LEVEL", "debug")
Expand Down
6 changes: 6 additions & 0 deletions src/test/tasks/tasks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,9 @@ tasks:
actions:
- cmd: cat ${COOL_FILE}
dir: ${COOL_DIR}
- name: env-templating
description: Tests setting an env var from variable
actions:
- cmd: echo ${HELLO_KITTEH}
env:
- HELLO_KITTEH=hello-${REPLACE_ME}

0 comments on commit fe71cd0

Please sign in to comment.