farmcontrol-server/Jenkinsfile
Tom Butcher de895fed7d
Some checks failed
farmcontrol/farmcontrol-server/pipeline/head There was a failure building this commit
Enhance Bun integration in Jenkinsfile
- Introduced helper functions to manage Bun installation paths for both Unix and Windows environments.
- Wrapped Bun installation and dependency setup stages in a new `withBun` closure for improved readability and organization.
- Ensured consistent handling of Bun installation across different stages of the CI pipeline.
2026-07-28 22:14:36 +01:00

158 lines
4.4 KiB
Groovy

properties([
buildDiscarder(logRotator(numToKeepStr: '20'))
])
def bunBinDir() {
if (isUnix()) {
return "${env.HOME}/.bun/bin"
}
return "${env.USERPROFILE}/.bun/bin"
}
def withBun(Closure body) {
def sep = isUnix() ? ':' : ';'
withEnv(["PATH=${bunBinDir()}${sep}${env.PATH}"]) {
body()
}
}
def ensureBunInstalled() {
if (isUnix()) {
sh '''
set -eu
export BUN_INSTALL="${HOME}/.bun"
export PATH="${BUN_INSTALL}/bin:${PATH}"
if ! command -v bun >/dev/null 2>&1; then
curl -fsSL https://bun.sh/install | bash
fi
bun -v
'''
} else {
bat '''
where bun >nul 2>nul || %SystemRoot%/System32/WindowsPowerShell/v1.0/powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "irm bun.sh/install.ps1 | iex"
set PATH=%USERPROFILE%/.bun/bin;%PATH%
bun -v
'''
}
}
def writeBuildMetadata() {
if (isUnix()) {
sh '''
bun --eval "await Bun.write('src/buildInfo.json', JSON.stringify({ buildNumber: process.env.BUILD_NUMBER || 'dev' }, null, 2) + '\\n')"
'''
} else {
bat '''
bun --eval "await Bun.write('src/buildInfo.json', JSON.stringify({ buildNumber: process.env.BUILD_NUMBER || 'dev' }, null, 2) + '\\n')"
'''
}
}
def buildLinux() {
node('ubuntu') {
try {
stage('Checkout (Ubuntu)') {
checkout scm
}
withBun {
stage('Setup Bun (Ubuntu)') {
ensureBunInstalled()
}
stage('Install Dependencies (Ubuntu)') {
sh 'bun install --frozen-lockfile'
}
stage('Write Build Metadata (Ubuntu)') {
writeBuildMetadata()
}
stage('Build (Ubuntu)') {
sh "VITE_BUILD_NUMBER=${env.BUILD_NUMBER} NODE_ENV=production bun run build:linux"
}
}
stage('Archive Artifacts (Ubuntu)') {
archiveArtifacts artifacts: 'app_dist/**/*.deb, app_dist/**/*.rpm', fingerprint: true
}
} finally {
cleanWs()
}
}
}
def buildOnLabel(label, buildCommand) {
return {
node(label) {
stage("Checkout (${label})") {
checkout scm
}
withBun {
stage("Setup Bun (${label})") {
ensureBunInstalled()
}
stage("Install Dependencies (${label})") {
if (isUnix()) {
sh 'bun install --frozen-lockfile'
} else {
bat 'bun install --frozen-lockfile'
}
}
stage("Write Build Metadata (${label})") {
writeBuildMetadata()
}
stage("Build (${label})") {
if (isUnix()) {
sh "VITE_BUILD_NUMBER=${env.BUILD_NUMBER} NODE_ENV=production ${buildCommand}"
} else {
bat "set VITE_BUILD_NUMBER=${env.BUILD_NUMBER} && set NODE_ENV=production && ${buildCommand}"
}
}
}
stage("Archive Artifacts (${label})") {
archiveArtifacts artifacts: 'app_dist/**/farmcontrol-server-*', fingerprint: true
}
}
}
}
def setBuildNameFromPackageVersion() {
node('ubuntu') {
stage('Set Build Name') {
checkout scm
withBun {
ensureBunInstalled()
def version = sh(
script: "bun --eval \"console.log(JSON.parse(await Bun.file('package.json').text()).version)\"",
returnStdout: true
).trim()
def buildName = "v${version}-b${env.BUILD_NUMBER}"
currentBuild.displayName = buildName
echo "Build name set to: ${buildName}"
}
}
}
}
try {
setBuildNameFromPackageVersion()
parallel(
'Windows Build': buildOnLabel('windows', 'bun run build:app'),
'MacOS Build': buildOnLabel('macos', 'bun run build:app:mac'),
'Ubuntu Build': { buildLinux() }
)
echo 'All parallel stages completed successfully!'
} catch (Exception e) {
echo "Pipeline failed: ${e.message}"
throw e
}