Update .gitlab-ci.yml

This commit is contained in:
Russell Ballestrini 2025-03-20 01:11:41 +00:00
parent d82414684f
commit 16bd06718a

View file

@ -1,10 +1,14 @@
stages:
- deploy
- deploy-streamlit
variables:
APP_NAME: "slop.unturf.com"
APP_DIR: "/opt/${APP_NAME}"
VENV_DIR: "${APP_DIR}/venv"
BACKEND_APP_NAME: "slop.unturf.com"
BACKEND_APP_DIR: "/opt/${BACKEND_APP_NAME}"
BACKEND_VENV_DIR: "${BACKEND_APP_DIR}/venv"
STREAMLIT_APP_NAME: "streamlit.slop.unturf.com"
STREAMLIT_APP_DIR: "/opt/${STREAMLIT_APP_NAME}"
STREAMLIT_VENV_DIR: "${STREAMLIT_APP_DIR}/venv"
CADDYFILE_DIR: "/etc/caddy"
CADDY_SNIPPETS_DIR: "/etc/caddy/caddy_snippets"
@ -17,28 +21,28 @@ deploy:
script:
# Step 1: Define variables with substitutions
- |
SERVICE_NAME="uwsgi-${APP_NAME//./-}.service" # Replace dots with dashes
UWSGI_SOCKET="${APP_DIR}/uwsgi.sock" # Place socket in app directory
SERVICE_NAME="uwsgi-${BACKEND_APP_NAME//./-}.service" # Replace dots with dashes
UWSGI_SOCKET="${BACKEND_APP_DIR}/uwsgi.sock" # Place socket in app directory
# Step 2: Synchronize the code to the application directory
# Step 2: Synchronize the code to the backend application directory
- |
if [ ! -d "${APP_DIR}" ]; then
mkdir -p "${APP_DIR}"
if [ ! -d "${BACKEND_APP_DIR}" ]; then
mkdir -p "${BACKEND_APP_DIR}"
fi
rsync -a --exclude=data/ --exclude=venv/ --delete "${CI_PROJECT_DIR}/" "${APP_DIR}/"
rsync -a --exclude=data/ --exclude=venv/ --delete "${CI_PROJECT_DIR}/" "${BACKEND_APP_DIR}/"
# Step 3: Set up Python virtual environment and install dependencies
- |
if [ ! -d "${VENV_DIR}" ]; then
python3 -m venv "${VENV_DIR}"
if [ ! -d "${BACKEND_VENV_DIR}" ]; then
python3 -m venv "${BACKEND_VENV_DIR}"
fi
source "${VENV_DIR}/bin/activate"
source "${BACKEND_VENV_DIR}/bin/activate"
pip install --upgrade pip
pip install -r "${APP_DIR}/requirements.txt"
pip install -r "${BACKEND_APP_DIR}/requirements.txt"
# Step 4: Install uWSGI in virtual environment
- |
source "${VENV_DIR}/bin/activate"
source "${BACKEND_VENV_DIR}/bin/activate"
pip install uwsgi
# Step 5: Create or update the systemd user service file for uWSGI
@ -50,21 +54,19 @@ deploy:
cat > ~/.config/systemd/user/${SERVICE_NAME} <<EOF
[Unit]
Description=uWSGI instance to serve ${APP_NAME}
Description=uWSGI instance to serve ${BACKEND_APP_NAME}
After=network.target
[Service]
WorkingDirectory=${APP_DIR}
Environment="PATH=${VENV_DIR}/bin"
# Pass environment variables for model endpoints
WorkingDirectory=${BACKEND_APP_DIR}
Environment="PATH=${BACKEND_VENV_DIR}/bin"
Environment="MODEL_ENDPOINT_1=https://hermes.ai.unturf.com/v1"
Environment="MODEL_ENDPOINT_2=https://node2.naptha.ai/inference"
Environment="MODEL_ENDPOINT_3=https://node3.naptha.ai/inference"
# Add API keys if needed (e.g., from GitLab variables)
Environment="MODEL_API_KEY_1=${MODEL_API_KEY_1:-not-needed}"
Environment="MODEL_API_KEY_2=${MODEL_API_KEY_2:-not-needed}"
Environment="MODEL_API_KEY_3=${MODEL_API_KEY_3:-not-needed}"
ExecStart=${VENV_DIR}/bin/uwsgi \\
ExecStart=${BACKEND_VENV_DIR}/bin/uwsgi \\
--master \\
--enable-threads \\
--processes 4 \\
@ -73,8 +75,8 @@ deploy:
--chown-socket=gitlab-runner:caddy \\
--vacuum \\
--die-on-term \\
--chdir ${APP_DIR} \\
--logto2 ${APP_DIR}/uwsgi.log \\
--chdir ${BACKEND_APP_DIR} \\
--logto2 ${BACKEND_APP_DIR}/uwsgi.log \\
--module ${MODULE}
Restart=always
@ -91,11 +93,11 @@ deploy:
- systemctl --user restart ${SERVICE_NAME}
- systemctl --user enable ${SERVICE_NAME}
# Step 7: Handle Caddy configurations
# Step 7: Handle Caddy configurations for backend
- |
mkdir -p "${CADDY_SNIPPETS_DIR}"
cat > "${CADDY_SNIPPETS_DIR}/${APP_NAME}.caddy" <<EOF
${APP_NAME} {
cat > "${CADDY_SNIPPETS_DIR}/${BACKEND_APP_NAME}.caddy" <<EOF
${BACKEND_APP_NAME} {
encode gzip
tls {
on_demand
@ -106,6 +108,78 @@ deploy:
}
EOF
# Deployment completed
- echo "Backend deployment completed successfully."
deploy-streamlit:
stage: deploy-streamlit
tags:
- master.unturf.com
only:
- main # Deploy only from the 'main' branch
script:
# Step 1: Define variables
- |
SERVICE_NAME="streamlit-${STREAMLIT_APP_NAME//./-}.service" # Replace dots with dashes
STREAMLIT_PORT="8501" # Default Streamlit port, can be changed
# Step 2: Synchronize the code to the Streamlit application directory
- |
if [ ! -d "${STREAMLIT_APP_DIR}" ]; then
mkdir -p "${STREAMLIT_APP_DIR}"
fi
rsync -a --exclude=data/ --exclude=venv/ --delete "${CI_PROJECT_DIR}/" "${STREAMLIT_APP_DIR}/"
# Step 3: Set up Python virtual environment and install dependencies
- |
if [ ! -d "${STREAMLIT_VENV_DIR}" ]; then
python3 -m venv "${STREAMLIT_VENV_DIR}"
fi
source "${STREAMLIT_VENV_DIR}/bin/activate"
pip install --upgrade pip
pip install -r "${STREAMLIT_APP_DIR}/requirements.txt"
pip install streamlit
# Step 4: Create or update the systemd user service file for Streamlit
- |
mkdir -p ~/.config/systemd/user/
cat > ~/.config/systemd/user/${SERVICE_NAME} <<EOF
[Unit]
Description=Streamlit instance to serve ${STREAMLIT_APP_NAME}
After=network.target
[Service]
WorkingDirectory=${STREAMLIT_APP_DIR}
Environment="PATH=${STREAMLIT_VENV_DIR}/bin"
ExecStart=${STREAMLIT_VENV_DIR}/bin/streamlit run streamlit_slop_with_models.py --server.port ${STREAMLIT_PORT} --server.address 127.0.0.1
Restart=always
Type=simple
[Install]
WantedBy=default.target
EOF
# Step 5: Reload systemd user daemon and restart the Streamlit service
- systemctl --user daemon-reload
- systemctl --user restart ${SERVICE_NAME}
- systemctl --user enable ${SERVICE_NAME}
# Step 6: Handle Caddy configurations for Streamlit
- |
mkdir -p "${CADDY_SNIPPETS_DIR}"
cat > "${CADDY_SNIPPETS_DIR}/${STREAMLIT_APP_NAME}.caddy" <<EOF
${STREAMLIT_APP_NAME} {
encode gzip
tls {
on_demand
}
handle {
reverse_proxy 127.0.0.1:${STREAMLIT_PORT}
}
}
EOF
# Concatenate all snippets into the main Caddyfile
echo '# Main Caddyfile - concatenated snippets' > "${CADDYFILE_DIR}/Caddyfile"
cat "${CADDY_SNIPPETS_DIR}"/*.caddy >> "${CADDYFILE_DIR}/Caddyfile"
@ -113,8 +187,8 @@ deploy:
# Validate the Caddyfile
caddy validate --config "${CADDYFILE_DIR}/Caddyfile"
# Step 8: Reload Caddy service
# Step 7: Reload Caddy service
- sudo /bin/systemctl restart caddy.service
# Deployment completed
- echo "Deployment completed successfully."
- echo "Streamlit deployment completed successfully."