Step 11 of 11· AI Customer Support Chatbot
Deployment
Package the desk with Docker, run it on a hosting platform with Supabase, add the widget to the website, and update the index automatically with GitHub Actions.
Purpose#
Run the desk on a public HTTPS address and add the widget to the company website. The index is updated automatically when articles change.
In this step:
- Create a production database
- Build a Docker image that runs as a non-root user
- Deploy to a hosting platform
- Add the widget to the website
- Update the index automatically with GitHub Actions
Setup#
1. Production database#
Create a second Supabase project, support-desk-prod, as in step 2. Development then never touches customer conversations.
DATABASE_URL="<production pooler URI>" uv run python -m support_desk.db
DATABASE_URL="<production pooler URI>" uv run python -m support_desk.index2. Docker image#
FROM python:3.12-slim
COPY --from=ghcr.io/astral-sh/uv:0.11.32 /uv /bin/uv
WORKDIR /app
COPY pyproject.toml uv.lock ./
RUN uv sync --frozen
COPY support_desk ./support_desk
COPY knowledge_base ./knowledge_base
# Run as an unprivileged user instead of root.
RUN useradd --create-home app && chown app /app
USER app
# Logs appear right away in the hosting platform's log view; no .pyc files in the image.
ENV PORT=8000 PYTHONUNBUFFERED=1 PYTHONDONTWRITEBYTECODE=1
CMD ["sh", "-c", "exec .venv/bin/uvicorn support_desk.app:app --host 0.0.0.0 --port ${PORT}"].env
.venv
.git
evals
__pycache__A small Python image installs the locked dependencies, copies the code and runs the server as a non-root user on the port the platform sets, with logs written immediately.
Build and run locally (requires Docker):
docker build -t support-desk .
docker run --rm -p 8000:8000 --env-file .env support-deskOpen http://localhost:8000.
3. Hosting#
Any platform that runs a Docker container and provides HTTPS works: Render, Railway, Fly.io, Google Cloud Run. The steps for Render:
- Push the project to a GitHub repository. Check that
.envis not in it:git ls-files | grep .envmust print only.env.example, if anything. - Give Render access to the repository. Open github.com/settings/applications, go to the Installed GitHub Apps tab and click Configure next to Render. Under Repository access, select the repository and click Save. If Render is not listed, connect GitHub in Render first: New → Web Service → GitHub.
- In render.com, click New → Web Service and choose the repository. Check that Language is Docker. Under Advanced, set Dockerfile Path to
./dockerfile. - Docker Command (called Start Command on some platforms): the command that starts the server.
sh -c 'exec .venv/bin/uvicorn support_desk.app:app --host 0.0.0.0 --port ${PORT:-8000}'It is the same command as CMD in the dockerfile, so the field can also stay empty where the platform allows it. --host 0.0.0.0 makes the server reachable from outside the container. ${PORT:-8000} uses the port the platform sets in PORT, or 8000.
- Region: the one closest to your Supabase project. Every question makes several database queries; a distant region adds latency to each.
- Health Check Path:
/healthz. - Environment: add the variables from
.envwith production values:
| Variable | Production value |
|---|---|
OPENAI_API_KEY | A separate key for production, so it can be revoked without affecting development |
DATABASE_URL | The production pooler URI |
ALLOWED_ORIGINS | The website, for example https://www.brightside.example,https://brightside.example |
TRUST_PROXY_HEADERS | true (Render's proxy sets X-Forwarded-For) |
CLIENT_SALT | A new random value, see below |
MAX_MESSAGE_CHARS, CACHE_TTL_HOURS | As needed |
Create the salt:
python3 -c "import secrets; print(secrets.token_hex(32))"- Click Deploy. When the log shows
Uvicorn running on http://0.0.0.0:10000, openhttps://<your-service>.onrender.com/healthz. It returns{"ok":true}.
4. Widget on the website#
Add one line before </body> on every page of the website, or in the site's footer template:
<script src="https://<your-service>.onrender.com/widget.js" data-api="https://<your-service>.onrender.com" defer></script>- Website builders (WordPress, Webflow, Squarespace, Wix) have a setting for custom code in the footer.
- The website's address must be listed in
ALLOWED_ORIGINSexactly: scheme,wwwor not, no trailing slash.
5. GitHub Actions#
A workflow that updates the production index when articles change. Merging a pull request into main counts as a push to main.
The file must be at the root of the repository: .github/workflows/knowledge-base.yml. If the project is in a subfolder of the repository, add the folder to the path (support-desk/knowledge_base/**) and set working-directory: support-desk on the steps.
# Updates the search index when articles change on main.
# Needs two repository secrets: OPENAI_API_KEY and DATABASE_URL (Settings -> Secrets and variables -> Actions).
name: knowledge-base
on:
push:
branches: [main]
paths: ["knowledge_base/**"]
workflow_dispatch: # a "Run workflow" button in the Actions tab
# Never run two index updates at the same time.
concurrency: knowledge-base
jobs:
index:
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- uses: actions/checkout@v4
- uses: astral-sh/setup-uv@v6
- run: uv sync --frozen
- run: uv run python -m support_desk.index
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
DATABASE_URL: ${{ secrets.DATABASE_URL }}A push to main that changes knowledge_base/ re-indexes production; two runs never overlap. Run workflow in the Actions tab starts it by hand.
Secrets: the workflow reads OPENAI_API_KEY and DATABASE_URL from repository secrets. Add both before the first run:
- In the GitHub repository, open Settings → Secrets and variables → Actions.
- On the Secrets tab, under Repository secrets, click New repository secret.
- Name:
DATABASE_URL. Secret: the production pooler URI. Click Add secret. - Repeat for
OPENAI_API_KEY.
Use the Secrets tab, not Variables, and not environment secrets under Settings → Environments: the workflow does not see those.
Checkpoint#
https://<your-service>.onrender.com/healthzreturns{"ok":true}.- On the website, the chat button appears. Ask "What is your phone number?". The answer contains 555-0142.
- In the browser's developer tools, Console shows no CORS errors.
- Ask a question that needs a person and leave an email address. The ticket appears in
DATABASE_URL="<production URI>" uv run python -m support_desk.handoff. - On GitHub, open Actions → knowledge-base → Run workflow. The run is green and its log ends with a line such as
Index updated: 0 added, 0 removed, 21 unchanged. - Change a sentence in an article, push to
main, wait for theindexjob, and ask about the change.
Troubleshooting#
- The repository is not in Render's list: Render has no access to it. Add it under Configure → Repository access for Render at github.com/settings/applications.
- The build fails with
failed to read dockerfileoropen Dockerfile: no such file: Dockerfile Path is still./Dockerfile. Set it to./dockerfileunder Settings → Build & Deploy. - The deploy fails with
Missing settings: …: the named environment variables are not set on the hosting platform. Add them and redeploy. - The log shows
CLIENT_SALT is the default value: setCLIENT_SALTto a random value (see above) before customers use the chat. - The service starts but is not reachable: the start command is missing
--host 0.0.0.0or uses a fixed port instead ofPORT. - Deploy fails with
uv.lockerrors: commituv.lock.--frozenrequires it. /healthzreturns 500: checkDATABASE_URLin the hosting settings. The log shows the database error.- The widget loads but every request fails with a CORS error: the website's exact origin is missing from
ALLOWED_ORIGINS. Redeploy after changing it. - The workflow fails with
DATABASE_URL is not set: the repository secret is missing, misspelled, or was added as a variable or environment secret. Add it as described in section 5 and run the workflow again. - The workflow fails with
missing front matter: an article does not start with---,title:andurl:(step 3). Common causes: a blank line or invisible character before the first---, or Windows line endings (switch CRLF to LF in the editor's status bar). Check locally withuv run python -m support_desk.kbbefore pushing. The database is not changed when the workflow fails. - The workflow does not start after a push: the push did not change files under
knowledge_base/, went to a branch other thanmain, or the workflow file is not at the root of the repository. - The first request after a pause takes 30 seconds or more: free hosting plans stop idle services. Use a paid instance for a customer-facing website.
AI Automations Guild
Join the community
Ask questions, get help with this tutorial and share what you build. Free on Skool.