Spaces:
Sleeping
A newer version of the Gradio SDK is available:
5.46.1
GitHub Actions CI/CD Setup Guide
This guide will help you set up automatic deployment from GitHub to Hugging Face Spaces using GitHub Actions.
Prerequisites
- GitHub Repository: Your code should be in a GitHub repository
- Hugging Face Space: You need to create a Space on Hugging Face first
- Hugging Face Token: You need a write token from Hugging Face
Step 1: Create Hugging Face Space
- Go to Hugging Face Spaces
- Click "Create new Space"
- Configure your space:
- Space name:
fitness-ai-assistant
(or your preferred name) - License: MIT
- SDK: Gradio
- Hardware: CPU basic (free) or upgrade as needed
- Visibility: Public or Private
- Space name:
- Important: Initialize with a README or empty repository
- Note down your username and space name
Step 2: Get Hugging Face Token
- Go to Hugging Face Settings > Access Tokens
- Click "New token"
- Configure the token:
- Name:
GitHub Actions Deploy
- Type: Write (required for pushing code)
- Scopes: Leave default or select specific repositories
- Name:
- Copy the token (you won't see it again!)
Step 3: Configure GitHub Repository Secrets
- Go to your GitHub repository
- Navigate to Settings > Secrets and variables > Actions
- Click New repository secret and add these three secrets:
Required Secrets:
Secret Name | Value | Example |
---|---|---|
HF_TOKEN |
Your Hugging Face write token | hf_xxxxxxxxxxxxxxxxxxxx |
HF_USERNAME |
Your Hugging Face username | yourusername |
HF_SPACE_NAME |
Your space name | fitness-ai-assistant |
Note: API keys (OPENAI_API_KEY, ANTHROPIC_API_KEY) should be set directly in your Hugging Face Space settings for better security.
Adding Each Secret:
- Click New repository secret
- Enter the Name (exactly as shown above)
- Enter the Secret (the actual value)
- Click Add secret
- Repeat for all three secrets
Important: After deployment, remember to set your API keys directly in your Hugging Face Space settings (see HF_SPACE_SETUP.md
).
Step 4: Choose Your Workflow
I've created two workflow options for you:
Option 1: Simple Workflow (deploy-to-hf.yml
)
- Basic deployment on push to main
- Suitable for most use cases
- Fewer validation steps
Option 2: Advanced Workflow (deploy-to-hf-advanced.yml
)
- Comprehensive validation and error handling
- Retry logic for failed deployments
- Manual trigger option
- More detailed logging
- Recommended for production use
Step 5: Test the Setup
Option A: Push to Main Branch
- Make any change to your code
- Commit and push to the main branch:
git add . git commit -m "Test GitHub Actions deployment" git push origin main
- Check the Actions tab in your GitHub repository
- Watch the deployment process
Option B: Manual Trigger (Advanced Workflow Only)
- Go to Actions tab in your GitHub repository
- Select "Deploy to Hugging Face Spaces (Advanced)"
- Click Run workflow
- Optionally enable "Force deployment"
- Click Run workflow
Step 6: Verify Deployment
- Check that the GitHub Action completed successfully
- Visit your Hugging Face Space:
https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME
- Wait for the Space to build (may take a few minutes)
- Test your deployed app
Troubleshooting
Common Issues:
1. "Failed to clone HF Space"
- Cause: Incorrect
HF_USERNAME
orHF_SPACE_NAME
- Solution: Double-check the secrets match your actual HF username and space name
2. "Permission denied"
- Cause:
HF_TOKEN
doesn't have write permissions - Solution: Generate a new token with Write access
3. "Space not found"
- Cause: Space doesn't exist or name is wrong
- Solution: Create the space first on Hugging Face, then update the secret
4. "Build failed on Hugging Face"
- Cause: Missing dependencies or code errors
- Solution: Check the Hugging Face Space logs for specific errors
Debugging Steps:
Check GitHub Action Logs:
- Go to Actions tab > Click on failed run > Expand failed steps
Verify Secrets:
- Go to Repository Settings > Secrets > Ensure all three secrets are set
Test Locally:
- Run
python fitness_agent/app.py
locally to ensure it works - Check for any import errors
- Run
Check Hugging Face Space Logs:
- Visit your space and check the "Logs" tab for build errors
Advanced Configuration
Customizing Deployment Triggers
You can modify when deployments happen by editing the workflow file:
on:
push:
branches: [ main, develop ] # Deploy from multiple branches
paths-ignore:
- 'docs/**' # Ignore documentation changes
release:
types: [ published ] # Deploy on releases
Environment-Specific Deployments
For multiple environments (dev, staging, prod):
- Create separate HF Spaces for each environment
- Use different secret names:
HF_SPACE_NAME_DEV
,HF_SPACE_NAME_PROD
- Modify the workflow to use different secrets based on branch
Slack/Discord Notifications
Add notification steps to your workflow:
- name: Notify on Success
if: success()
run: |
curl -X POST -H 'Content-type: application/json' \
--data '{"text":"π Fitness App deployed successfully!"}' \
${{ secrets.SLACK_WEBHOOK_URL }}
Security Best Practices
- Token Rotation: Regularly rotate your HF tokens
- Scope Limitation: Use minimal required token permissions
- Secret Management: Never commit tokens to code
- Branch Protection: Enable branch protection on main branch
- Review Required: Require PR reviews for production deployments
Workflow Features
What the Workflow Does:
- Validates your code structure and syntax
- Tests critical imports
- Syncs your code to Hugging Face Space
- Handles errors gracefully with retries
- Provides detailed logs and status updates
Automatic Triggers:
- Push to main branch (excluding documentation changes)
- Manual workflow dispatch
- Merged pull requests to main
Smart Deployment:
- Only deploys when there are actual code changes
- Skips deployment for documentation-only changes
- Provides force-deploy option for manual runs
Support
If you encounter issues:
- Check this troubleshooting guide
- Review GitHub Action logs
- Check Hugging Face Space logs
- Verify all secrets are correctly configured
Happy deploying! π