art.ai.unturf.com/.gitlab-ci.yml

98 lines
3 KiB
YAML

stages:
- deploy
variables:
# Domain/app name
STREAMLIT_APP_NAME: "art.ai.unturf.com"
# Deployment paths
STREAMLIT_APP_DIR: "/opt/${STREAMLIT_APP_NAME}"
STREAMLIT_VENV_DIR: "${STREAMLIT_APP_DIR}/venv"
# Main python script for Streamlit
STREAMLIT_SCRIPT_FILENAME: "black_forest_streamlit.py"
# Use a different port (8502), so it doesn't conflict with anything on 8501
STREAMLIT_PORT: "8502"
# Caddy config paths
CADDYFILE_DIR: "/etc/caddy"
CADDY_SNIPPETS_DIR: "/etc/caddy/caddy_snippets"
deploy:
stage: deploy
tags:
- master.unturf.com
only:
- main
script:
# 1. Define a systemd user service name based on domain:
- |
SERVICE_NAME="streamlit-${STREAMLIT_APP_NAME//./-}.service"
# 2. Sync code to the target 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}/"
# 3. Setup Python venv & install packages:
- |
if [ ! -d "${STREAMLIT_VENV_DIR}" ]; then
python3 -m venv "${STREAMLIT_VENV_DIR}"
fi
source "${STREAMLIT_VENV_DIR}/bin/activate"
pip install --upgrade pip
# Uncomment if you have a requirements.txt for your app
pip install -r "${STREAMLIT_APP_DIR}/requirements.txt"
# 4. Create/update the systemd service, including the API key environment variable:
- |
mkdir -p ~/.config/systemd/user/
cat > ~/.config/systemd/user/${SERVICE_NAME} <<EOF
[Unit]
Description=Streamlit Service for ${STREAMLIT_APP_NAME}
After=network.target
[Service]
WorkingDirectory=${STREAMLIT_APP_DIR}
Environment="PATH=${STREAMLIT_VENV_DIR}/bin"
Environment="BLACK_FOREST_LABS_API_KEY=${BLACK_FOREST_LABS_API_KEY}"
ExecStart=${STREAMLIT_VENV_DIR}/bin/streamlit run ${STREAMLIT_SCRIPT_FILENAME} \\
--server.port ${STREAMLIT_PORT} \\
--server.address 127.0.0.1
Restart=always
Type=simple
[Install]
WantedBy=default.target
EOF
# 5. Reload systemd (user) and restart the Streamlit service:
- systemctl --user daemon-reload
- systemctl --user restart ${SERVICE_NAME}
- systemctl --user enable ${SERVICE_NAME}
# 6. Configure Caddy to proxy the domain to port 8502:
- |
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
echo '# Main Caddyfile - concatenated snippets' > "${CADDYFILE_DIR}/Caddyfile"
cat "${CADDY_SNIPPETS_DIR}"/*.caddy >> "${CADDYFILE_DIR}/Caddyfile"
caddy validate --config "${CADDYFILE_DIR}/Caddyfile"
- sudo /bin/systemctl restart caddy.service
# Done
- echo "Deployment for ${STREAMLIT_APP_NAME} on port ${STREAMLIT_PORT} completed successfully."