Hey everyone,
I’m currently working on multiple pipelines across various applications and have noticed a considerable amount of duplicated code. My intention is to centralize this code into a shared library for better reuse.
While I’ve had success with extracting functions into the library, I’ve run into challenges when trying to move entire stages. From my research, it seems that constructs like when don’t function as expected outside the context of steps or script in declarative pipelines.
For clarity, here’s a snippet of one of the stages I’m dealing with:
stage('Precheck') {
when {
expression { IS_RELEASE == 'false' }
}
parallel {
stage('Lint') {
steps {
sh 'npm run-script lint'
}
}
stage('Typecheck (App)') {
steps {
sh 'npm run-script typecheck:app'
}
}
stage('Typecheck (Api)') {
steps {
sh 'npm run-script typecheck:api'
}
}
stage('Typecheck (Spec)') {
steps {
sh 'npm run-script typecheck:spec'
}
}
stage('Typecheck (E2E)') {
steps {
sh 'npm run-script typecheck:e2e'
}
}
stage('Security Audit') {
steps {
sh('npm run-script audit')
}
}
}
}
I’d appreciate any insights or best practices on how to efficiently refactor such stages for a shared library setup. Thanks in advance!