64

I'm trying to create a task with a function inside:

def doCopyMibArtefactsHere(projectName) {
    step ([
        $class: 'CopyArtifact',
        projectName: $projectName,
        filter: '**/**.mib',
        fingerprintArtifacts: true, 
        flatten: true
    ]);
}

def BuildAndCopyMibsHere(projectName, params) {
    build job: $project, parameters: $params
    doCopyMibArtefactsHere($projectName)
}


node { 
    stage('Prepare Mib'){
        BuildAndCopyMibsHere('project1')
    }
}

But this gives me an exception:

java.lang.NoSuchMethodError: No such DSL method 'BuildAndCopyMibsHere' found among steps*

Is there any way to use embedded functions within a Pipeline script?

customcommander
  • 17,580
  • 5
  • 58
  • 84
Dr.eel
  • 1,837
  • 3
  • 18
  • 28
  • 1
    @dr-eel Don't go back and edit the question, especially when an existing answer (see the one from @jon-s) refers to the mistakes in it, as it loses context. I have reverted your change because of this reason only. As for your improvement of using the default parameter `[:]`, suggest that as a comment to the answer from @jon-s or even improve the answer by editing it. – haridsv May 25 '18 at 07:21

1 Answers1

55

First off, you shouldn't add $ when you're outside of strings ($class in your first function being an exception), so it should be:

def doCopyMibArtefactsHere(projectName) {
    step ([
        $class: 'CopyArtifact',
        projectName: projectName,
        filter: '**/**.mib',
        fingerprintArtifacts: true, 
        flatten: true
    ]);
}

def BuildAndCopyMibsHere(projectName, params) {
    build job: project, parameters: params
    doCopyMibArtefactsHere(projectName)
}
...

Now, as for your problem; the second function takes two arguments while you're only supplying one argument at the call. Either you have to supply two arguments at the call:

...
node { 
    stage('Prepare Mib'){
        BuildAndCopyMibsHere('project1', null)
    }
}

... or you need to add a default value to the functions' second argument:

def BuildAndCopyMibsHere(projectName, params = null) {
    build job: project, parameters: params
    doCopyMibArtefactsHere($projectName)
}
Rutger de Knijf
  • 1,112
  • 14
  • 23
Jon S
  • 15,846
  • 4
  • 44
  • 45
  • Ok. Now it says `java.lang.UnsupportedOperationException: must specify $class with an implementation of interface java.util.List`. P.S. original code corrected - $ replaced where vars not in strings. – Dr.eel Feb 10 '17 at 12:29
  • 1
    Strange, seems like a separate issue, try to use the snippet generator to regenerate the copy artefact step. If it still fails, I would suggest posting a new questions as it's a separate issue. – Jon S Feb 10 '17 at 13:04