MADTOOL / .github /workflows /sync-to-hf.yml
github-actions[bot]
Sync backend files from GitHub 66452e1
62ba81f
Raw History Blame Contribute Delete
4.33 kB
name: Sync to Hugging Face Hub
# GitHub main is the source of truth for both Spaces:
# - mppolak/madtools (madtools.org) runs the website and gets the repo as-is.
# - AccuratePaperExtraction/MADTOOL only displays the backend files: it gets a
# snapshot without index.html, with deploy/backend-space-README.md as README.md.
# The two jobs are independent, so one failing doesn't block the other.
on:
push:
branches: [ main ]
workflow_dispatch:
jobs:
website:
name: sync website (mppolak/madtools)
runs-on: ubuntu-latest
env:
SPACE: mppolak/madtools
steps:
- uses: actions/checkout@v6
with:
fetch-depth: 0
lfs: true
- name: Block deploy if the Space has commits GitHub doesn't
env:
HF_TOKEN: ${{ secrets.HF_TOKEN_MADTOOLS }}
run: |
git remote add hf https://user:$HF_TOKEN@huggingface.co/spaces/$SPACE
git fetch hf main
AHEAD=$(git rev-list --count HEAD..hf/main)
if [ "$AHEAD" -gt 0 ]; then
echo "::error::The Space $SPACE has $AHEAD commit(s) not on GitHub. Deploy blocked to avoid discarding them."
echo "Commits only on the Space:"
git log --format=' %h %an %s' HEAD..hf/main
echo ""
echo "To reconcile, from a local clone of this repo:"
echo " git fetch https://huggingface.co/spaces/$SPACE main"
echo " git merge FETCH_HEAD # review the diff, resolve, or revert what you don't want"
echo " git push origin main # re-runs this workflow, which will then fast-forward the Space"
exit 1
fi
echo "Space $SPACE is in sync with GitHub; proceeding."
- name: Push to HF Space
run: git push hf HEAD:main
backend-files:
name: sync backend files (AccuratePaperExtraction/MADTOOL)
runs-on: ubuntu-latest
env:
SPACE: AccuratePaperExtraction/MADTOOL
steps:
- uses: actions/checkout@v6
with:
fetch-depth: 0
lfs: true
- name: Block deploy if the Space was edited directly
env:
HF_TOKEN: ${{ secrets.HF_TOKEN }}
run: |
git remote add hf https://user:$HF_TOKEN@huggingface.co/spaces/$SPACE
git fetch hf main
# The Space's history holds snapshots this job made (marked with a
# Deployed-From trailer); any other commit GitHub lacks is a manual edit.
MANUAL=$(git log --format='%h %(trailers:key=Deployed-From,valueonly,separator=)' HEAD..hf/main | awk 'NF==1 {print $1}')
if [ -n "$MANUAL" ]; then
echo "::error::The Space $SPACE has commits made directly on it. Deploy blocked to avoid discarding them."
for c in $MANUAL; do git log -1 --format=' %h %an %s' "$c"; done
echo ""
echo "To reconcile, from a local clone of this repo, bring any wanted changes into GitHub"
echo "(the README lives in deploy/backend-space-README.md), then record the edits as merged:"
echo " git fetch https://huggingface.co/spaces/$SPACE main"
echo " git merge -s ours FETCH_HEAD"
echo " git push origin main"
exit 1
fi
echo "Space $SPACE has no direct edits; proceeding."
- name: Build backend-files snapshot and push
run: |
export GIT_INDEX_FILE="$RUNNER_TEMP/backend-files.index"
git read-tree HEAD
git rm --cached --quiet --ignore-unmatch index.html deploy/backend-space-README.md
git update-index --add --cacheinfo 100644,"$(git hash-object -w deploy/backend-space-README.md)",README.md
TREE=$(git write-tree)
unset GIT_INDEX_FILE
if [ "$TREE" = "$(git rev-parse 'hf/main^{tree}')" ]; then
echo "Space $SPACE already has these files; nothing to push."
exit 0
fi
COMMIT=$(git -c user.name="github-actions[bot]" \
-c user.email="41898282+github-actions[bot]@users.noreply.github.com" \
commit-tree "$TREE" -p hf/main \
-m "Sync backend files from GitHub ${GITHUB_SHA::7}" \
-m "Deployed-From: $GITHUB_SHA")
git push hf "$COMMIT:refs/heads/main"