shakauthossain's picture
Enhance CI workflow with secret checks and error handling
3f9f52c unverified
name: Backend CI
on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main ]
jobs:
backend:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.9", "3.10", "3.11"]
steps:
- uses: actions/checkout@v4
- name: Setup Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Cache pip
uses: actions/cache@v3
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('pyproject.toml', 'requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -e ".[dev]"
- name: Lint with flake8
run: |
flake8 app/ tests/
- name: Type check with mypy
run: |
mypy app/
- name: Run tests with coverage
run: |
python -m pytest --cov --cov-report=xml --cov-report=term-missing
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
file: ./coverage.xml
flags: unittests
name: codecov-umbrella
fail_ci_if_error: false
deploy:
runs-on: ubuntu-latest
needs: backend
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
lfs: true
- name: Debug - Check secrets are set
run: |
echo "Checking if secrets are available..."
if [ -z "${{ secrets.HF_TOKEN }}" ]; then
echo "❌ HF_TOKEN is NOT set!"
else
echo "βœ… HF_TOKEN is set (length: ${{ secrets.HF_TOKEN != '' && 'yes' || 'no' }})"
fi
if [ -z "${{ secrets.HF_USERNAME }}" ]; then
echo "❌ HF_USERNAME is NOT set!"
else
echo "βœ… HF_USERNAME is set"
fi
if [ -z "${{ secrets.HF_SPACE_NAME }}" ]; then
echo "❌ HF_SPACE_NAME is NOT set!"
else
echo "βœ… HF_SPACE_NAME is set"
fi
- name: Verify Space Configuration
env:
HF_TOKEN: ${{ secrets.HF_TOKEN }}
HF_USERNAME: ${{ secrets.HF_USERNAME }}
HF_SPACE_NAME: ${{ secrets.HF_SPACE_NAME }}
run: |
set +e # Don't exit on error in this step
echo "=== Space Configuration Check ==="
# Check if variables are set
if [ -z "$HF_USERNAME" ]; then
echo "❌ ERROR: HF_USERNAME is empty!"
echo "Please add HF_USERNAME secret in GitHub Settings > Secrets > Actions"
exit 1
fi
if [ -z "$HF_SPACE_NAME" ]; then
echo "❌ ERROR: HF_SPACE_NAME is empty!"
echo "Please add HF_SPACE_NAME secret in GitHub Settings > Secrets > Actions"
exit 1
fi
if [ -z "$HF_TOKEN" ]; then
echo "❌ ERROR: HF_TOKEN is empty!"
echo "Please add HF_TOKEN secret in GitHub Settings > Secrets > Actions"
exit 1
fi
echo "βœ… All secrets are set"
echo ""
echo "String lengths:"
echo " HF_USERNAME: ${#HF_USERNAME} characters"
echo " HF_SPACE_NAME: ${#HF_SPACE_NAME} characters"
echo " HF_TOKEN: ${#HF_TOKEN} characters"
echo ""
# Check for common issues
if [[ "$HF_USERNAME" =~ ^[[:space:]] ]] || [[ "$HF_USERNAME" =~ [[:space:]]$ ]]; then
echo "⚠️ WARNING: HF_USERNAME has leading or trailing spaces!"
fi
if [[ "$HF_SPACE_NAME" =~ ^[[:space:]] ]] || [[ "$HF_SPACE_NAME" =~ [[:space:]]$ ]]; then
echo "⚠️ WARNING: HF_SPACE_NAME has leading or trailing spaces!"
fi
# Test API access
echo ""
echo "Testing Hugging Face API access..."
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" \
-H "Authorization: Bearer ${HF_TOKEN}" \
"https://huggingface.co/api/spaces/${HF_USERNAME}/${HF_SPACE_NAME}")
echo "API Response: $HTTP_CODE"
case $HTTP_CODE in
200)
echo "βœ… Space found and accessible!"
;;
404)
echo "❌ Space not found (404)"
echo ""
echo "Troubleshooting steps:"
echo "1. Verify your space exists on Hugging Face"
echo "2. Check the URL format: https://huggingface.co/spaces/USERNAME/SPACE-NAME"
echo "3. Ensure HF_USERNAME matches your Hugging Face username exactly (case-sensitive)"
echo "4. Ensure HF_SPACE_NAME matches your space name exactly (case-sensitive)"
exit 1
;;
401|403)
echo "❌ Authentication failed ($HTTP_CODE)"
echo ""
echo "Token may be invalid or expired. Please:"
echo "1. Go to https://huggingface.co/settings/tokens"
echo "2. Create a new token with 'write' permissions"
echo "3. Update the HF_TOKEN secret in GitHub"
exit 1
;;
*)
echo "⚠️ Unexpected response: $HTTP_CODE"
echo "Proceeding anyway..."
;;
esac
echo "===================================="
- name: Deploy to Hugging Face Space
env:
HF_TOKEN: ${{ secrets.HF_TOKEN }}
HF_USERNAME: ${{ secrets.HF_USERNAME }}
HF_SPACE_NAME: ${{ secrets.HF_SPACE_NAME }}
run: |
# Configure Git
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git config --global user.name "github-actions[bot]"
# Set up the remote URL with authentication
SPACE_URL="https://${HF_USERNAME}:${HF_TOKEN}@huggingface.co/spaces/${HF_USERNAME}/${HF_SPACE_NAME}"
# Remove existing remote if present
git remote remove space 2>/dev/null || true
# Add the Hugging Face Space as a remote
git remote add space "${SPACE_URL}"
# Try to fetch from the space
echo "Fetching from Hugging Face Space..."
if git fetch space 2>&1 | tee /tmp/fetch_output.txt; then
echo "βœ… Fetch successful"
else
if grep -q "Repository not found" /tmp/fetch_output.txt; then
echo "❌ Repository not found - this usually means:"
echo " 1. The space doesn't exist"
echo " 2. The username or space name is incorrect"
echo " 3. The space is private and the token doesn't have access"
exit 1
fi
echo "⚠️ Fetch failed, but continuing (may be first push)"
fi
# Push to the space
echo ""
echo "Pushing to Hugging Face Space..."
if git push space HEAD:main --force; then
echo ""
echo "βœ… Successfully deployed!"
echo "πŸš€ View your space at: https://huggingface.co/spaces/${HF_USERNAME}/${HF_SPACE_NAME}"
else
echo ""
echo "❌ Push failed!"
exit 1
fi