ravenous_roll20/Jenkinsfile

252 lines
9.9 KiB
Groovy

// Add this webhook in Gitea:
// http://192.168.1.37:8080/gitea-webhook/post
pipeline {
agent { label 'rust_no_docker' }
environment {
UV_CACHE_DIR = "${WORKSPACE}/.uv-cache"
BUILD_DIR = 'build'
CRAFT_BUILD_DIR = 'craft_web/build'
DEPLOY_ROOT = '/mnt/books/RPG'
SHEET_DIR = 'ravenous_sheet'
CRAFT_DIR = 'ravenous_craft'
LEGACY_DEPLOY_DIR = '/mnt/books/ravenous_sheet'
}
stages {
stage('Checkout') {
steps {
checkout scm
sh '''
set -e
git submodule sync --recursive
git submodule update --init --recursive
'''
}
}
stage('Install uv') {
steps {
sh '''
set -e
if ! command -v uv >/dev/null 2>&1; then
echo "uv not found — installing it with the Rust toolchain..."
cargo install --locked uv
else
echo "uv is already installed."
fi
uv --version
'''
}
}
stage('Install Python SQLite') {
steps {
sh '''
set -e
uv sync --frozen
if ! uv run python -c 'import sqlite3' >/dev/null 2>&1; then
if [ "$(uname -s)" != FreeBSD ] || ! command -v pkg >/dev/null 2>&1; then
echo 'The selected Python lacks sqlite3, and no FreeBSD pkg installer is available.' >&2
exit 1
fi
python_flavor=$(uv run python -c 'import sys; print(f"{sys.version_info.major}{sys.version_info.minor}")')
sqlite_package="py${python_flavor}-sqlite3"
echo "Installing ${sqlite_package} for the Python used by uv..."
if [ "$(id -u)" -eq 0 ]; then
pkg install -y "${sqlite_package}"
elif command -v sudo >/dev/null 2>&1; then
sudo -n pkg install -y "${sqlite_package}"
elif command -v doas >/dev/null 2>&1; then
doas -n pkg install -y "${sqlite_package}"
else
echo "Install ${sqlite_package} on the Jenkins agent or grant noninteractive pkg access." >&2
exit 1
fi
fi
uv run python -c 'import sqlite3; print("SQLite", sqlite3.sqlite_version)'
'''
}
}
stage('Build and test sheet') {
steps {
sh '''
set -e
mkdir -p "${BUILD_DIR}"
uv sync --frozen
./scripts/build.sh
uv run python craft_web/tools/build.py --output "${CRAFT_BUILD_DIR}"
uv run python tools/export_monsters.py --id 2 --output "${BUILD_DIR}/example-monster-2.json"
uv run python tools/export_monsters.py --id 5 --output "${BUILD_DIR}/example-monster-5.json"
uv run python -m unittest discover -s tests -v
uv run python -m unittest discover -s craft_web/tests -v
if command -v node >/dev/null 2>&1; then
RAVENOUS_CHARACTER_JSON="${BUILD_DIR}/example-character.json" \
RAVENOUS_MONSTER_JSON="${BUILD_DIR}/example-monster-2.json" \
node tests/test_character_io.mjs
RAVENOUS_MONSTER_JSON="${BUILD_DIR}/example-monster-5.json" \
node tests/test_character_io.mjs
node tests/test_craft_import.mjs
else
echo 'Node is not installed on this agent; skipping the optional Roll20 Mod simulator tests.'
fi
./scripts/build.sh --check
uv run python craft_web/tools/build.py --output "${CRAFT_BUILD_DIR}" --check
test -f "${BUILD_DIR}/ravenous.html"
test -f "${BUILD_DIR}/ravenous.css"
test -f "${BUILD_DIR}/translation.json"
test -f "${BUILD_DIR}/sheet.json"
test -f "${BUILD_DIR}/build-info.json"
test -f "${BUILD_DIR}/example-character.json"
test -f "${BUILD_DIR}/example-craft-forge.json"
test -f "${BUILD_DIR}/example-craft-alchemy.json"
test -f "${BUILD_DIR}/example-monster-2.json"
test -f "${BUILD_DIR}/example-monster-5.json"
test -f "${CRAFT_BUILD_DIR}/index.html"
test -f "${CRAFT_BUILD_DIR}/styles.css"
test -f "${CRAFT_BUILD_DIR}/app.js"
test -f "${CRAFT_BUILD_DIR}/catalog.js"
test -f "${CRAFT_BUILD_DIR}/build-info.json"
echo "Roll20 sheet build complete:"
ls -lh "${BUILD_DIR}"
echo "Craft workshop build complete:"
ls -lh "${CRAFT_BUILD_DIR}"
'''
}
}
stage('Archive sheet') {
steps {
archiveArtifacts(
artifacts: 'build/**,craft_web/build/**',
fingerprint: true
)
}
}
stage('Deploy sheet') {
steps {
script {
def reviewBranches = ['origin/lighthouse', 'lighthouse']
def devBranches = ['origin/candlelight', 'candlelight']
def productionBranches = ['origin/main', 'main', 'origin/master', 'master']
def targetDir
def legacyDir
def craftTargetDir
if (reviewBranches.contains(env.BRANCH_NAME)) {
targetDir = "${DEPLOY_ROOT}/${SHEET_DIR}_lighthouse"
legacyDir = "${LEGACY_DEPLOY_DIR}_lighthouse"
craftTargetDir = "${DEPLOY_ROOT}/${CRAFT_DIR}_lighthouse"
} else if (devBranches.contains(env.BRANCH_NAME)) {
targetDir = "${DEPLOY_ROOT}/${SHEET_DIR}_candlelight"
legacyDir = "${LEGACY_DEPLOY_DIR}_candlelight"
craftTargetDir = "${DEPLOY_ROOT}/${CRAFT_DIR}_candlelight"
} else if (productionBranches.contains(env.BRANCH_NAME)) {
targetDir = "${DEPLOY_ROOT}/${SHEET_DIR}"
legacyDir = LEGACY_DEPLOY_DIR
craftTargetDir = "${DEPLOY_ROOT}/${CRAFT_DIR}"
} else {
echo "Branch '${env.BRANCH_NAME}' is not a publication branch — skipping deployment."
return
}
deployTo(targetDir, legacyDir)
deployCraftTo(craftTargetDir)
}
}
}
}
post {
success {
publishChecks(
conclusion: 'SUCCESS',
name: 'Roll20 Sheet Build Successful',
summary: 'The Ravenous Roll20 sheet was generated and validated successfully.',
text: 'The Roll20 bundle and static craft workshop are available as Jenkins artifacts.',
title: 'Roll20 Sheet Build Successful'
)
echo '✅ Ravenous Roll20 sheet built successfully!'
}
failure {
publishChecks(
conclusion: 'FAILURE',
name: 'Roll20 Sheet Build Failed',
summary: 'The Ravenous Roll20 sheet build or deployment failed.',
text: 'Review the Jenkins build, test, deterministic-check, and deployment logs.',
title: 'Roll20 Sheet Build Failed'
)
echo '❌ Ravenous Roll20 sheet failed to build or publish.'
}
unstable {
publishChecks(
conclusion: 'ACTION_REQUIRED',
name: 'Roll20 Sheet Build Unstable',
summary: 'The Ravenous Roll20 sheet build is unstable.',
text: 'The Jenkins build completed with an unstable status.',
title: 'Roll20 Sheet Build Unstable'
)
echo '⚠️ Ravenous Roll20 sheet build is unstable.'
}
}
}
/** Publish the static craft workshop below the shared RPG root. */
def deployCraftTo(String targetDir) {
def allowedDirs = [
'/mnt/books/RPG/ravenous_craft',
'/mnt/books/RPG/ravenous_craft_candlelight',
'/mnt/books/RPG/ravenous_craft_lighthouse'
]
if (!allowedDirs.contains(targetDir)) {
error("Refusing to publish craft workshop to unexpected path: ${targetDir}")
}
sh """
set -e
echo "Publishing Ravenous craft workshop to ${targetDir}"
mkdir -p "${targetDir}"
rsync -a --delete "${env.CRAFT_BUILD_DIR}/" "${targetDir}/"
"""
}
/** Publish under the shared RPG root, then remove this branch's legacy location. */
def deployTo(String targetDir, String legacyDir) {
def allowedLegacyDirs = [
'/mnt/books/ravenous_sheet',
'/mnt/books/ravenous_sheet_candlelight',
'/mnt/books/ravenous_sheet_lighthouse'
]
if (!allowedLegacyDirs.contains(legacyDir)) {
error("Refusing to remove unexpected legacy deployment path: ${legacyDir}")
}
sh """
set -e
echo "Publishing Ravenous Roll20 sheet to ${targetDir}"
mkdir -p "${targetDir}"
rsync -a --delete "${env.BUILD_DIR}/" "${targetDir}/"
if [ -d "${legacyDir}" ]; then
echo "Removing legacy Ravenous Roll20 sheet from ${legacyDir}"
rm -rf -- "${legacyDir}"
fi
"""
}