Skip to content

Commit cc93f14

Browse files
committed
New AI Server Repo
1 parent aca6078 commit cc93f14

File tree

251 files changed

+98029
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

251 files changed

+98029
-1
lines changed

.deploy/docker-compose.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
services:
2+
app:
3+
image: ghcr.io/${IMAGE_REPO}:${RELEASE_VERSION}
4+
restart: always
5+
ports:
6+
- "8080"
7+
container_name: ${APP_NAME}_app
8+
env_file: ".env"
9+
environment:
10+
VIRTUAL_HOST: ${HOST_DOMAIN},ai-server-cdn.diffusion.works
11+
VIRTUAL_PORT: 8080
12+
LETSENCRYPT_HOST: ${HOST_DOMAIN},ai-server-cdn.diffusion.works
13+
LETSENCRYPT_EMAIL: ${LETSENCRYPT_EMAIL}
14+
CIVIT_AI_API_KEY: ${CIVIT_AI_API_KEY}
15+
REPLICATE_API_KEY: ${REPLICATE_API_KEY}
16+
HTTPS_METHOD: noredirect # Disable HTTPS redirect since Cloudflare infinite loop redirects when default
17+
volumes:
18+
- ./App_Data:/app/App_Data
19+
20+
app-migration:
21+
image: ghcr.io/${IMAGE_REPO}:${RELEASE_VERSION}
22+
restart: "no"
23+
container_name: ${APP_NAME}_app_migration
24+
env_file: ".env"
25+
# API Providers populated based on available Environment Variables
26+
environment:
27+
GOOGLE_API_KEY: ${GOOGLE_API_KEY}
28+
GROQ_API_KEY: ${GROQ_API_KEY}
29+
MISTRAL_API_KEY: ${MISTRAL_API_KEY}
30+
OPENAI_API_KEY: ${OPENAI_API_KEY}
31+
OPENROUTER_API_KEY: ${OPENROUTER_API_KEY}
32+
profiles:
33+
- migration
34+
command: --AppTasks=migrate
35+
volumes:
36+
- ./App_Data:/app/App_Data
37+
38+
networks:
39+
default:
40+
external: true
41+
name: nginx

.deploy/nginx-proxy-compose.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
version: "3.9"
2+
3+
services:
4+
nginx-proxy:
5+
image: nginxproxy/nginx-proxy
6+
container_name: nginx-proxy
7+
restart: always
8+
ports:
9+
- "80:80"
10+
- "443:443"
11+
volumes:
12+
- conf:/etc/nginx/conf.d
13+
- vhost:/etc/nginx/vhost.d
14+
- html:/usr/share/nginx/html
15+
- dhparam:/etc/nginx/dhparam
16+
- certs:/etc/nginx/certs:ro
17+
- /var/run/docker.sock:/tmp/docker.sock:ro
18+
labels:
19+
- "com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy"
20+
21+
letsencrypt:
22+
image: nginxproxy/acme-companion:2.2
23+
container_name: nginx-proxy-le
24+
restart: always
25+
depends_on:
26+
- "nginx-proxy"
27+
environment:
28+
29+
volumes:
30+
- certs:/etc/nginx/certs:rw
31+
- acme:/etc/acme.sh
32+
- vhost:/etc/nginx/vhost.d
33+
- html:/usr/share/nginx/html
34+
- /var/run/docker.sock:/var/run/docker.sock:ro
35+
36+
networks:
37+
default:
38+
name: nginx
39+
40+
volumes:
41+
conf:
42+
vhost:
43+
html:
44+
dhparam:
45+
certs:
46+
acme:

.github/workflows/README.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
## Overview
2+
3+
This template uses the deployment configurations for a ServiceStack .NET 8 application. The application is containerized using Docker and is set up to be automatically built and deployed via GitHub Actions. The recommended deployment target is a stand-alone Linux server running Ubuntu, with an NGINX reverse proxy also containerized using Docker, which a Docker Compose file is included in the template under the `.deploy` directory.
4+
5+
### Highlights
6+
- 🌐 **NGINX Reverse Proxy**: Utilizes an NGINX reverse proxy to handle web traffic and SSL termination.
7+
- 🚀 **GitHub Actions**: Leverages GitHub Actions for CI/CD, pushing Docker images to GitHub Container Registry and deploying them on a remote server.
8+
- 🐳 **Dockerized ServiceStack App**: The application is containerized, with the image built using `.NET 8`.
9+
- 🔄 **Automated Migrations**: Includes a separate service for running database migrations.
10+
11+
### Technology Stack
12+
- **Web Framework**: ServiceStack
13+
- **Language**: C# (.NET 8)
14+
- **Containerization**: Docker
15+
- **Reverse Proxy**: NGINX
16+
- **CI/CD**: GitHub Actions
17+
- **OS**: Ubuntu 22.04 (Deployment Server)
18+
19+
20+
21+
## Deployment Server Setup
22+
23+
To successfully host your ServiceStack applications, there are several components you need to set up on your deployment server. This guide assumes you're working on a standalone Linux server (Ubuntu is recommended) with SSH access enabled.
24+
25+
### Prerequisites
26+
27+
1. **SSH Access**: Required for GitHub Actions to communicate with your server.
28+
2. **Docker**: To containerize your application.
29+
3. **Docker-Compose**: For orchestrating multiple containers.
30+
4. **Ports**: 80 and 443 should be open for web access.
31+
5. **nginx-reverse-proxy**: For routing traffic to multiple ServiceStack applications and managing TLS certificates.
32+
33+
You can use any cloud-hosted or on-premises server like Digital Ocean, AWS, Azure, etc., for this setup.
34+
35+
### Step-by-Step Guide
36+
37+
#### 1. Install Docker and Docker-Compose
38+
39+
It is best to follow the [latest installation instructions on the Docker website](https://docs.docker.com/engine/install/ubuntu/) to ensure to have the correct setup with the latest patches.
40+
41+
#### 2. Configure SSH for GitHub Actions
42+
43+
Generate a dedicated SSH key pair to be used by GitHub Actions:
44+
45+
```bash
46+
ssh-keygen -t rsa -b 4096 -f ~/.ssh/github_actions
47+
```
48+
49+
Add the public key to the `authorized_keys` file on your server:
50+
51+
```bash
52+
cat ~/.ssh/github_actions.pub >> ~/.ssh/authorized_keys
53+
```
54+
55+
Then, add the *private* key to your GitHub Secrets as `DEPLOY_KEY` to enable GitHub Actions to SSH into the server securely.
56+
57+
#### 3. Set Up nginx-reverse-proxy
58+
59+
You should have a `docker-compose` file similar to the `nginx-proxy-compose.yml` in your repository. Upload this file to your server:
60+
61+
```bash
62+
scp nginx-proxy-compose.yml user@your_server:~/
63+
```
64+
65+
To bring up the nginx reverse proxy and its companion container for handling TLS certificates, run:
66+
67+
```bash
68+
docker compose -f ~/nginx-proxy-compose.yml up -d
69+
```
70+
71+
This will start an nginx reverse proxy along with a companion container. They will automatically watch for additional Docker containers on the same network and initialize them with valid TLS certificates.
72+
73+
74+
75+
## GitHub Repository Setup
76+
77+
Configuring your GitHub repository is an essential step for automating deployments via GitHub Actions. This guide assumes you have a `release.yml` workflow file in your repository's `.github/workflows/` directory, and your deployment server has been set up according to the [Deployment Server Setup](#Deployment-Server-Setup) guidelines.
78+
79+
### Secrets Configuration
80+
81+
Your GitHub Actions workflow requires the following secrets to be set in your GitHub repository:
82+
83+
1. **`DEPLOY_HOST`**: The hostname for SSH access. This can be either an IP address or a domain with an A-record pointing to your server.
84+
2. **`DEPLOY_USERNAME`**: The username for SSH login. Common examples include `ubuntu`, `ec2-user`, or `root`.
85+
3. **`DEPLOY_KEY`**: The SSH private key to securely access the deployment server. This should be the same key you've set up on your server for GitHub Actions.
86+
4. **`LETSENCRYPT_EMAIL`**: Your email address, required for Let's Encrypt automated TLS certificates.
87+
88+
#### Using GitHub CLI for Secret Management
89+
90+
You can conveniently set these secrets using the [GitHub CLI](https://cli.github.com/manual/gh_secret_set) like this:
91+
92+
```bash
93+
gh secret set DEPLOY_HOST --body="your-host-or-ip"
94+
gh secret set DEPLOY_USERNAME --body="your-username"
95+
gh secret set DEPLOY_KEY --bodyFile="path/to/your/ssh-private-key"
96+
gh secret set LETSENCRYPT_EMAIL --body="[email protected]"
97+
```
98+
99+
These secrets will populate environment variables within your GitHub Actions workflow and other configuration files, enabling secure and automated deployment of your ServiceStack applications.

.github/workflows/build.yml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
name: Build
2+
3+
on:
4+
pull_request: {}
5+
push:
6+
branches:
7+
- '**' # matches every branch
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: checkout
14+
uses: actions/checkout@v3
15+
16+
- name: Setup dotnet
17+
uses: actions/setup-dotnet@v3
18+
with:
19+
dotnet-version: '8.0'
20+
21+
- name: Extract SS license
22+
run: |
23+
SS_LICENSE=`echo '${{ secrets.APPSETTINGS_PATCH }}' | jq -r .[0].value.license`
24+
echo "::add-mask::$SS_LICENSE"
25+
echo "SERVICESTACK_LICENSE=${SS_LICENSE}" >> $GITHUB_ENV
26+
# Add Comfy Agent API key
27+
echo "COMFY_API_KEY=${{ secrets.COMFY_API_KEY }}" >> $GITHUB_ENV
28+
# Add Open AI Provider Keys
29+
echo "GOOGLE_API_KEY=${{ secrets.GOOGLE_API_KEY }}" >> $GITHUB_ENV
30+
echo "GROQ_API_KEY=${{ secrets.GROQ_API_KEY }}" >> $GITHUB_ENV
31+
echo "MISTRAL_API_KEY=${{ secrets.MISTRAL_API_KEY }}" >> $GITHUB_ENV
32+
echo "OPENAI_API_KEY=${{ secrets.OPENAI_API_KEY }}" >> $GITHUB_ENV
33+
echo "OPENROUTER_API_KEY=${{ secrets.OPENROUTER_API_KEY }}" >> $GITHUB_ENV
34+
35+
- name: build
36+
run: dotnet build
37+
working-directory: .
38+
39+
- name: test
40+
run: |
41+
dotnet test
42+
if [ $? -eq 0 ]; then
43+
echo TESTS PASSED
44+
else
45+
echo TESTS FAILED
46+
exit 1
47+
fi
48+
working-directory: ./AiServer.Tests
49+

.github/workflows/dockerhub.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: Build and Push .NET 8 Docker Image to DockerHub
2+
3+
on:
4+
push:
5+
branches: [main, master]
6+
release:
7+
types: [published]
8+
workflow_dispatch:
9+
10+
jobs:
11+
build_and_push:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Checkout code
15+
uses: actions/checkout@v3
16+
17+
- name: Setup .NET
18+
uses: actions/setup-dotnet@v3
19+
with:
20+
dotnet-version: '8.0.x'
21+
22+
- name: Login to DockerHub
23+
uses: docker/login-action@v2
24+
with:
25+
username: servicestack
26+
password: ${{ secrets.DOCKERHUB_TOKEN }}
27+
28+
- name: Build and push Docker image
29+
env:
30+
DOCKER_REPO: servicestack/${{ github.event.repository.name }}
31+
run: |
32+
# Determine version tag
33+
if [[ "${{ github.ref }}" == "refs/tags/"* ]]; then
34+
VERSION=${GITHUB_REF#refs/tags/}
35+
else
36+
VERSION=${{ github.sha }}
37+
fi
38+
39+
# Build and push using dotnet publish
40+
dotnet publish --os linux --arch x64 -c Release \
41+
-p:PublishProfile=DefaultContainer \
42+
-p:ContainerRepository=$DOCKER_REPO \
43+
-p:ContainerImageTags="latest"
44+
45+
# Push the image
46+
docker push $DOCKER_REPO:latest

0 commit comments

Comments
 (0)