ci(molecule): skip the Molecule runner when no scenario is selected #503
No files matched your search
@@ -79,10 +79,11 @@ issue.
|
||||
pin pull request. *Held:* review.
|
||||
- **Molecule has three jobs: `select`, `scenarios` and `result`.** `select` runs the selector in
|
||||
a job container on `kamino-docker`, in the `molecule-controller` pin (never the `ci` pin, whose
|
||||
bumps would then select every scenario), and outputs the selected directories. `scenarios`
|
||||
runs them on `kamino-molecule` and is skipped when the list is empty. `result` (check
|
||||
`Molecule / Result (pull_request)`) is the required check: it runs `if: always()` and fails
|
||||
unless `select` passed and `scenarios` passed or was skipped for an empty list. The
|
||||
bumps would then select every scenario), and outputs the selected directories, or `none`.
|
||||
`scenarios` runs them on `kamino-molecule` and is skipped for `none` or an empty output.
|
||||
`result` (check `Molecule / Result (pull_request)`) is the required check: it runs
|
||||
`if: always()` and fails unless `select` passed and `scenarios` passed or was skipped for
|
||||
`none`, so a lost output fails it. The
|
||||
controller pin is written in all three jobs, because `container.image` cannot read `env`.
|
||||
*Why:* a pull request that selects nothing takes no Molecule runner slot, and a skipped job
|
||||
counts as passing in Gitea, so only a job that always runs can be required. *Held:* control:
|
||||
|
||||
@@ -110,19 +110,18 @@ jobs:
|
||||
echo "::notice::no base copy of $sel; running this checkout's copy"
|
||||
fi
|
||||
selected="$(python3 -I "$sel" "$BASE_REF")" || exit 1
|
||||
# One directory per line. A random delimiter, so no line of the list can end the value;
|
||||
# an empty selection writes no line, so the output is the empty string.
|
||||
# `none` marks an empty selection, so a lost or empty output fails result, never passes.
|
||||
if [ -z "$selected" ]; then selected=none; fi
|
||||
# One directory per line; a random delimiter, so no line of the list can end the value.
|
||||
delim="scenarios-$(od -An -N16 -tx1 /dev/urandom | tr -d ' \n')"
|
||||
{
|
||||
echo "scenarios<<$delim"
|
||||
if [ -n "$selected" ]; then printf '%s\n' "$selected"; fi
|
||||
echo "$delim"
|
||||
} >> "$GITHUB_OUTPUT"
|
||||
printf 'scenarios<<%s\n%s\n%s\n' "$delim" "$selected" "$delim" >> "$GITHUB_OUTPUT"
|
||||
|
||||
scenarios:
|
||||
needs: select
|
||||
# Skipped, not failed, when select fails or selects nothing; result tells the two apart.
|
||||
# Skipped, not failed, when select fails, selects nothing (`none`) or loses its output (empty);
|
||||
# result passes only the second.
|
||||
if: >-
|
||||
needs.select.outputs.scenarios != 'none' &&
|
||||
needs.select.outputs.scenarios != '' &&
|
||||
github.repository == 'mortis/kamino' &&
|
||||
(github.event_name != 'pull_request' ||
|
||||
@@ -248,7 +247,7 @@ jobs:
|
||||
github.event.pull_request.head.repo.full_name == github.repository)
|
||||
runs-on: kamino-docker
|
||||
timeout-minutes: 5
|
||||
# Any small image does; the controller pin is reused so there is no other pin to bump.
|
||||
# The controller pin again, so this job adds no pin of its own to bump.
|
||||
container:
|
||||
image: git.rkdr.net/mortis/molecule-controller:0.1.1@sha256:94af071ecae858016f9a61afa6e45f8344f8fef7d22d1fe899e05f5f48f756e8
|
||||
steps:
|
||||
@@ -261,8 +260,8 @@ jobs:
|
||||
if [ "$SELECT_RESULT" != success ]; then
|
||||
echo "::error::select ended as $SELECT_RESULT"; exit 1
|
||||
fi
|
||||
# A skip is a pass only for an empty selection: a cancelled or failed run is not.
|
||||
if [ "$SCENARIOS_RESULT" = skipped ] && [ -z "$SELECTED" ]; then
|
||||
# A skip is a pass only for select's `none`: an empty output means it was lost.
|
||||
if [ "$SCENARIOS_RESULT" = skipped ] && [ "$SELECTED" = none ]; then
|
||||
echo "No scenario selected."
|
||||
elif [ "$SCENARIOS_RESULT" != success ]; then
|
||||
echo "::error::scenarios ended as $SCENARIOS_RESULT"; exit 1
|
||||
|
||||
@@ -540,12 +540,38 @@ class WorkflowJobsTest(unittest.TestCase):
|
||||
SELECTED=selected,
|
||||
)
|
||||
return subprocess.run(
|
||||
["bash", "-e", "-c", step["run"]],
|
||||
# act runs a job container's step with sh when the step sets no shell
|
||||
["sh", "-e", "-c", step["run"]],
|
||||
check=False,
|
||||
env=env,
|
||||
capture_output=True,
|
||||
).returncode
|
||||
|
||||
def written_output(self, selected):
|
||||
"""The `scenarios` output select's run step writes for that selector output."""
|
||||
step = next(s for s in self.JOBS["select"]["steps"] if s.get("id") == "select")
|
||||
block = step["run"].split("|| exit 1\n", 1)[1]
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
out = pathlib.Path(tmp, "output")
|
||||
subprocess.run(
|
||||
["sh", "-e", "-c", 'selected="$1"\n' + block, "sh", selected],
|
||||
check=True,
|
||||
env=dict(os.environ, GITHUB_OUTPUT=str(out)),
|
||||
)
|
||||
lines = out.read_text().splitlines()
|
||||
# GITHUB_OUTPUT's `name<<delimiter` form: the value is every line up to the delimiter
|
||||
name, delim = lines[0].split("<<", 1)
|
||||
self.assertEqual((name, lines[-1]), ("scenarios", delim))
|
||||
self.assertNotIn(delim, lines[1:-1])
|
||||
return "\n".join(lines[1:-1])
|
||||
|
||||
def test_select_writes_none_for_an_empty_selection(self):
|
||||
self.assertEqual(self.written_output(""), "none")
|
||||
|
||||
def test_select_writes_the_selection_one_directory_per_line(self):
|
||||
selected = "extensions/molecule/image-smoke\nroles/a/molecule/default"
|
||||
self.assertEqual(self.written_output(selected), selected)
|
||||
|
||||
def test_three_jobs_each_on_its_runner(self):
|
||||
self.assertEqual(
|
||||
{name: job["runs-on"] for name, job in self.JOBS.items()},
|
||||
@@ -563,15 +589,25 @@ class WorkflowJobsTest(unittest.TestCase):
|
||||
self.assertNotIn("container", self.JOBS["scenarios"])
|
||||
|
||||
def test_every_job_has_the_fork_guard(self):
|
||||
guard = self.JOBS["select"]["if"]
|
||||
self.assertIn("github.repository == 'mortis/kamino'", guard)
|
||||
guard = (
|
||||
"github.repository == 'mortis/kamino' && "
|
||||
"(github.event_name != 'pull_request' || "
|
||||
"github.event.pull_request.head.repo.full_name == github.repository)"
|
||||
)
|
||||
self.assertEqual(self.JOBS["select"]["if"], guard)
|
||||
for name, job in self.JOBS.items():
|
||||
self.assertTrue(job["if"].endswith(guard), name)
|
||||
|
||||
def test_the_scenarios_run_only_for_a_selection(self):
|
||||
job = self.JOBS["scenarios"]
|
||||
self.assertEqual(job["needs"], "select")
|
||||
self.assertTrue(job["if"].startswith("needs.select.outputs.scenarios != '' &&"))
|
||||
# false for `none` and for an empty or missing output alike
|
||||
self.assertTrue(
|
||||
job["if"].startswith(
|
||||
"needs.select.outputs.scenarios != 'none' && "
|
||||
"needs.select.outputs.scenarios != '' &&"
|
||||
)
|
||||
)
|
||||
self.assertEqual(
|
||||
self.JOBS["select"]["outputs"]["scenarios"],
|
||||
"${{ steps.select.outputs.scenarios }}",
|
||||
@@ -587,11 +623,13 @@ class WorkflowJobsTest(unittest.TestCase):
|
||||
self.assertEqual(job["needs"], ["select", "scenarios"])
|
||||
self.assertTrue(job["if"].startswith("always() &&"))
|
||||
|
||||
def test_the_result_passes_a_run_or_an_empty_selection_only(self):
|
||||
def test_the_result_passes_a_run_or_a_none_selection_only(self):
|
||||
self.assertEqual(
|
||||
self.result("success", "success", "roles/a/molecule/default"), 0
|
||||
)
|
||||
self.assertEqual(self.result("success", "skipped", ""), 0)
|
||||
self.assertEqual(self.result("success", "skipped", "none"), 0)
|
||||
# an empty output was lost, not an empty selection
|
||||
self.assertNotEqual(self.result("success", "skipped", ""), 0)
|
||||
self.assertNotEqual(
|
||||
self.result("success", "skipped", "roles/a/molecule/default"), 0
|
||||
)
|
||||
@@ -601,8 +639,8 @@ class WorkflowJobsTest(unittest.TestCase):
|
||||
self.assertNotEqual(
|
||||
self.result("success", "cancelled", "roles/a/molecule/default"), 0
|
||||
)
|
||||
self.assertNotEqual(self.result("failure", "skipped", ""), 0)
|
||||
self.assertNotEqual(self.result("skipped", "skipped", ""), 0)
|
||||
self.assertNotEqual(self.result("failure", "skipped", "none"), 0)
|
||||
self.assertNotEqual(self.result("skipped", "skipped", "none"), 0)
|
||||
|
||||
|
||||
class ScenarioSequenceTest(unittest.TestCase):
|
||||
|
||||
Reference in new issue
Block a user