Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I define custom steps using a hash-table for params for a declarative pipeline?

I want to create a custom step as detailed here: https://jenkins.io/doc/book/pipeline/shared-libraries/#defining-custom-steps

The script looks like this:

// vars/buildPlugin.groovy
def call(body) {
    // evaluate the body block, and collect configuration into the object
    def config = [:]
    body.resolveStrategy = Closure.DELEGATE_FIRST
    body.delegate = config
    body()
...

And I can run it like this in a scripted pipeline:

buildPlugin {
    name = 'git'
}

Which means in a declarative pipeline i gotta wrap it in a script block:

script {
    buildPlugin {
        name = 'git'
    }
}

I have a lot of custom scripts and groovy classes and it clutters things up to have to wrap them in script blocks in my pipeline. Can I write groovy scripts in a way the declarative pipeline can use without script{}?

EDIT:

Calling a groovy script from the pipeline like this works:

myCustomStep('sldkfjlskdf')

But I want to use a hashtable like they have in the examples:

# In myCustomStep.grooy
def call(body) {

    def config = [:]
    body.resolveStrategy = Closure.DELEGATE_FIRST
    body.delegate = config
    body()

In order to call it now I have to do:

myCustomStep{
    param1 = 'sldkfjlskdf'
    param2 = 'sdfsdfsdfdf'
}

Doing this I get Expected a step @ line.... and have to wrap it in a step

Is there a way to get nice named params like with the hash-table approach but not have to wrap in a step? I also tried calling it like myCustomStep({param1 = 'sdfsdf'}) which did not work

like image 506
red888 Avatar asked Dec 20 '17 17:12

red888


People also ask

How do you pass parameters in Jenkins declarative pipeline?

Using build parameters, we can pass any data we want: git branch name, secret credentials, hostnames and ports, and so on. Any Jenkins job or pipeline can be parameterized. All we need to do is check the box on the General settings tab, “This project is parameterized”: Then we click the Add Parameter button.

Which directive is used to set variables in declarative pipeline?

The environment directive specifies a sequence of key-value pairs which will be defined as environment variables for the all steps, or stage-specific steps, depending on where the environment directive is located within the Pipeline.

How does Jenkins pipeline pass Choice parameters?

Go to Jenkins Home, select New Item, add a name for your Job, for the project type, select Pipeline project and click on Ok. On the configure job page select the This project is parameterized checkbox in the general tab. Now, we will add an Active Choices Parameter which renders our Application Tiers as a Dropdown.


1 Answers

you can use it also in declarative pipline without script wrapper

Here is example that works well:

script

//vars/shOut.groovy
def call(shellScript) {
  return sh(returnStdout: true, script: shellScript).trim()
}

Jenkinsfile

@Library('modelsLib') _

pipeline {
  agent { label 'master' }

  stages {
    stage('some stage') {
      steps {
        echo "hello!"
        shOut 'touch xxyyzz'
        sh 'ls -otr'
      }
    }
  }
}

output

[Pipeline] {
[Pipeline] stage
[Pipeline] { (some stage)
[Pipeline] echo
hello!
[Pipeline] sh
[test-pipeline] Running shell script
+ touch xxyyzz
[Pipeline] sh
[test-pipeline] Running shell script
+ ls -otr
total 32
-rw-r--r-- 1 jenkins     0 Dec 20 17:59 xxyyzz
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
like image 185
tworec Avatar answered Sep 24 '22 20:09

tworec