LawMadad / .github /workflows /sync_env_to_huggingface.yml
ali4568's picture
πŸš€ Auto-deploy from GitHub Action
6607e80 verified
name: πŸ” Sync .env to Hugging Face Secrets
on:
workflow_dispatch: # βœ… safer than push trigger for secrets
# you can still use `push` if you want it automatic
jobs:
sync:
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v4
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install huggingface_hub python-dotenv requests
- name: Sync .env to Hugging Face Space Secrets
env:
HF_TOKEN: ${{ secrets.HF_TOKEN }}
DOTENV_CONTENT: ${{ secrets.DOTENV_CONTENT }}
SPACE_ID: ali4568/LawMadad # βœ… moved to env for cleaner code
run: |
python - <<'EOF'
import os, requests
HF_TOKEN = os.getenv("HF_TOKEN")
DOTENV_CONTENT = os.getenv("DOTENV_CONTENT")
SPACE_ID = os.getenv("SPACE_ID")
if not HF_TOKEN or not DOTENV_CONTENT:
raise ValueError("❌ Missing HF_TOKEN or DOTENV_CONTENT environment variables.")
# Parse .env-like content from secret
secrets = {}
for line in DOTENV_CONTENT.strip().splitlines():
if line and not line.startswith("#") and "=" in line:
key, value = line.split("=", 1)
secrets[key.strip()] = value.strip()
url = f"https://huggingface.co/api/spaces/{SPACE_ID}/secrets"
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
for key, value in secrets.items():
print(f"πŸ” Syncing {key} ...")
resp = requests.post(url, headers=headers, json={"key": key, "value": value})
if resp.status_code in (200, 201):
print(f"βœ… {key} created/updated successfully.")
elif resp.status_code == 409:
print(f"πŸŒ€ {key} already exists, updating...")
update_url = f"{url}/{key}"
update_resp = requests.put(update_url, headers=headers, json={"value": value})
if update_resp.status_code in (200, 201):
print(f"βœ… {key} updated successfully.")
else:
print(f"❌ Failed to update {key}: {update_resp.status_code} - {update_resp.text}")
else:
print(f"❌ Failed to sync {key}: {resp.status_code} - {resp.text}")
print("🎯 All .env variables synced to Hugging Face successfully!")
EOF