jenkinsgroovyjenkins-pipelinejenkins-shared-libraries

Jenkins shared library does now work inside a loop


There is something wrong when using a my shared library code in a loop inside a Jenkins file.

I have a Jenkins Shared Library function like below in vars/codeSigning.groovy

void windows(String filePath) {
    String signFile = 'sign_windows.ps1'
    writeFile(file: signFile, text: libraryResource('sign_windows.ps1'))
    File signFilePath = new File(signFile)
    withCredentials([string(credentialsId: 'SM_API_KEY', variable: 'SM_API_KEY'), string(credentialsId: 'SM_CLIENT_CERT_PASSWORD', variable: 'SM_CLIENT_CERT_PASSWORD')]) {
        echo "Signing file: ${filePath}..."
        powershell label: "Signing file ${filePath}", script: """./${signFilePath} -FilePath "${filePath}" -CertificateName "COMPANY Incorporated" """
    }
}

When I call the function like this in a Jenkins file it works perfectly fine

stage('CTC - Pre Build Stage') {
   steps {
      script {
         codeSigning.windows('pathtofile')
      }
   }
}

When I run it like this get an error message from Jenkins that

java.lang.NoSuchMethodError: No such DSL method 'windows' found among steps [....] or globals [codeSigning, ...]

stage('Sign SharedScripts') {
   steps {
      unstash 'SharedScripts'
      script {
         def filesToSign = []
         def foundFiles = findFiles(glob: "scripts/**/*")
         foundFiles.each { filename ->
            if ("${filename}".endsWith(".ps1")) {
               filesToSign.add(filename)
            }
         }
         filesToSign.each { fileToSign ->
            codeSigning.windows(fileToSign)
         }
      }
   }
}

Solution

  • findFiles step returns an array of file info objects, not a list of strings, as you might expect. So you get mismatched method signatures. This should resolve it:

                codeSigning.windows(fileToSign.path)