Skip to content

Python tool for aircraft Doppler audio geolocation using historical ADS-B flight data (Source-available)

Keep stale workflow reruns from replacing the main image tag

Cinna Davisca49ab8c329a
diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml
index c134658..53ffe93 100644
--- a/.github/workflows/docker.yml
+++ b/.github/workflows/docker.yml
@@ -20,6 +20,7 @@ jobs:
outputs:
publish: ${{ steps.metadata.outputs.publish }}
image_tags: ${{ steps.metadata.outputs.tags }}
+ image_revision: ${{ steps.metadata.outputs.revision }}
image_id: ${{ steps.export.outputs.image_id }}
image_artifact_id: ${{ steps.upload.outputs.artifact-id }}
steps:
@@ -175,21 +176,48 @@ jobs:
- name: Publish the validated image
env:
IMAGE_TAGS: ${{ needs.docker.outputs.image_tags }}
+ IMAGE_REVISION: ${{ needs.docker.outputs.image_revision }}
run: |
python - <<'PY'
import json
import os
from pathlib import Path
+ import re
import subprocess
tags = json.loads(os.environ["IMAGE_TAGS"])
if not tags:
raise ValueError("No approved image tags to publish")
- for tag in tags:
- subprocess.run(["docker", "tag", "dopplergeist:ci", tag], check=True)
- subprocess.run(["docker", "push", tag], check=True)
with Path(os.environ["GITHUB_STEP_SUMMARY"]).open("a") as summary:
summary.write("## Published Docker image\n\n")
- summary.writelines(f"- `{tag}`\n" for tag in tags)
- summary.write("\nAll tags refer to the image validated by this workflow run.\n")
+ for tag in tags:
+ subprocess.run(["docker", "tag", "dopplergeist:ci", tag], check=True)
+ if tag.endswith(":main"):
+ try:
+ remote = subprocess.run(
+ ["git", "ls-remote", "--exit-code",
+ "https://github.com/1337isnot1337/DOPPLERGEIST.git",
+ "refs/heads/main"],
+ check=True, capture_output=True, text=True, timeout=30)
+ fields = remote.stdout.split()
+ if (len(fields) != 2 or fields[1] != "refs/heads/main"
+ or not re.fullmatch(r"[0-9a-f]{40}", fields[0])):
+ raise ValueError("Unexpected public main-ref response")
+ except (OSError, subprocess.SubprocessError, ValueError) as error:
+ summary.write(f"- Did not push `{tag}`: current public main could not be verified.\n")
+ raise RuntimeError("Could not verify current public main; :main was not pushed") from error
+ if fields[0] != os.environ["IMAGE_REVISION"]:
+ message = (f"Skipped `{tag}`: built revision `{os.environ['IMAGE_REVISION']}` "
+ f"differs from current main `{fields[0]}`.")
+ print(message)
+ summary.write(f"- {message}\n")
+ continue
+ subprocess.run(["docker", "push", tag], check=True)
+ summary.write(f"- Published `{tag}`\n")
+ digests = json.loads(subprocess.run(
+ ["docker", "image", "inspect", "--format", "{{json .RepoDigests}}", tag],
+ check=True, capture_output=True, text=True).stdout)
+ for digest in digests or []:
+ summary.write(f" - Registry digest: `{digest}`\n")
+ summary.write("\nAll published tags refer to the image validated by this workflow run.\n")
PY
diff --git a/docs/DOCKER.md b/docs/DOCKER.md
index 68eb863..8afe531 100644
--- a/docs/DOCKER.md
+++ b/docs/DOCKER.md
@@ -106,11 +106,17 @@ image without rebuilding it.
| `vMAJOR.MINOR.PATCH-PRERELEASE` | The matching prerelease tag, such as `v0.2.0-rc.1` |
| `sha-FULL_COMMIT_ID` | The complete source commit ID for every published image |
+Immediately before pushing `main`, the workflow reads the current public Git
+branch. If that branch has moved since the image was built, the run keeps its
+commit tag and skips `main`, with the reason in the workflow summary. An
+unreadable branch ref fails publication without updating `main`. This also
+applies to reruns of older builds.
+
Version tags use SemVer release/prerelease syntax without `+build` metadata.
Image labels record the source repository, commit, version and
`PolyForm-Noncommercial-1.0.0` license. A revision tag identifies the source
-commit. To retain an exact built image, use the registry digest printed by the
-publishing job: `ghcr.io/1337isnot1337/dopplergeist@sha256:DIGEST`.
+commit. To retain an exact built image, use the registry digest recorded in the
+workflow summary: `ghcr.io/1337isnot1337/dopplergeist@sha256:DIGEST`.
Maintainers can publish `main` through **Actions → Docker build, local service
and GHCR → Run workflow**, selecting `main`. Other manually selected branches
diff --git a/tests/test_container_publish_metadata.py b/tests/test_container_publish_metadata.py
index fced403..6ee7916 100644
--- a/tests/test_container_publish_metadata.py
+++ b/tests/test_container_publish_metadata.py
@@ -1,7 +1,13 @@
"""Publication-policy checks; no audio, trajectories or image builds."""
import importlib.util
+import json
+import os
from pathlib import Path
+import subprocess
+import tempfile
+import textwrap
import unittest
+from unittest import mock
SOURCE = Path(__file__).resolve().parents[1] / "scripts/container_publish_metadata.py"
@@ -9,6 +15,13 @@ SPEC = importlib.util.spec_from_file_location("container_publish_metadata", SOUR
MODULE = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(MODULE)
REVISION = "dcd8cb73114af0a20be869e9e8c3295080b65ab8"
+WORKFLOW = SOURCE.parents[1] / ".github/workflows/docker.yml"
+# Exercise the actual publishing step, with registry/ref commands replaced by
+# software controls. These checks neither build nor publish a container.
+PUBLISH_STEP = WORKFLOW.read_text().split(" - name: Publish the validated image\n", 1)[1]
+PUBLISH_SCRIPT = compile(textwrap.dedent(
+ PUBLISH_STEP.split(" python - <<'PY'\n", 1)[1].split(" PY\n", 1)[0]),
+ str(WORKFLOW), "exec")
class ContainerPublishMetadataTests(unittest.TestCase):
@@ -66,5 +79,79 @@ class ContainerPublishMetadataTests(unittest.TestCase):
MODULE.metadata(MODULE.REPOSITORY, "push", "refs/heads/main", revision)
+class ContainerPublishCurrentMainTests(unittest.TestCase):
+ image = "ghcr.io/1337isnot1337/dopplergeist"
+ sha_tag = image + ":sha-" + REVISION
+ main_tag = image + ":main"
+ digest = image + "@sha256:" + "a" * 64
+
+ def run_publication(self, remote, tags=None):
+ calls = []
+
+ def run(arguments, **kwargs):
+ calls.append(arguments)
+ if arguments[0] == "git":
+ self.assertEqual(arguments, ["git", "ls-remote", "--exit-code",
+ "https://github.com/1337isnot1337/DOPPLERGEIST.git", "refs/heads/main"])
+ self.assertEqual(kwargs, {"check": True, "capture_output": True,
+ "text": True, "timeout": 30})
+ if isinstance(remote, Exception):
+ raise remote
+ return subprocess.CompletedProcess(arguments, 0, stdout=remote)
+ if arguments[:3] == ["docker", "image", "inspect"]:
+ return subprocess.CompletedProcess(arguments, 0, stdout=json.dumps([self.digest]))
+ self.assertIn(arguments[:2], (["docker", "tag"], ["docker", "push"]))
+ return subprocess.CompletedProcess(arguments, 0)
+
+ with tempfile.TemporaryDirectory() as directory:
+ summary = Path(directory) / "summary.md"
+ environment = {"IMAGE_TAGS": json.dumps(tags or [self.sha_tag, self.main_tag]),
+ "IMAGE_REVISION": REVISION, "GITHUB_STEP_SUMMARY": str(summary)}
+ with mock.patch.dict(os.environ, environment), mock.patch.object(subprocess, "run", run):
+ failure = None
+ try:
+ exec(PUBLISH_SCRIPT, {})
+ except RuntimeError as error:
+ failure = error
+ return calls, failure, summary.read_text()
+
+ def test_current_main_pushes_both_tags_with_last_moment_ref_check(self):
+ calls, failure, summary = self.run_publication(REVISION + "\trefs/heads/main\n")
+ self.assertIsNone(failure)
+ pushes = [call[-1] for call in calls if call[:2] == ["docker", "push"]]
+ self.assertEqual(pushes, [self.sha_tag, self.main_tag])
+ main_push = calls.index(["docker", "push", self.main_tag])
+ self.assertEqual(calls[main_push - 1][0], "git")
+ self.assertIn(self.digest, summary)
+
+ def test_stale_main_keeps_sha_and_records_skipped_mutable_tag(self):
+ current = "6eca37d6208d5006551ae7be7b62053e7ace2dd6"
+ calls, failure, summary = self.run_publication(current + "\trefs/heads/main\n")
+ self.assertIsNone(failure)
+ self.assertEqual([call[-1] for call in calls if call[:2] == ["docker", "push"]],
+ [self.sha_tag])
+ self.assertIn("Skipped `" + self.main_tag + "`", summary)
+ self.assertIn(current, summary)
+ self.assertIn(REVISION, summary)
+
+ def test_unreadable_main_fails_after_sha_without_pushing_main(self):
+ for remote in ("", REVISION + "\trefs/heads/other\n", "invalid\trefs/heads/main\n",
+ subprocess.CalledProcessError(128, ["git", "ls-remote"]),
+ subprocess.TimeoutExpired(["git", "ls-remote"], 30)):
+ with self.subTest(remote=remote):
+ calls, failure, summary = self.run_publication(remote)
+ self.assertIsInstance(failure, RuntimeError)
+ self.assertEqual([call[-1] for call in calls if call[:2] == ["docker", "push"]],
+ [self.sha_tag])
+ self.assertIn("current public main could not be verified", summary)
+
+ def test_release_publication_does_not_depend_on_main(self):
+ tags = [self.sha_tag, self.image + ":v1.2.3"]
+ calls, failure, summary = self.run_publication("", tags=tags)
+ self.assertIsNone(failure)
+ self.assertFalse(any(call[0] == "git" for call in calls))
+ self.assertEqual([call[-1] for call in calls if call[:2] == ["docker", "push"]], tags)
+
+
if __name__ == "__main__":
unittest.main()