Spaces:
Runtime error
Runtime error
RyZ commited on
Commit ·
9a41034
1
Parent(s): 7a9f9c6
New feature
Browse files- .github/workflows/sync-to-hub.yml +19 -0
- Dockerfile +4 -1
- deploy_guide.md +123 -0
- fix_git_push.bat +39 -0
.github/workflows/sync-to-hub.yml
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
name: Sync to Hugging Face Hub
|
| 2 |
+
|
| 3 |
+
on:
|
| 4 |
+
push:
|
| 5 |
+
branches: [test]
|
| 6 |
+
|
| 7 |
+
jobs:
|
| 8 |
+
sync-to-hub:
|
| 9 |
+
runs-on: ubuntu-latest
|
| 10 |
+
steps:
|
| 11 |
+
- uses: actions/checkout@v3
|
| 12 |
+
with:
|
| 13 |
+
fetch-depth: 0
|
| 14 |
+
lfs: true
|
| 15 |
+
- name: Push to hub
|
| 16 |
+
env:
|
| 17 |
+
HF_TOKEN: ${{ secrets.HF_TOKEN }}
|
| 18 |
+
run: |
|
| 19 |
+
git push https://XRyZ:$HF_TOKEN@huggingface.co/spaces/XRyZ/whatsapp-backend-test test:main -f
|
Dockerfile
CHANGED
|
@@ -16,7 +16,6 @@ RUN go mod download
|
|
| 16 |
# Copy seluruh kode
|
| 17 |
COPY . .
|
| 18 |
|
| 19 |
-
# Buat direktori yang dibutuhkan dan setup permission
|
| 20 |
RUN mkdir -p /app/images /app/logs /app/audio && \
|
| 21 |
chmod -R 777 /app/images /app/logs /app/audio && \
|
| 22 |
chown -R appuser:appuser /app/images /app/logs /app/audio
|
|
@@ -27,6 +26,10 @@ RUN go build -o main .
|
|
| 27 |
# Beralih ke user non-root
|
| 28 |
USER appuser
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
# Expose port untuk Hugging Face Spaces
|
| 31 |
EXPOSE 7860
|
| 32 |
|
|
|
|
| 16 |
# Copy seluruh kode
|
| 17 |
COPY . .
|
| 18 |
|
|
|
|
| 19 |
RUN mkdir -p /app/images /app/logs /app/audio && \
|
| 20 |
chmod -R 777 /app/images /app/logs /app/audio && \
|
| 21 |
chown -R appuser:appuser /app/images /app/logs /app/audio
|
|
|
|
| 26 |
# Beralih ke user non-root
|
| 27 |
USER appuser
|
| 28 |
|
| 29 |
+
# Set environment variables for Hugging Face Spaces
|
| 30 |
+
ENV HOST_ADDRESS=0.0.0.0
|
| 31 |
+
ENV HOST_PORT=7860
|
| 32 |
+
|
| 33 |
# Expose port untuk Hugging Face Spaces
|
| 34 |
EXPOSE 7860
|
| 35 |
|
deploy_guide.md
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Deploying Go Backend to Hugging Face Spaces
|
| 2 |
+
|
| 3 |
+
This guide outlines the steps to deploy your Go application (`whatsapp-backend`) to Hugging Face Spaces using Docker.
|
| 4 |
+
|
| 5 |
+
## Prerequisites
|
| 6 |
+
|
| 7 |
+
- A [Hugging Face](https://huggingface.co/) account.
|
| 8 |
+
- Your project code locally.
|
| 9 |
+
|
| 10 |
+
## Step 1: Create a New Space
|
| 11 |
+
|
| 12 |
+
1. Log in to your Hugging Face account.
|
| 13 |
+
2. Click on your profile picture in the top right and select **New Space**.
|
| 14 |
+
3. **Owner**: Select your username or organization.
|
| 15 |
+
4. **Space Name**: Enter a name for your space (e.g., `whatsapp-backend`).
|
| 16 |
+
5. **License**: Choose a license (e.g., MIT) or leave it empty correctly.
|
| 17 |
+
6. **SDK**: Select **Docker**.
|
| 18 |
+
7. **Blank**: Choose "Blank" to start with an empty repository.
|
| 19 |
+
8. Click **Create Space**.
|
| 20 |
+
|
| 21 |
+
## Step 2: Prepare Your Code (Already Done by Agent)
|
| 22 |
+
|
| 23 |
+
Your `Dockerfile` has been configured to:
|
| 24 |
+
|
| 25 |
+
- Use port `7860` (required by Hugging Face Spaces).
|
| 26 |
+
- Bind to address `0.0.0.0`.
|
| 27 |
+
|
| 28 |
+
Ensure your `Dockerfile` contains:
|
| 29 |
+
|
| 30 |
+
```dockerfile
|
| 31 |
+
ENV HOST_ADDRESS=0.0.0.0
|
| 32 |
+
ENV HOST_PORT=7860
|
| 33 |
+
EXPOSE 7860
|
| 34 |
+
```
|
| 35 |
+
|
| 36 |
+
## Step 3: Deployment Methods
|
| 37 |
+
|
| 38 |
+
You can deploy using one of two methods:
|
| 39 |
+
|
| 40 |
+
### Method A: Upload via Web Interface (Easiest)
|
| 41 |
+
|
| 42 |
+
1. In your newly created Space, go to the **Files** tab.
|
| 43 |
+
2. Click **Add file** -> **Upload files**.
|
| 44 |
+
3. Drag and drop all your project files (including `.dockerignore` if you have one, `Dockerfile`, `go.mod`, `go.sum`, `main.go`, and all source directories like `config`, `controllers`, etc.) into the upload area.
|
| 45 |
+
- _Note: Do not upload `.git` folder._
|
| 46 |
+
4. Commit the changes with a message like "Initial commit for deployment".
|
| 47 |
+
5. Hugging Face will automatically start building your Docker image. You can see the progress in the **App** tab.
|
| 48 |
+
|
| 49 |
+
### Method B: Push via Git (Recommended for updates)
|
| 50 |
+
|
| 51 |
+
1. In your Space, click on the **Clone repository** button (or just copy the HTTPS URL).
|
| 52 |
+
2. Open your terminal in your project directory.
|
| 53 |
+
3. Initialize git if you haven't (or use your existing git setup):
|
| 54 |
+
```bash
|
| 55 |
+
git remote add space https://huggingface.co/spaces/YOUR_USERNAME/SPACE_NAME
|
| 56 |
+
```
|
| 57 |
+
4. Push your code:
|
| 58 |
+
```bash
|
| 59 |
+
git push space main
|
| 60 |
+
```
|
| 61 |
+
_(Note: You might need to use a Hugging Face Access Token as your password if prompted)._
|
| 62 |
+
|
| 63 |
+
### Method C: Automated Sync (GitHub Actions) - **RECOMMENDED**
|
| 64 |
+
|
| 65 |
+
This method automatically pushes your code to the Hugging Face Space whenever you push to the `test` branch of your GitHub repository.
|
| 66 |
+
|
| 67 |
+
1. **Generate a Hugging Face Token**:
|
| 68 |
+
|
| 69 |
+
- Go to your [Hugging Face Settings > Tokens](https://huggingface.co/settings/tokens).
|
| 70 |
+
- Create a new token with **write** permissions.
|
| 71 |
+
- Copy the token.
|
| 72 |
+
|
| 73 |
+
2. **Add Secret to GitHub**:
|
| 74 |
+
|
| 75 |
+
- Go to your GitHub repository.
|
| 76 |
+
- Navigate to **Settings** > **Secrets and variables** > **Actions**.
|
| 77 |
+
- Click **New repository secret**.
|
| 78 |
+
- Name: `HF_TOKEN`
|
| 79 |
+
- Value: Paste your Hugging Face token.
|
| 80 |
+
- Click **Add secret**.
|
| 81 |
+
|
| 82 |
+
3. **Update Workflow File**:
|
| 83 |
+
|
| 84 |
+
- Open `.github/workflows/sync-to-hub.yml` in your project.
|
| 85 |
+
- Find the `git push` command at the bottom.
|
| 86 |
+
- Replace `YOUR_USERNAME` and `SPACE_NAME` with your actual Hugging Face username and Space name.
|
| 87 |
+
- _Example_: `git push https://ryu:Start@huggingface.co/spaces/ryu/whatsapp-backend test:main -f`
|
| 88 |
+
|
| 89 |
+
4. **Trigger Deployment**:
|
| 90 |
+
- Simply push any change to your `test` branch:
|
| 91 |
+
```bash
|
| 92 |
+
git checkout test
|
| 93 |
+
git add .
|
| 94 |
+
git commit -m "New feature"
|
| 95 |
+
git push origin test
|
| 96 |
+
```
|
| 97 |
+
- The GitHub Action will run and sync your code to the Spaces.
|
| 98 |
+
|
| 99 |
+
## Step 4: Configure Secrets (Environment Variables)
|
| 100 |
+
|
| 101 |
+
Your application uses environment variables (like Database credentials). You **must** set these in Hugging Face settings, not in the `Dockerfile` (for security).
|
| 102 |
+
|
| 103 |
+
1. Go to your Space's **Settings** tab.
|
| 104 |
+
2. Scroll down to **Variables and secrets**.
|
| 105 |
+
3. Click **New secret** for sensitive data (like passwords) or **New variable** for public config.
|
| 106 |
+
4. Add the following based on your `env_config.go` and `.env` needs:
|
| 107 |
+
|
| 108 |
+
- `DB_HOST`
|
| 109 |
+
- `DB_PORT`
|
| 110 |
+
- `DB_USER`
|
| 111 |
+
- `DB_PASSWORD`
|
| 112 |
+
- `DB_NAME`
|
| 113 |
+
- `SALT` (Optional, defaults to "Def4u|7")
|
| 114 |
+
- `TZ` (Optional, e.g., "Asia/Jakarta")
|
| 115 |
+
|
| 116 |
+
_Note: `HOST_ADDRESS` and `HOST_PORT` are already set in the Dockerfile, so you don't need to add them here unless you want to override them._
|
| 117 |
+
|
| 118 |
+
## Step 5: Verify Deployment
|
| 119 |
+
|
| 120 |
+
1. Go to the **App** tab.
|
| 121 |
+
2. Wait for the status to change from **Building** to **Running**.
|
| 122 |
+
3. If successful, you will see your application output or a blank screen (since it's a backend API).
|
| 123 |
+
4. You can verify it's running by checking the **Logs** tab for server startup messages.
|
fix_git_push.bat
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
@echo off
|
| 2 |
+
echo ==========================================
|
| 3 |
+
echo Fix Git Push Issue
|
| 4 |
+
echo ==========================================
|
| 5 |
+
|
| 6 |
+
echo [1/3] Checking Git Status...
|
| 7 |
+
git status
|
| 8 |
+
if %ERRORLEVEL% NEQ 0 (
|
| 9 |
+
echo Error: Not a git repository or git is not installed.
|
| 10 |
+
pause
|
| 11 |
+
exit /b
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
echo.
|
| 15 |
+
echo [2/3] Setting up 'dev' branch...
|
| 16 |
+
REM Try to create the branch. If it fails, it might exist.
|
| 17 |
+
git checkout -b dev 2>nul
|
| 18 |
+
if %ERRORLEVEL% NEQ 0 (
|
| 19 |
+
echo Branch 'dev' may already exist. Switching to it...
|
| 20 |
+
git checkout dev
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
echo.
|
| 24 |
+
echo [3/3] Pushing to remote...
|
| 25 |
+
git push -u origin dev
|
| 26 |
+
|
| 27 |
+
if %ERRORLEVEL% EQU 0 (
|
| 28 |
+
echo.
|
| 29 |
+
echo SUCCESS: Successfully pushed to dev branch!
|
| 30 |
+
) else (
|
| 31 |
+
echo.
|
| 32 |
+
echo FAILED: Could not push to remote. Please check:
|
| 33 |
+
echo 1. You have internet connection.
|
| 34 |
+
echo 2. You have permission to push to this repository.
|
| 35 |
+
echo 3. The remote URL is correct.
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
echo.
|
| 39 |
+
pause
|