Skip to content

Commit

Permalink
Add 'dir' as a valid step attribute (#853)
Browse files Browse the repository at this point in the history
Add 'dir' attribute for steps, as well as tests for it. Corresponding tests added as well.

fixes #847
  • Loading branch information
Ricardo-HE committed Jun 11, 2020
1 parent 715f59a commit 60c7701
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 8 deletions.
1 change: 1 addition & 0 deletions docs/sections/cli_features.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ steps:
- id: mystep
uses: docker://ubuntu:18.04
runs: ["ls", "-l"]
dir: /tmp/
env:
MYENVVAR: "foo"
```
Expand Down
24 changes: 22 additions & 2 deletions docs/sections/cn_workflows.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ step. All attributes are optional with the exception of the `uses` attribute.
| `env` | **optional** The environment variables to set inside the container's runtime environment. If<br>you need to pass environment variables into a step, make sure it runs a command<br>shell to perform variable substitution. For example, if your `runs` attribute is<br>set to `["sh", "-c"]`, the value of `args` will be passed to `sh -c` and<br>executed in a command shell. Alternatively, if your `Dockerfile` uses an<br>`ENTRYPOINT` to run the same command (`"sh -c"`), `args` will execute in a<br>command shell as well. See [`ENTRYPOINT`](https://docs.docker.com/engine/reference/builder/#entrypoint) for more details. |
| `secrets` | **optional** Specifies the names of the secret variables to set in the runtime environment<br>which the container can access as an environment variable. For example,<br>`secrets: ["SECRET1", "SECRET2"]`. |
| `skip_pull` | **optional** Assume that the given container image already exist and skip pulling it. |

| `dir` | **optional** Specifies the working directory for a step. By default, the directory is always `/workspace` if another one is not defined. |
### Referencing images in a step

A step in a workflow can reference a container image defined in a
Expand Down Expand Up @@ -211,7 +211,27 @@ popper run -f /home/user/proj/wf.yml -w /home/user/proj/

The above writes the `/home/user/proj/myfile` even though Popper is
being invoked from `/tmp`, since the `-w` flag is being passed to
`ppopper run`.
`popper run`.

### Changing the working directory

To specify a working directory for a step you can use the `dir` attribute
in the workflow. This going to change where the specified
command is executed.

For example, adding `dir` to a workflow results in the following:

```yaml
version: '1'
steps:
- uses: docker://alpine:3.9
args: [touch, ./myfile]
dir: /path/to/dir/
```

It is worth mentioning that if the directory is specified outside the
`/workspace` folder, then anything that gets written to it won't persist
(see below for more).

### Filesystem namespaces and persistence

Expand Down
1 change: 1 addition & 0 deletions src/popper/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class WorkflowParser(object):
"args": {"type": "seq", "sequence": [{"type": "str"}]},
"runs": {"type": "seq", "sequence": [{"type": "str"}]},
"secrets": {"type": "seq", "sequence": [{"type": "str"}]},
"dir": {"type": "str"},
"skip_pull": {"type": "bool"},
"env": {
"type": "map",
Expand Down
2 changes: 1 addition & 1 deletion src/popper/runner_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def _get_container_kwargs(self, step, img, name):
f"{self._config.workspace_dir}:/workspace",
"/var/run/docker.sock:/var/run/docker.sock",
],
"working_dir": "/workspace",
"working_dir": step.dir if step.dir else "/workspace",
"environment": self._prepare_environment(step),
"entrypoint": step.runs if step.runs else None,
"detach": not self._config.pty,
Expand Down
11 changes: 9 additions & 2 deletions src/test/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ def test_new_workflow(self):

wf_data = {
"steps": [
{"uses": "foo", "id": "step", "env": {"EN": "EE"}, "secrets": ["S"]},
{
"uses": "foo",
"id": "step",
"env": {"EN": "EE"},
"secrets": ["S"],
"dir": "/path/to/",
},
{"uses": "bar", "runs": ["a", "b"], "args": ["c"], "skip_pull": True},
],
"options": {"env": {"FOO": "bar"}, "secrets": ["Z"]},
Expand All @@ -43,6 +49,7 @@ def test_new_workflow(self):
self.assertEqual("foo", step.uses)
self.assertEqual(("Z", "S"), step.secrets)
self.assertEqual({"EN": "EE", "FOO": "bar"}, step.env)
self.assertEqual("/path/to/", step.dir)
self.assertTrue(not step.runs)
self.assertTrue(not step.args)
self.assertFalse(step.skip_pull)
Expand All @@ -52,9 +59,9 @@ def test_new_workflow(self):
self.assertEqual(("a", "b"), step.runs)
self.assertEqual(("c",), step.args)
self.assertTrue(step.skip_pull)
self.assertTrue(not step.dir)
self.assertEqual({"FOO": "bar"}, step.env)
self.assertEqual(("Z",), step.secrets)

self.assertEqual({"FOO": "bar"}, wf.options.env)
self.assertEqual(("Z",), wf.options.secrets)

Expand Down
11 changes: 8 additions & 3 deletions src/test/test_runner_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,12 @@ def test_stop_running_tasks(self):
@unittest.skipIf(os.environ.get("ENGINE", "docker") != "docker", "ENGINE != docker")
def test_get_container_kwargs(self):
step = Box(
{"uses": "popperized/bin/sh@master", "args": ["ls"], "id": "one",},
{
"uses": "popperized/bin/sh@master",
"args": ["ls"],
"id": "one",
"dir": "/tmp/",
},
default_box=True,
)

Expand Down Expand Up @@ -176,7 +181,7 @@ def test_get_container_kwargs(self):
"/var/run/docker.sock:/var/run/docker.sock",
"/path/in/host:/path/in/container",
],
"working_dir": "/workspace",
"working_dir": "/tmp/",
"environment": {"FOO": "bar"},
"entrypoint": None,
"detach": True,
Expand Down Expand Up @@ -207,7 +212,7 @@ def test_get_container_kwargs(self):
"/var/run/docker.sock:/var/run/docker.sock",
"/path/in/host:/path/in/container",
],
"working_dir": "/workspace",
"working_dir": "/tmp/",
"environment": {"FOO": "bar"},
"entrypoint": None,
"detach": False,
Expand Down

0 comments on commit 60c7701

Please sign in to comment.