3

I would like to call the next shared library from a pipeline:

[a link] https://github.com/docker/jenkins-pipeline-scripts/blob/master/vars/wrappedNode.groovy

I don't know how to call and populate the vars map and the body closure from the Jenkins 2 pipeline.

My shared library name is "vars/my_shared_library.groovy", and their content is

def call(Map vars, Closure body=null) {
    vars = vars ?: [:]
    def myParameter = vars.get("myParam1",null)
    if (body) { body() }
    stuff...
}

The Jenkinsfile content is:

@Library 'my_shared_library'
pipeline {
  agent none
  stages {
     stage ('info') {
         node {
            my_shared_library {
               myParam1 = "myValue1"
            }
         }
    }
  }
}

1 Answers1

4

To call you custom step from the pipeline, invoke it like this:

node {
  my_shared_library(myParam1: "Jose"){
    echo "hello"
  }
}

To do something in the body with the map handed in, you need to change your step:

def call(Map vars, Closure body=null) {
    vars = vars ?: [:]
    def myParameter = vars.get("myParam1",null)
    if (body) { body(myParameter) }
    stuff...
}

And the pipeline to this:

node {
  my_shared_library(myParam1: "Jose"){ param -> 
    echo "hello ${param}"
  }
}
Christopher
  • 1,103
  • 1
  • 6
  • 18