98 lines
3.2 KiB
YAML
98 lines
3.2 KiB
YAML
stages:
|
||
- deploy
|
||
|
||
variables:
|
||
# Change these to match your desired domain/app name
|
||
STREAMLIT_APP_NAME: "art.ai.unturf.com"
|
||
STREAMLIT_APP_DIR: "/opt/${STREAMLIT_APP_NAME}"
|
||
STREAMLIT_VENV_DIR: "${STREAMLIT_APP_DIR}/venv"
|
||
|
||
# Adjust if your main Streamlit script has a different filename
|
||
STREAMLIT_SCRIPT_FILENAME: "streamlit_art_app.py"
|
||
|
||
# Caddy-related directories
|
||
CADDYFILE_DIR: "/etc/caddy"
|
||
CADDY_SNIPPETS_DIR: "/etc/caddy/caddy_snippets"
|
||
|
||
deploy:
|
||
stage: deploy
|
||
tags:
|
||
- master.unturf.com # Change to match your runner’s tags
|
||
only:
|
||
- main # Deploy only from the 'main' branch
|
||
script:
|
||
# Step 1: Define variables for systemd service
|
||
- |
|
||
SERVICE_NAME="streamlit-${STREAMLIT_APP_NAME//./-}.service" # Replace dots with dashes
|
||
STREAMLIT_PORT="8501" # Default Streamlit port
|
||
|
||
# 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
|
||
# If you have a requirements.txt, uncomment the line below:
|
||
# 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 service for ${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_SCRIPT_FILENAME} \\
|
||
--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
|
||
|
||
# Combine all snippet files into one main Caddyfile
|
||
echo '# Main Caddyfile - concatenated snippets' > "${CADDYFILE_DIR}/Caddyfile"
|
||
cat "${CADDY_SNIPPETS_DIR}"/*.caddy >> "${CADDYFILE_DIR}/Caddyfile"
|
||
|
||
# Validate the final Caddyfile
|
||
caddy validate --config "${CADDYFILE_DIR}/Caddyfile"
|
||
|
||
- sudo /bin/systemctl restart caddy.service
|
||
|
||
- echo "Streamlit deployment for ${STREAMLIT_APP_NAME} completed successfully."
|