| name: π Sync .env to Hugging Face Secrets |
|
|
| on: |
| workflow_dispatch: |
| |
|
|
| 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 |
| 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.") |
|
|
| |
| 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 |