Interview Prep/Jenkins/Shared libraries

How does a `vars/` global step work?

BeginnerConceptShared libraries
Answer

Each vars/<name>.groovy file becomes a global variable named <name>. If it defines call(...), it can be invoked like a step.

// vars/trivyScan.groovy
def call(Map cfg = [:]) {
  String severity = cfg.get('severity', 'HIGH,CRITICAL')
  sh "trivy image --exit-code 1 --ignore-unfixed --severity ${severity} --input ${cfg.input}"
}
trivyScan(input: 'image.tar')

Other methods in the same file can be called as trivyScan.helper().

All Jenkins questions Roadmap: Jenkins

More on Shared libraries