| name: CI/CD to Hugging Face Space with uv |
|
|
| on: |
| push: |
| branches: |
| - main |
|
|
| jobs: |
| deploy: |
| runs-on: ubuntu-latest |
| steps: |
| - name: Check for HF_TOKEN availability |
| id: check_hf_token |
| env: |
| HF_TOKEN_CHECK: ${{ secrets.HF_TOKEN }} |
| run: | |
| if [ -z "$HF_TOKEN_CHECK" ]; then |
| echo "::notice::HF_TOKEN secret is not set. Hugging Face Space push will be skipped." |
| echo "push_enabled=false" >> $GITHUB_OUTPUT |
| else |
| echo "::notice::HF_TOKEN secret is set. Proceeding with Hugging Face Space push." |
| echo "push_enabled=true" >> $GITHUB_OUTPUT |
| fi |
| - name: Checkout repository |
| uses: actions/checkout@v4 |
| with: |
| fetch-depth: 0 |
| lfs: true |
|
|
| - name: Set up Python |
| uses: actions/setup-python@v5 |
| with: |
| python-version: "3.10" |
|
|
| - name: Install uv |
| |
| uses: astral-sh/setup-uv@v1 |
|
|
| - name: Check for pyproject.toml existence |
| id: check_pyproject |
| run: | |
| if [ -f requirements.txt ]; then |
| echo "::notice::requirements.txt already exists. Skipping uv generation." |
| echo "generate_reqs=false" >> $GITHUB_OUTPUT |
| elif [ -f pyproject.toml ]; then |
| echo "::notice::pyproject.toml found and no requirements.txt. Proceeding with uv pip compile." |
| echo "generate_reqs=true" >> $GITHUB_OUTPUT |
| else |
| echo "::notice::Neither requirements.txt nor pyproject.toml found. Skipping uv pip compile." |
| echo "generate_reqs=false" >> $GITHUB_OUTPUT |
| fi |
| |
| - name: Generate requirements.txt using uv |
| id: generate_reqs |
| |
| if: ${{ steps.check_pyproject.outputs.generate_reqs == 'true' }} |
| run: | |
| # Use uv pip compile to generate a locked requirements.txt from pyproject.toml |
| # This ensures reproducibility. |
| uv export --no-hashes --format requirements-txt > requirements.txt |
| # uv pip compile pyproject.toml -o requirements.txt |
| |
| |
| if [ -f requirements.txt ]; then |
| echo "requirements.txt generated successfully:" |
| cat requirements.txt |
| else |
| echo "Error: requirements.txt was not generated despite pyproject.toml existing." |
| exit 1 |
| fi |
|
|
| - name: Commit requirements.txt if changed |
| if: ${{ steps.check_pyproject.outputs.generate_reqs == 'true' }} |
| run: | |
| git config user.name "github-actions[bot]" |
| git config user.email "github-actions[bot]@users.noreply.github.com" |
| git add requirements.txt |
| git commit -m "chore: update requirements.txt [auto-generated by CI]" |
| echo "requirements.txt committed." |
| |
| - name: Push to HuggingFace Space |
| if: ${{ steps.check_hf_token.outputs.push_enabled == 'true' }} |
| env: |
| HF_TOKEN: ${{ secrets.HF_TOKEN }} |
| FORCE_PUSH: ${{ secrets.FORCE_PUSH }} |
| run: | |
| if [ -z "$FORCE_PUSH" ]; then |
| echo "::notice::FORCE_PUSH secret is not set." |
| git push https://GF-John:$HF_TOKEN@huggingface.co/spaces/GF-John/sam3 main |
| else |
| echo "::notice::FORCE_PUSH secret is set. Doing Force Push to Hugging Face Space." |
| git push -f https://GF-John:$HF_TOKEN@huggingface.co/spaces/GF-John/sam3 main |
| fi |
| |