Repository navigation
Serve versioned web releases without restarting application services #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,14 @@ | ||
| :8080 { | ||
| root * /srv/web | ||
| try_files {path} /index.html | ||
| file_server | ||
| root * /srv/web/current | ||
| header Cache-Control "no-cache" | ||
| @metadata path /release.json | ||
| handle @metadata { | ||
| file_server | ||
| } | ||
| handle { | ||
| try_files {path} /index.html | ||
| file_server { | ||
| hide .artifact-sha256 | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| """Snapshot the existing flat web deployment without deleting or modifying its files.""" | ||
|
|
||
| import argparse | ||
| import os | ||
| import shutil | ||
| import tempfile | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| def migrate(root: Path) -> None: | ||
| current = root / "current" | ||
|
|
||
| if current.is_symlink() and (current / "index.html").is_file(): | ||
| return | ||
|
|
||
| if current.exists() or current.is_symlink(): | ||
| raise ValueError("An invalid current entry already exists; inspect it before migrating") | ||
|
|
||
| if not (root / "index.html").is_file(): | ||
| raise ValueError("Extract an initial web release into this directory before migrating") | ||
|
|
||
| sources = [path for path in root.iterdir() if path.name != "releases" and not path.name.startswith(".")] | ||
|
|
||
| if any(path.is_symlink() or any(child.is_symlink() for child in path.rglob("*")) for path in sources): | ||
| raise ValueError("The initial web deployment must not contain symlinks") | ||
|
|
||
| releases = root / "releases" | ||
| releases.mkdir(exist_ok=True) | ||
| snapshot = Path(tempfile.mkdtemp(prefix="initial-", dir=releases)) | ||
|
|
||
| try: | ||
| for source in sources: | ||
| if source.is_dir(): | ||
| shutil.copytree(source, snapshot / source.name) | ||
| else: | ||
| shutil.copy2(source, snapshot / source.name) | ||
|
|
||
| snapshot.chmod(0o755) | ||
| link = root / ".current-next" | ||
| link.symlink_to(f"releases/{snapshot.name}") | ||
| os.replace(link, current) | ||
| except BaseException: | ||
| shutil.rmtree(snapshot) | ||
| raise | ||
|
|
||
|
|
||
| def main() -> None: | ||
| parser = argparse.ArgumentParser(description=__doc__) | ||
| parser.add_argument("--root", type=Path, default=Path(__file__).resolve().parent / "web") | ||
| args = parser.parse_args() | ||
| migrate(args.root) | ||
| print("Versioned web layout ready; original flat files remain intact") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| """The web layout migration preserves the deployed app and is safe to repeat.""" | ||
|
|
||
| import importlib.util | ||
| import tempfile | ||
| import unittest | ||
| from pathlib import Path | ||
|
|
||
| spec = importlib.util.spec_from_file_location( | ||
| "migrate_web_layout", Path(__file__).resolve().parents[2] / "deploy/migrate_web_layout.py" | ||
| ) | ||
| assert spec is not None and spec.loader is not None | ||
| layout = importlib.util.module_from_spec(spec) | ||
| spec.loader.exec_module(layout) | ||
|
|
||
|
|
||
| class WebLayoutTest(unittest.TestCase): | ||
| def test_snapshot_preserves_original_files_and_is_idempotent(self) -> None: | ||
| with tempfile.TemporaryDirectory() as directory: | ||
| root = Path(directory) | ||
| (root / "index.html").write_text("existing release") | ||
| (root / "assets").mkdir() | ||
| (root / "assets/book.js").write_text("book") | ||
| layout.migrate(root) | ||
| target = (root / "current").resolve() | ||
| layout.migrate(root) | ||
| self.assertEqual((root / "current").resolve(), target) | ||
| self.assertEqual((root / "index.html").read_text(), "existing release") | ||
| self.assertEqual((root / "current/assets/book.js").read_text(), "book") | ||
|
|
||
| def test_missing_initial_release_rejected(self) -> None: | ||
| with tempfile.TemporaryDirectory() as directory, self.assertRaises(ValueError): | ||
| layout.migrate(Path(directory)) | ||
|
|
||
| def test_symlink_content_rejected_without_changes(self) -> None: | ||
| with tempfile.TemporaryDirectory() as directory: | ||
| root = Path(directory) | ||
| (root / "index.html").write_text("existing release") | ||
| (root / "escape").symlink_to("/tmp") | ||
|
|
||
| with self.assertRaises(ValueError): | ||
| layout.migrate(root) | ||
|
|
||
| self.assertFalse((root / "current").exists()) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When an existing flat deployment contains publicly served dot-prefixed content such as
.well-known/assetlinks.json, this filter silently omits the entire entry even though the previous/srv/webroot served it. After Caddy switches tocurrent, requests for those files fall through to/index.html, breaking domain verification or app-link association; copy dot-prefixed public assets and exclude only specific migration-control or private artifacts.Useful? React with 👍 / 👎.