diff --git a/.gitea/workflows/run-build.yml b/.gitea/workflows/run-build.yml new file mode 100644 index 0000000..c14f0a9 --- /dev/null +++ b/.gitea/workflows/run-build.yml @@ -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 \ No newline at end of file diff --git a/.gitignore b/.gitignore index c2f2db2..00344bb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,8 @@ build data __pycache__ +*.env +*.zip +*config* +*local* +package \ No newline at end of file diff --git a/deploy/bundle.py b/deploy/bundle.py new file mode 100644 index 0000000..3cba4d7 --- /dev/null +++ b/deploy/bundle.py @@ -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() \ No newline at end of file diff --git a/deploy/pull-package.sh b/deploy/pull-package.sh new file mode 100755 index 0000000..c6ec57b --- /dev/null +++ b/deploy/pull-package.sh @@ -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 " + 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}" diff --git a/deploy/push-package.sh b/deploy/push-package.sh new file mode 100755 index 0000000..9660123 --- /dev/null +++ b/deploy/push-package.sh @@ -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