34-create-package-process (#35)
Some checks failed
Python tests (make) / test (push) Successful in 11s
Build & Push Package / package-and-push (push) Failing after 3s

works on #34

Reviewed-on: #35
Co-authored-by: Josh <josh@joshuaschuett.com>
Co-committed-by: Josh <josh@joshuaschuett.com>
This commit is contained in:
2025-08-26 20:02:48 +00:00
committed by Josh
parent 27d0f1b6e6
commit 5f3d54f3d7
5 changed files with 159 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
name: Build & Push Package
on:
push:
branches: ["main"]
workflow_dispatch:
jobs:
package-and-push:
needs: test
runs-on: ubuntu-22.04
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Build and package
run: |
make clean
make all
python package.py
mkdir -p deploy/packages
mv chess-bundle.zip deploy/packages/
- name: Push package to registry
env:
REGISTRY_URL: ${{ secrets.REGISTRY_URL }}
REGISTRY_USER: ${{ secrets.REGISTRY_USER }}
REGISTRY_TOKEN: ${{ secrets.REGISTRY_TOKEN }}
run: ./deploy/push-to-registry.sh

5
.gitignore vendored
View File

@@ -1,3 +1,8 @@
build
data
__pycache__
*.env
*.zip
*config*
*local*
package

63
deploy/bundle.py Normal file
View File

@@ -0,0 +1,63 @@
from datetime import datetime
import fnmatch
import os
import zipfile
class Packager:
def __init__(
self,
name="chess",
include_dirs=None,
ignore_patterns=None
):
timestamp = datetime.now().strftime("%Y%m%d%H%M")
filename = f"{timestamp}.{name}.zip"
self.outfile = os.path.join("./deploy/package", filename)
self.include_dirs = include_dirs or []
self.ignore_patterns = ignore_patterns or []
def create_package(self):
with zipfile.ZipFile(self.outfile, "w", zipfile.ZIP_DEFLATED) as z:
for d in self.include_dirs:
if not os.path.exists(d):
raise FileNotFoundError(f"Missing directory {d}")
for root, _, files in os.walk(d):
for fname in files:
rel_path = os.path.relpath(
os.path.join(root, fname),
start=os.path.dirname(d)
)
if self._is_ignored(rel_path):
continue
z.write(os.path.join(root, fname), rel_path)
print(f"[+] Package created: {self.outfile}")
os.system(f"unzip -l {self.outfile}")
def _is_ignored(self, path):
for pat in self.ignore_patterns:
if fnmatch.fnmatch(path, pat):
return True
return False
if __name__ == "__main__":
packager = Packager(
name="chess",
include_dirs=[
"build",
"scripts",
"binding",
],
ignore_patterns=[
"*.pyc",
"__pycache__/*",
"*env*",
"*config*",
]
)
packager.create_package()

29
deploy/pull-package.sh Executable file
View File

@@ -0,0 +1,29 @@
#!/usr/bin/env bash
set -euo pipefail
CONFIG_FILE="$(dirname "$0")/local.env"
if [ -f "$CONFIG_FILE" ]; then
source "$CONFIG_FILE"
fi
VERSION="${VERSION:-${1:-latest}}"
if [ -z "$VERSION" ]; then
echo "Usage: $0 <version>"
echo "Or set VERSION env var."
exit 1
fi
OUTDIR="${DOWNLOAD_DIR:-downloads}"
mkdir -p "$OUTDIR"
FILENAME="${VERSION}.chess.zip"
URL="${REGISTRY_URL}/api/packages/projects/generic/chess/${VERSION}/chess.zip"
echo "${URL}"
echo "[*] Pulling package $FILENAME (version: $VERSION) from registry..."
curl -f -u "${REGISTRY_USER}:${REGISTRY_TOKEN}" \
-o "${OUTDIR}/${FILENAME}" \
"$URL"
echo "[+] Downloaded to ${OUTDIR}/${FILENAME}"

32
deploy/push-package.sh Executable file
View File

@@ -0,0 +1,32 @@
#!/usr/bin/env bash
# Load config if present else use environment.
CONFIG_FILE="$(dirname "$0")/local.env"
if [ -f "$CONFIG_FILE" ]; then
source "$CONFIG_FILE"
fi
# Latest package.
OUTFILE=$(ls -1t ./deploy/package/* | head -n1 || true)
FILENAME="$(basename "$OUTFILE")"
VERSION="${FILENAME%%.*}"
echo "[*] Pushing ${VERSION} to registry."
curl -f -u "${REGISTRY_USER}:${REGISTRY_TOKEN}" \
--upload-file "$OUTFILE" \
"${REGISTRY_URL}/api/packages/projects/generic/chess/${VERSION}/chess.zip"
echo "[*] Deleting ${VERSION} latest to overwrite."
curl -f -u "${REGISTRY_USER}:${REGISTRY_TOKEN}" \
-X DELETE \
"${REGISTRY_URL}/api/packages/projects/generic/chess/latest/chess.zip" || true
echo "[*] Pushing ${VERSION} as latest to registry."
curl -f -u "${REGISTRY_USER}:${REGISTRY_TOKEN}" \
--upload-file "$OUTFILE" \
"${REGISTRY_URL}/api/packages/projects/generic/chess/latest/chess.zip" || true