Skip to content

Commit

Permalink
fix: nested includes of uds tasks do not resolve paths as expected (#270
Browse files Browse the repository at this point in the history
)
  • Loading branch information
decleaver authored Dec 14, 2023
1 parent d6ca31f commit 4403470
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 5 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ The packages referenced in `packages` can exist either locally or in an OCI regi
#### Declarative Syntax
The syntax of a `uds-bundle.yaml` is entirely declarative. As a result, the UDS CLI will not prompt users to deploy optional components in a Zarf package. If you want to deploy an optional Zarf component, it must be specified in the `optional-components` key of a particular `package`.

#### Registry Structure
<b>UDS core bundles:</b> `ghcr.io/defenseunicorns/packages/uds/bundles/`

<b>UDS delivery bundles:</b> `ghcr.io/defenseunicorns/packages/delivery/`

While you can specify the full path for commands that accept an OCI registry path, for `deploy`,`inspect`,`remove`, and `pull` commands, if you just specify the artifact name and tag, UDS CLI by default will look in `ghcr.io/defenseunicorns/packages/uds/bundles/` for that artifact. UDS bundles used for delivery should be located in `ghcr.io/defenseunicorns/packages/delivery/`. When referencing a remote delivery bundle artifact in `ghcr.io/defenseunicorns/packages/delivery/`, you can provide the entire path, or you can just provide the artifact name and tag, prefaced by `delivery/`

### Bundle Create
Pulls the Zarf packages from the registry and bundles them into an OCI artifact.

Expand Down
3 changes: 2 additions & 1 deletion docs/runner.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ tasks:

### Includes

The `includes` key is used to import tasks from either local or remote task files. This is useful for sharing common tasks across multiple task files.
The `includes` key is used to import tasks from either local or remote task files. This is useful for sharing common tasks across multiple task files. When importing a task from a local task file, the path is relative to the file you are currently in.

```yaml
includes:
Expand All @@ -258,3 +258,4 @@ tasks:

Note that included task files can also include other task files, with the following restriction:
- If a task file includes a remote task file, the included remote task file cannot include any local task files
Note that the paths for a given `includes` are relative to the file that
9 changes: 5 additions & 4 deletions src/pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func Run(tasksFile types.TasksFile, taskName string, setVariables map[string]str
// only process includes if the task requires them
for _, a := range task.Actions {
if strings.Contains(a.TaskReference, ":") {
err = runner.importTasks(tasksFile.Includes)
err = runner.importTasks(tasksFile.Includes,config.TaskFileLocation)
if err != nil {
return err
}
Expand All @@ -66,10 +66,11 @@ func Run(tasksFile types.TasksFile, taskName string, setVariables map[string]str
return err
}

func (r *Runner) importTasks(includes []map[string]string) error {
func (r *Runner) importTasks(includes []map[string]string, dir string) error {
// iterate through includes, open the file, and unmarshal it into a Task
var includeFilenameKey string
var includeFilename string
dir = filepath.Dir(dir)
for _, include := range includes {
if len(include) > 1 {
return fmt.Errorf("included item %s must have only one key", include)
Expand Down Expand Up @@ -98,7 +99,7 @@ func (r *Runner) importTasks(includes []map[string]string) error {
return fmt.Errorf(lang.ErrDownloading, includeFilename, err.Error())
}
} else {
includePath = filepath.Join(filepath.Dir(config.TaskFileLocation), includeFilename)
includePath = filepath.Join(dir, includeFilename)
}

if err := zarfUtils.ReadYaml(includePath, &tasksFile); err != nil {
Expand Down Expand Up @@ -130,7 +131,7 @@ func (r *Runner) importTasks(includes []map[string]string) error {

// recursively import tasks from included files
if tasksFile.Includes != nil {
if err := r.importTasks(tasksFile.Includes); err != nil {
if err := r.importTasks(tasksFile.Includes,includePath); err != nil {
return err
}
}
Expand Down
18 changes: 18 additions & 0 deletions src/test/e2e/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,22 @@ func TestUseCLI(t *testing.T) {
require.Error(t, err, stdOut, stdErr)
require.Contains(t, stdErr, "task loop detected")
})

t.Run("test includes paths", func(t *testing.T) {
t.Parallel()

// NOTE: Even though the foobar task doesn't use remote include, since it does reference an "include", all the "includes" get processed,
// which means we must set "GIT_REVISION" for the remote include
// get current git revision
gitRev, err := e2e.GetGitRevision()
if err != nil {
return
}
setVar := fmt.Sprintf("GIT_REVISION=%s", gitRev)

stdOut, stdErr, err := e2e.RunTasksWithFile("run", "foobar", "--set", setVar)
require.NoError(t, err, stdOut, stdErr)
require.Contains(t, stdErr, "echo foo")
require.Contains(t, stdErr, "echo bar")
})
}
4 changes: 4 additions & 0 deletions src/test/tasks/more-tasks/bar.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
tasks:
- name: bar
actions:
- cmd: "echo bar"
8 changes: 8 additions & 0 deletions src/test/tasks/more-tasks/foo.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
includes:
- bar: ./bar.yaml

tasks:
- name: foobar
actions:
- cmd: "echo foo"
- task: bar:bar
4 changes: 4 additions & 0 deletions src/test/tasks/tasks.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
includes:
- local: ./tasks-to-import.yaml
- foo: ./more-tasks/foo.yaml
- remote: https://raw.githubusercontent.com/defenseunicorns/uds-cli/${GIT_REVISION}/src/test/tasks/remote-import-tasks.yaml

variables:
Expand Down Expand Up @@ -88,3 +89,6 @@ tasks:
- task: rerunnable-task
- task: rerunnable-task
- task: recursive
- name: foobar
actions:
- task: foo:foobar

0 comments on commit 4403470

Please sign in to comment.