Interview Prep/Jenkins/Shared libraries

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.

All Jenkins questions Roadmap: Jenkins

More on Shared libraries