agent-tasks/Jenkinsfile

104 lines
3.4 KiB
Groovy

// Add the Gitea webhook configured for this Jenkins installation.
pipeline {
agent { label 'rust_nodocker' }
options {
disableConcurrentBuilds()
skipDefaultCheckout(true)
}
environment {
ASSET_SOURCE_DIR = 'tasks'
ASSET_PUBLISH_ROOT = '/mnt/assets/agent-tasks'
}
stages {
stage('Checkout') {
steps {
deleteDir()
checkout scm
}
}
stage('Validate publishable tasks') {
steps {
sh '''
set -eu
test -d "${ASSET_SOURCE_DIR}" || {
echo "ERROR: ${ASSET_SOURCE_DIR} is not a directory." >&2
exit 1
}
invalid_type=$(find "${ASSET_SOURCE_DIR}" ! -type d ! -type f -print -quit)
test -z "${invalid_type}" || {
echo "ERROR: publishable assets must be regular files: ${invalid_type}" >&2
exit 1
}
task_count=0
for task in "${ASSET_SOURCE_DIR}"/*.task.md; do
test -e "${task}" || continue
test -f "${task}" && test ! -L "${task}" && test -s "${task}" || {
echo "ERROR: task must be a nonempty regular file: ${task}" >&2
exit 1
}
task_count=$((task_count + 1))
done
test "${task_count}" -gt 0 || {
echo "ERROR: no publishable *.task.md files were found." >&2
exit 1
}
unexpected=$(find "${ASSET_SOURCE_DIR}" -mindepth 1 \
\( ! -type f -o ! -name '*.task.md' \) -print -quit)
test -z "${unexpected}" || {
echo "ERROR: unexpected file in ${ASSET_SOURCE_DIR}: ${unexpected}" >&2
exit 1
}
echo "Validated ${task_count} task files for publication."
'''
}
}
stage('Publish assets') {
when {
branch 'main'
}
steps {
sh '''
set -eu
case "${ASSET_PUBLISH_ROOT}" in
/*) ;;
*)
echo "ERROR: ASSET_PUBLISH_ROOT must be absolute." >&2
exit 1
;;
esac
mkdir -p "${ASSET_PUBLISH_ROOT}"
chmod 0755 "${ASSET_PUBLISH_ROOT}"
for source in "${ASSET_SOURCE_DIR}"/*.task.md; do
name=${source##*/}
destination="${ASSET_PUBLISH_ROOT}/${name}"
temporary="${destination}.tmp-${BUILD_NUMBER}-$$"
trap 'rm -f "${temporary}"' 0 1 2 3 15
cp "${source}" "${temporary}"
chmod 0644 "${temporary}"
mv "${temporary}" "${destination}"
trap - 0 1 2 3 15
done
echo "Published task files below ${ASSET_PUBLISH_ROOT}."
'''
}
}
}
}