Skip to content

Commit

Permalink
Allow global runs_on
Browse files Browse the repository at this point in the history
  • Loading branch information
luismedel committed Sep 21, 2024
1 parent c17117f commit b0dc8bf
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .bluish/bluish.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

var:
project_version: "0.5.7"
project_version: "0.6.0"
python_version: "3.12"

jobs:
Expand Down
17 changes: 15 additions & 2 deletions src/bluish/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,16 @@ def __init__(self, definition: dict[str, Any]) -> None:
self.jobs = {k: JobContext(self, k, v) for k, v in self.attrs.jobs.items()}
self.var = dict(self.attrs.var)

self.runs_on_host: dict[str, Any] | None = None

def dispatch(self) -> process.ProcessResult:
self.status = ExecutionStatus.RUNNING

if self.attrs.runs_on:
self.runs_on_host = process.prepare_host(
self.expand_expr(self.attrs.runs_on)
)

try:
for job in self.jobs.values():
result = self.dispatch_job(job, no_deps=False)
Expand All @@ -187,7 +194,10 @@ def dispatch(self) -> process.ProcessResult:
return self.result

finally:
self.status = ExecutionStatus.FINISHED
if self.status == ExecutionStatus.RUNNING:
self.status = ExecutionStatus.FINISHED
process.cleanup_host(self.runs_on_host)
self.runs_on_host = None

def dispatch_job(
self, job: "JobContext", no_deps: bool
Expand Down Expand Up @@ -266,7 +276,7 @@ def __init__(

self.id = step_id

self.runs_on_host: dict[str, Any] = {}
self.runs_on_host: dict[str, Any] | None = None

self.attrs.ensure_property("steps", [])
self.attrs.ensure_property("continue_on_error", False)
Expand Down Expand Up @@ -306,6 +316,8 @@ def dispatch(self) -> process.ProcessResult | None:
self.runs_on_host = process.prepare_host(
self.expand_expr(self.attrs.runs_on)
)
else:
self.runs_on_host = self.workflow.runs_on_host

try:
if self.matrix:
Expand Down Expand Up @@ -333,6 +345,7 @@ def dispatch(self) -> process.ProcessResult | None:
if self.status == ExecutionStatus.RUNNING:
self.status = ExecutionStatus.FINISHED
process.cleanup_host(self.runs_on_host)
self.runs_on_host = None

return self.result

Expand Down
7 changes: 5 additions & 2 deletions src/bluish/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ def prepare_host(opts: str | dict[str, Any] | None) -> dict[str, Any]:
def cleanup_host(host_opts: dict[str, Any] | None) -> None:
"""Stops and removes a container if it was started by the process module."""

if not host_opts:
return

host = host_opts.get("host", None) if isinstance(host_opts, dict) else host_opts
if not host:
return
Expand Down Expand Up @@ -191,7 +194,7 @@ def run(
return result


def get_flavor(host_opts: dict[str, Any]) -> str:
def get_flavor(host_opts: dict[str, Any] | None) -> str:
ids = {}
for line in run("cat /etc/os-release | grep ^ID", host_opts).stdout.splitlines():
key, value = line.split("=", maxsplit=1)
Expand All @@ -200,7 +203,7 @@ def get_flavor(host_opts: dict[str, Any]) -> str:


def install_package(
host_opts: dict[str, Any], packages: list[str], flavor: str = "auto"
host_opts: dict[str, Any] | None, packages: list[str], flavor: str = "auto"
) -> ProcessResult:
"""Installs a package on a host."""

Expand Down
45 changes: 45 additions & 0 deletions test/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,51 @@ def test_pass_env() -> None:
assert wf.jobs["test_job"].result.stdout == "Hello, World!"


def test_runs_on_job() -> None:
wf = create_workflow("""
jobs:
test_job:
runs_on: docker://alpine:3.20.3
steps:
- run: |
cat /etc/os-release
""")
_ = wf.dispatch()

assert "3.20.3" in wf.jobs["test_job"].result.stdout


def test_runs_on_workflow() -> None:
wf = create_workflow("""
runs_on: docker://alpine:3.20.3
jobs:
test_job:
steps:
- run: |
cat /etc/os-release
""")
_ = wf.dispatch()

assert "3.20.3" in wf.jobs["test_job"].result.stdout


def test_runs_on_workflow_override_in_job() -> None:
wf = create_workflow("""
runs_on: docker://alpine:3.20.3
jobs:
test_job:
runs_on: docker://ubuntu:22.04
steps:
- run: |
cat /etc/os-release
""")
_ = wf.dispatch()

assert "22.04" in wf.jobs["test_job"].result.stdout


@pytest.mark.docker
def test_docker_pass_env() -> None:
wf = create_workflow("""
Expand Down

0 comments on commit b0dc8bf

Please sign in to comment.