How do you write a "pipeline template" step in a shared library?
AdvancedConceptShared libraries
Answer
A vars/ step can contain an entire declarative pipeline, so app repos only pass parameters.
// vars/nodeServicePipeline.groovy
def call(Map cfg) {
pipeline {
agent { kubernetes { yaml libraryResource('pods/node-build.yaml'); defaultContainer 'node' } }
options { timeout(time: 30, unit: 'MINUTES') }
stages {
stage('Test') { steps { sh 'npm ci && npm test' } }
stage('Image') { steps { kanikoBuild(image: cfg.image) } }
}
}
}
@Library('shack-lib@v1') _
nodeServicePipeline(image: 'docker.io/devopsshack/shack-api')
Only one pipeline {} may run per build. It standardizes teams but reduces flexibility, so expose hooks for extra stages.
More on Shared libraries
What is the directory structure of a shared library?BeginnerHow does a `vars/` global step work?BeginnerWhat goes in `src/` and how is it used?IntermediateHow do classes in `src/` call pipeline steps?IntermediateWhat is `libraryResource` used for?IntermediateWhat are the ways to load a library in a Jenkinsfile?Intermediate