Publish encrypted report archives from Jenkins

This commit is contained in:
Dandaedre 2026-09-14 19:01:59 +02:00
parent e08a71fad4
commit 5cf5f012a2
Signed by: Dandaedre
GPG Key ID: 440C6592E8A26D99
4 changed files with 103 additions and 169 deletions

3
.gitignore vendored
View File

@ -18,3 +18,6 @@
.DS_Store
Thumbs.db
Desktop.ini
# Jenkins encrypted publication artifacts
/.ci-artifacts/

197
Jenkinsfile vendored
View File

@ -4,11 +4,17 @@
pipeline {
agent { label 'rust_no_docker' }
options { disableConcurrentBuilds() }
environment {
CARGO_TERM_COLOR = 'always'
// CI-only staging directory.
CI_ARTIFACT_DIR = '.ci-artifacts'
DEPLOY_DIR = '/mnt/books/Reports'
DIAGNOSTICS_AUTHOR = 'Beffroi'
// Later replace with credentials('reports-zip-password') (Secret text).
REPORT_ZIP_PASSWORD = 'CHANGE_ME'
}
stages {
@ -62,49 +68,14 @@ pipeline {
}
}
stage('Validate Scripts') {
stage('Validate') {
steps {
sh '''
set -eu
echo "Checking shell scripts..."
if [ ! -f render.sh ]; then
echo "ERROR: render.sh is missing." >&2
exit 1
fi
bash -n render.sh
if [ ! -x render.sh ]; then
echo "ERROR: render.sh is not executable." >&2
exit 1
fi
echo "render.sh syntax OK."
'''
}
}
stage('Validate Typst Sources') {
steps {
sh '''
set -eu
echo "Checking Typst sources..."
typ_files="$(find . \
-type f \
-name '*.typ' \
-not -path './.git/*')"
if [ -z "${typ_files}" ]; then
echo "ERROR: no Typst source files found." >&2
exit 1
fi
echo "Typst sources:"
echo "${typ_files}"
bash -n render.sh render-diagnostics.sh tools/package-reports.sh
command -v zip
command -v python3
typst compile --root . tools/check-diagnostics.typ /tmp/check-diagnostics.pdf
'''
}
}
@ -113,112 +84,22 @@ pipeline {
steps {
sh '''
set -eu
echo "Rendering all reports..."
echo
rm -rf "${CI_ARTIFACT_DIR}"
mkdir -p "${CI_ARTIFACT_DIR}"
# Remove previously generated PDFs so that CI can verify
# that the current build actually produced them.
find . \
-type f \
-name '*.pdf' \
-not -path './.git/*' \
-not -path "./${CI_ARTIFACT_DIR}/*" \
-delete
# Only clear generated build outputs, never source PDFs.
rm -rf build "${CI_ARTIFACT_DIR}"
./render.sh --all
echo
echo "Render complete."
./render-diagnostics.sh --all "${DIAGNOSTICS_AUTHOR}"
'''
}
}
stage('Collect Reports') {
stage('Package Reports') {
steps {
sh '''
set +x
set -eu
echo "Collecting generated reports..."
count=0
find . \
-type f \
-name '*.pdf' \
-not -path './.git/*' \
-not -path "./${CI_ARTIFACT_DIR}/*" \
-print |
while IFS= read -r pdf; do
relative="${pdf#./}"
destination="${CI_ARTIFACT_DIR}/${relative}"
mkdir -p "$(dirname "${destination}")"
cp "${pdf}" "${destination}"
echo " ${relative}"
done
count="$(find "${CI_ARTIFACT_DIR}" \
-type f \
-name '*.pdf' |
wc -l |
tr -d ' ')"
echo
echo "Generated ${count} PDF report(s)."
if [ "${count}" -eq 0 ]; then
echo "ERROR: render.sh completed but produced no PDFs." >&2
exit 1
fi
'''
}
}
stage('Verify Reports') {
steps {
sh '''
set -eu
echo "Verifying generated PDFs..."
failed=0
find "${CI_ARTIFACT_DIR}" \
-type f \
-name '*.pdf' \
-print |
while IFS= read -r pdf; do
if [ ! -s "${pdf}" ]; then
echo "ERROR: empty PDF: ${pdf}" >&2
failed=1
continue
fi
# PDF files should begin with the PDF magic header.
magic="$(head -c 5 "${pdf}")"
if [ "${magic}" != "%PDF-" ]; then
echo "ERROR: invalid PDF header: ${pdf}" >&2
failed=1
continue
fi
echo "OK: ${pdf}"
done
if [ "${failed}" -ne 0 ]; then
exit 1
fi
echo
echo "All generated reports look valid."
./tools/package-reports.sh build "${CI_ARTIFACT_DIR}"
'''
archiveArtifacts artifacts: '.ci-artifacts/*.zip', fingerprint: true
}
}
@ -226,34 +107,12 @@ pipeline {
steps {
sh '''
set -eu
# ---------------------------------------------------------
# REPORT PUBLICATION PLACEHOLDER
# ---------------------------------------------------------
#
# Publishing is deliberately disabled for now.
#
# Generated PDFs are available under:
#
# .ci-artifacts/
#
# Future publication could for example:
#
# - rsync PDFs to the Beffroi report archive
# - copy them to a mounted archive directory
# - upload them to Gitea
# - publish only reports changed by this commit
#
# Do NOT deploy anything from this stage yet.
# ---------------------------------------------------------
echo "Report publication is currently disabled."
echo "Artifacts ready in ${CI_ARTIFACT_DIR}:"
find "${CI_ARTIFACT_DIR}" \
-type f \
-name '*.pdf' \
-print
mkdir -p "${DEPLOY_DIR}"
# Rename complete uploads on the destination filesystem.
for archive in Reports.zip diagnostics.zip; do
cp "${CI_ARTIFACT_DIR}/${archive}" "${DEPLOY_DIR}/.${archive}.tmp"
mv -f "${DEPLOY_DIR}/.${archive}.tmp" "${DEPLOY_DIR}/${archive}"
done
'''
}
}
@ -265,11 +124,11 @@ pipeline {
conclusion: 'SUCCESS',
name: 'Report CI',
summary: 'typst_reporter passed all CI checks.',
text: 'Shell validation, Typst source checks, report rendering, and PDF validation completed successfully. External publication is disabled.',
title: 'Reports rendered successfully'
text: 'Validation, rendering, encrypted ZIP packaging, and publication completed successfully.',
title: 'Encrypted reports published'
)
echo '✅ typst_reporter rendered and validated all reports.'
echo '✅ typst_reporter published encrypted report archives.'
}
failure {
@ -277,7 +136,7 @@ pipeline {
conclusion: 'FAILURE',
name: 'Report CI',
summary: 'typst_reporter failed CI.',
text: 'A script validation, Typst compilation, report rendering, or PDF validation step failed. Check the Jenkins log.',
text: 'A validation, rendering, packaging, or publication step failed. Check the Jenkins log.',
title: 'Report CI failed'
)

View File

@ -118,3 +118,25 @@ policy; see [Generated PDF policy](docs/advanced-setup.md#generated-pdf-policy).
## License
See [LICENSE](LICENSE).
### Jenkins publication
Jenkins renders YAML reports and JSON diagnostics, validates the PDFs, and
publishes encrypted archives to the same `/mnt/books` mount used by the books:
- `/mnt/books/Reports/Reports.zip`: YAML report PDFs.
- `/mnt/books/Reports/diagnostics.zip`: diagnostic PDFs, preserving subfolders.
Only the ZIPs are archived as Jenkins artifacts. The agent needs `zip`, Python 3,
and write access to `/mnt/books/Reports`. `DIAGNOSTICS_AUTHOR` defaults to Beffroi.
Packaging uses standard ZIP password encryption (ZipCrypto, not AES).
The temporary password is `CHANGE_ME`; it is a public placeholder, not a secret.
Later create a Jenkins **Secret text** credential and replace the Jenkinsfile
assignment with `REPORT_ZIP_PASSWORD = credentials('reports-zip-password')`.
The packaging script reads that environment variable with shell tracing disabled.
To package an existing local build:
```bash
REPORT_ZIP_PASSWORD=CHANGE_ME ./tools/package-reports.sh
```

50
tools/package-reports.sh Executable file
View File

@ -0,0 +1,50 @@
#!/usr/bin/env bash
# Create fresh encrypted ZIPs containing only validated PDFs.
set +x
set -euo pipefail
: "${REPORT_ZIP_PASSWORD:?Set REPORT_ZIP_PASSWORD before packaging}"
command -v zip >/dev/null
command -v python3 >/dev/null
build_dir="$(realpath "${1:-build}")"
output_dir="$(realpath -m "${2:-.ci-artifacts}")"
mkdir -p "$output_dir"
staging_dir="$(mktemp -d)"
trap 'rm -rf -- "$staging_dir"' EXIT
for category in Reports diagnostics; do
mkdir -p "$staging_dir/$category"
if [[ "$category" == Reports ]]; then
source_dir="$build_dir"
depth=(-maxdepth 1)
else
source_dir="$build_dir/diagnostics"
depth=()
fi
count=0
while IFS= read -r -d '' pdf; do
[[ -s "$pdf" && "$(head -c 5 "$pdf")" == '%PDF-' ]] || {
echo "Invalid or empty PDF: $pdf" >&2
exit 1
}
relative="${pdf#"$source_dir/"}"
mkdir -p "$staging_dir/$category/$(dirname "$relative")"
cp "$pdf" "$staging_dir/$category/$relative"
count=$((count + 1))
done < <(find "$source_dir" "${depth[@]}" -type f -name '*.pdf' -print0)
((count > 0)) || { echo "No PDFs for $category" >&2; exit 1; }
(
cd "$staging_dir/$category"
zip -q -r -P "$REPORT_ZIP_PASSWORD" "$staging_dir/$category.zip" .
)
# Check encryption and decrypt every entry to verify its CRC before publishing.
python3 - "$staging_dir/$category.zip" <<'PY_VERIFY'
import os, sys, zipfile
with zipfile.ZipFile(sys.argv[1]) as archive:
entries = [entry for entry in archive.infolist() if not entry.is_dir()]
assert entries, "Empty archive"
for entry in entries:
assert entry.flag_bits & 1, f"Unencrypted entry: {entry.filename}"
assert archive.read(entry, pwd=os.environ["REPORT_ZIP_PASSWORD"].encode()).startswith(b"%PDF-")
PY_VERIFY
cp "$staging_dir/$category.zip" "$output_dir/$category.zip"
done