Skip to content

armgit / fuobe

Public Imported from GitHub
0

The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.

Deploys: GitHub Actions builds each push to main, the server only installs it

Adarsh78c0e4312574
diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml
new file mode 100644
index 00000000..bcbc53dd
--- /dev/null
+++ b/.github/workflows/deploy.yml
@@ -0,0 +1,180 @@
+# Builds each push to main on GitHub's runners and sends the build to the server, which installs it.
+#
+# Two jobs, so the server's key never meets the code that builds:
+# - build: no secrets. Installs, typechecks, builds, packs .next with scripts/pack-build.sh, and keeps the
+# archive for a week. It holds only compiled code from this public repository: no node_modules, data or env.
+# - deploy: only for pushes to main of this repository, in the "production" environment, whose secrets only
+# it can read. It runs no code from the repository: it downloads the archive, checks its sha256, and sends it
+# over SSH. Its log says only whether the delivery worked.
+# The key only hands the file over. The server installs it only when GitHub's own record of this run's artifact
+# has the same sha256, so the key alone can't put code on the site.
+#
+# Secrets (environment "production"): FUOBE_DEPLOY_TARGET (user@host:port), FUOBE_DEPLOY_KEY (private key),
+# FUOBE_DEPLOY_KNOWN_HOSTS (the server's host key line).
+# A manual run (workflow_dispatch) builds and checks any branch. It never deploys, and never cancels a push's run.
+name: Build and deploy
+
+on:
+ push:
+ branches: [main]
+ workflow_dispatch:
+
+permissions:
+ contents: read
+
+concurrency:
+ group: build-and-deploy-${{ github.event_name }}-${{ github.ref }}
+ cancel-in-progress: true
+
+defaults:
+ run:
+ shell: bash
+
+jobs:
+ build:
+ if: github.repository == 'workingprototype/Fuobe'
+ runs-on: ubuntu-24.04
+ timeout-minutes: 30
+ permissions:
+ contents: read
+ outputs:
+ sha256: ${{ steps.pack.outputs.sha256 }}
+ env:
+ NEXT_TELEMETRY_DISABLED: "1"
+ COREPACK_ENABLE_DOWNLOAD_PROMPT: "0"
+ steps:
+ - name: Check out
+ uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
+ with:
+ persist-credentials: false
+
+ - name: Set up Node
+ uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
+ with:
+ node-version: 22.23.4
+ package-manager-cache: false
+
+ - name: Set up pnpm (the version package.json names)
+ run: |
+ corepack enable
+ node --version
+ pnpm --version
+
+ - name: Install
+ run: pnpm install --frozen-lockfile
+
+ - name: Typecheck
+ run: pnpm exec next typegen && pnpm typecheck
+
+ - name: Build
+ run: pnpm build
+
+ - name: Pack
+ id: pack
+ env:
+ COMMIT: ${{ github.sha }}
+ run: |
+ [ "$(git rev-parse HEAD)" = "$COMMIT" ] || { echo "::error::The checkout is not the pushed commit."; exit 1; }
+ scripts/pack-build.sh "$RUNNER_TEMP/fuobe-build.tar.zst"
+ echo "sha256=$(sha256sum "$RUNNER_TEMP/fuobe-build.tar.zst" | cut -d' ' -f1)" >> "$GITHUB_OUTPUT"
+
+ - name: Keep the build for the deploy job
+ uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
+ with:
+ path: ${{ runner.temp }}/fuobe-build.tar.zst
+ archive: false
+ retention-days: 7
+ if-no-files-found: error
+ overwrite: true
+
+ deploy:
+ needs: build
+ if: github.event_name == 'push' && github.ref == 'refs/heads/main' && github.repository == 'workingprototype/Fuobe'
+ runs-on: ubuntu-24.04
+ timeout-minutes: 15
+ environment: production
+ permissions: {}
+ steps:
+ - name: Download the build
+ uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
+ with:
+ name: fuobe-build.tar.zst
+ path: ${{ runner.temp }}/build
+
+ - name: Check the build
+ env:
+ COMMIT: ${{ github.sha }}
+ SHA256: ${{ needs.build.outputs.sha256 }}
+ run: |
+ if ! [[ $COMMIT =~ ^[0-9a-f]{40}$ && $SHA256 =~ ^[0-9a-f]{64}$ ]]; then
+ echo "::error::The build job gave no checksum."
+ exit 1
+ fi
+ mapfile -t found < <(find "$RUNNER_TEMP/build" -type f -name fuobe-build.tar.zst)
+ if [ "${#found[@]}" != 1 ]; then
+ echo "::error::The download holds ${#found[@]} builds, not one."
+ exit 1
+ fi
+ mv "${found[0]}" "$RUNNER_TEMP/fuobe-build.tar.zst"
+ if ! echo "$SHA256 $RUNNER_TEMP/fuobe-build.tar.zst" | sha256sum --check --status; then
+ echo "::error::The downloaded build doesn't match the build job's checksum."
+ exit 1
+ fi
+ echo "The build matches its checksum."
+
+ # Prints nothing about the server: not its address, user, port or answers. ssh's own messages go nowhere.
+ - name: Deliver
+ env:
+ COMMIT: ${{ github.sha }}
+ SHA256: ${{ needs.build.outputs.sha256 }}
+ DEPLOY_TARGET: ${{ secrets.FUOBE_DEPLOY_TARGET }}
+ DEPLOY_KEY: ${{ secrets.FUOBE_DEPLOY_KEY }}
+ DEPLOY_KNOWN_HOSTS: ${{ secrets.FUOBE_DEPLOY_KNOWN_HOSTS }}
+ run: |
+ missing=""
+ [ -n "$DEPLOY_TARGET" ] || missing+=" FUOBE_DEPLOY_TARGET"
+ [ -n "$DEPLOY_KEY" ] || missing+=" FUOBE_DEPLOY_KEY"
+ [ -n "$DEPLOY_KNOWN_HOSTS" ] || missing+=" FUOBE_DEPLOY_KNOWN_HOSTS"
+ if [ -n "$missing" ]; then
+ echo "::error::Missing secrets in the production environment:$missing"
+ exit 1
+ fi
+ # One secret for user, host and port: a short one, such as a port number alone, would be masked inside
+ # unrelated numbers in this public log, and so show what it is.
+ target_re='^([a-z_][a-z0-9_-]{0,31})@([A-Za-z0-9][A-Za-z0-9.-]*|\[[0-9A-Fa-f:]+\]):([0-9]{1,5})$'
+ if ! [[ $DEPLOY_TARGET =~ $target_re ]]; then
+ echo "::error::FUOBE_DEPLOY_TARGET is not user@host:port."
+ exit 1
+ fi
+ user=${BASH_REMATCH[1]}
+ host=${BASH_REMATCH[2]}
+ port=${BASH_REMATCH[3]}
+ host=${host#\[}
+ host=${host%\]}
+
+ # The key exists only for this step, readable by this user only.
+ umask 077
+ keys=$(mktemp -d)
+ trap 'rm -rf "$keys"' EXIT
+ printf '%s\n' "$DEPLOY_KEY" > "$keys/id"
+ printf '%s\n' "$DEPLOY_KNOWN_HOSTS" > "$keys/known_hosts"
+
+ set +e
+ answer=$(ssh -F /dev/null -q -o LogLevel=ERROR -o BatchMode=yes \
+ -o StrictHostKeyChecking=yes -o UserKnownHostsFile="$keys/known_hosts" -o GlobalKnownHostsFile=/dev/null \
+ -o UpdateHostKeys=no -o IdentitiesOnly=yes -o IdentityFile="$keys/id" -o IdentityAgent=none \
+ -o PasswordAuthentication=no -o KbdInteractiveAuthentication=no -o ClearAllForwardings=yes \
+ -o ForwardAgent=no -o ForwardX11=no -o RequestTTY=no \
+ -o ConnectTimeout=30 -o ServerAliveInterval=15 -o ServerAliveCountMax=4 \
+ -p "$port" -l "$user" -- "$host" "deliver $COMMIT $SHA256" \
+ < "$RUNNER_TEMP/fuobe-build.tar.zst" 2> /dev/null)
+ status=$?
+ set -e
+ rm -rf "$keys"
+
+ case "$status:$answer" in
+ 0:ok) echo "Delivered. The server installs it within a few minutes." ;;
+ *:refused) echo "::error::The server refused the build. Its journal says why."; exit 1 ;;
+ 255:*) echo "::error::Couldn't reach the server, or it didn't accept the host key or the deploy key."; exit 1 ;;
+ *) echo "::error::The delivery failed."; exit 1 ;;
+ esac
diff --git a/scripts/pack-build.sh b/scripts/pack-build.sh
new file mode 100755
index 00000000..2f9ff5dd
--- /dev/null
+++ b/scripts/pack-build.sh
@@ -0,0 +1,87 @@
+#!/usr/bin/env bash
+# Packs a production build for a server that runs it from its own export of the same commit.
+#
+# pnpm build && scripts/pack-build.sh <out.tar.zst>
+#
+# The archive holds two things:
+# - fuobe-build.txt: the commit, the sha256 of its pnpm-lock.yaml, the Node, pnpm and Next versions, and the
+# packages the build loads from node_modules.
+# - .next, without its cache, traces and generated types.
+# No node_modules, no data, no env file: the server installs the commit's dependencies itself (native modules
+# such as better-sqlite3 and sharp are built there) and keeps its own data and settings.
+#
+# The build reaches packages that stay outside the bundle through links: .next/node_modules/<package>-<hash>
+# points into node_modules/.pnpm. The links are listed in fuobe-build.txt, not packed, so the archive holds only
+# plain files and folders and the server makes each link again inside its own install.
+set -euo pipefail
+
+die() { echo "pack-build: $*" >&2; exit 1; }
+
+out=${1:-}
+[ -n "$out" ] || die "usage: scripts/pack-build.sh <out.tar.zst>"
+[ -f package.json ] && [ -f pnpm-lock.yaml ] && [ -e .git ] || die "run it from the repository's root"
+[ -s .next/BUILD_ID ] || die "no build here: run pnpm build first"
+root=$(pwd -P)
+
+# The build must be the commit: no changed tracked files, and the lockfile as committed.
+commit=$(git rev-parse --verify HEAD)
+[[ $commit =~ ^[0-9a-f]{40}$ ]] || die "HEAD is not a commit"
+git diff --quiet HEAD -- || die "tracked files differ from $commit: pack a clean checkout"
+lock=$(git cat-file blob "$commit:pnpm-lock.yaml" | sha256sum | cut -d' ' -f1)
+next=$(node -p 'require("next/package.json").version')
+
+# Links into node_modules: .next/node_modules/<name>-<hash>, or @scope/<name>-<hash>.
+links=""
+if [ -d .next/node_modules ]; then
+ while IFS= read -r -d '' entry; do
+ name=${entry#.next/node_modules/}
+ if [ -d "$entry" ] && [ ! -L "$entry" ]; then
+ [[ $name =~ ^@[A-Za-z0-9._-]+$ ]] || die "unexpected folder $entry"
+ continue
+ fi
+ [ -L "$entry" ] || die "$entry is not a link"
+ [[ $name =~ ^(@[A-Za-z0-9._-]+/)?[A-Za-z0-9._-]+-[0-9a-f]{8,64}$ ]] || die "unexpected link name $entry"
+ target=$(realpath "$entry")
+ case $target in
+ "$root"/node_modules/.pnpm/*) target=${target#"$root"/} ;;
+ *) die "$entry points outside node_modules/.pnpm" ;;
+ esac
+ [[ $target =~ ^node_modules/\.pnpm/[^/]+/node_modules/(@[^/]+/)?[^/]+$ ]] || die "unexpected target for $entry"
+ links+="link $name $target"$'\n'
+ done < <(find .next/node_modules -mindepth 1 -maxdepth 2 -print0 | LC_ALL=C sort -z)
+fi
+
+# What goes in: .next minus what only the build machine or an editor uses.
+skip=(-path .next/cache -o -path .next/node_modules -o -path .next/types -o -path .next/dev
+ -o -path .next/trace -o -path .next/trace-build)
+odd=$(find .next \( "${skip[@]}" \) -prune -o ! -type f ! -type d -print | head -5)
+[ -z "$odd" ] || die "not a plain file or folder: $odd"
+private=$(find .next \( "${skip[@]}" \) -prune -o \( -name '.env*' -o -name '*.pem' -o -name '*.key' \
+ -o -name '*.db' -o -name '*.db-*' -o -name '*.sqlite*' -o -name 'id_*' \) -print | head -5)
+[ -z "$private" ] || die "refusing to pack what looks private: $private"
+
+work=$(mktemp -d)
+trap 'rm -rf "$work"' EXIT
+find .next -mindepth 1 \( "${skip[@]}" \) -prune -o -print0 | LC_ALL=C sort -z > "$work/files"
+{
+ echo "fuobe-build 1"
+ echo "commit $commit"
+ echo "lockfile-sha256 $lock"
+ echo "node $(node -v)"
+ echo "pnpm $(pnpm -v)"
+ echo "next $next"
+ printf '%s' "$links"
+} > "$work/fuobe-build.txt"
+
+# Same commit, same bytes: fixed owner, modes and dates, and sorted names. zstd -19 makes it about a third of
+# gzip's size (the server source maps are most of it).
+mkdir -p "$(dirname "$out")"
+tar --create --format=gnu --owner=0 --group=0 --numeric-owner --mode='u+rwX,go+rX,go-w' \
+ --mtime="$(git log -1 --format=%cI "$commit")" --no-recursion \
+ -C "$work" fuobe-build.txt -C "$root" --null -T "$work/files" | zstd -19 -T0 -q -c > "$out"
+
+bytes=$(wc -c < "$out")
+max=$((200 * 1024 * 1024))
+[ "$bytes" -le "$max" ] || die "the archive is $((bytes / 1048576)) MB, over the server's limit"
+echo "Packed ${commit:0:7}: $(tr -cd '\0' < "$work/files" | wc -c) entries, $((bytes / 1048576)) MB ($bytes bytes)."
+echo "sha256 $(sha256sum "$out" | cut -d' ' -f1)"