Enhance Bun integration in Jenkinsfile
Some checks failed
farmcontrol/farmcontrol-server/pipeline/head There was a failure building this commit

- 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.
This commit is contained in:
Tom Butcher 2026-07-28 22:14:36 +01:00
parent 0b1530a405
commit de895fed7d

98
Jenkinsfile vendored
View File

@ -2,6 +2,20 @@ properties([
buildDiscarder(logRotator(numToKeepStr: '20')) 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() { def ensureBunInstalled() {
if (isUnix()) { if (isUnix()) {
sh ''' sh '''
@ -13,14 +27,12 @@ def ensureBunInstalled() {
fi fi
bun -v bun -v
''' '''
env.PATH = "${env.HOME}/.bun/bin:${env.PATH}"
} else { } else {
bat ''' bat '''
where bun >nul 2>nul || powershell -c "irm bun.sh/install.ps1 | iex" 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% set PATH=%USERPROFILE%/.bun/bin;%PATH%
bun -v bun -v
''' '''
env.PATH = "${env.USERPROFILE}/.bun/bin;${env.PATH}"
} }
} }
@ -43,20 +55,22 @@ def buildLinux() {
checkout scm checkout scm
} }
stage('Setup Bun (Ubuntu)') { withBun {
ensureBunInstalled() stage('Setup Bun (Ubuntu)') {
} ensureBunInstalled()
}
stage('Install Dependencies (Ubuntu)') { stage('Install Dependencies (Ubuntu)') {
sh 'bun install --frozen-lockfile' sh 'bun install --frozen-lockfile'
} }
stage('Write Build Metadata (Ubuntu)') { stage('Write Build Metadata (Ubuntu)') {
writeBuildMetadata() writeBuildMetadata()
} }
stage('Build (Ubuntu)') { stage('Build (Ubuntu)') {
sh "VITE_BUILD_NUMBER=${env.BUILD_NUMBER} NODE_ENV=production bun run build:linux" sh "VITE_BUILD_NUMBER=${env.BUILD_NUMBER} NODE_ENV=production bun run build:linux"
}
} }
stage('Archive Artifacts (Ubuntu)') { stage('Archive Artifacts (Ubuntu)') {
@ -75,27 +89,29 @@ def buildOnLabel(label, buildCommand) {
checkout scm checkout scm
} }
stage("Setup Bun (${label})") { withBun {
ensureBunInstalled() 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})") { stage("Install Dependencies (${label})") {
writeBuildMetadata() if (isUnix()) {
} sh 'bun install --frozen-lockfile'
} else {
bat 'bun install --frozen-lockfile'
}
}
stage("Build (${label})") { stage("Write Build Metadata (${label})") {
if (isUnix()) { writeBuildMetadata()
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("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}"
}
} }
} }
@ -110,14 +126,16 @@ def setBuildNameFromPackageVersion() {
node('ubuntu') { node('ubuntu') {
stage('Set Build Name') { stage('Set Build Name') {
checkout scm checkout scm
ensureBunInstalled() withBun {
def version = sh( ensureBunInstalled()
script: "bun --eval \"console.log(JSON.parse(await Bun.file('package.json').text()).version)\"", def version = sh(
returnStdout: true script: "bun --eval \"console.log(JSON.parse(await Bun.file('package.json').text()).version)\"",
).trim() returnStdout: true
def buildName = "v${version}-b${env.BUILD_NUMBER}" ).trim()
currentBuild.displayName = buildName def buildName = "v${version}-b${env.BUILD_NUMBER}"
echo "Build name set to: ${buildName}" currentBuild.displayName = buildName
echo "Build name set to: ${buildName}"
}
} }
} }
} }