0

I am trying to implement Shared Libraries in my Jenkins scripted pipeline. I was able to configure Jenkins to pull the library from Git. I currently have one file, vars/Pipeline.groovy which I'm calling from my Jenkinsfile to control the build process. That file contains the following steps, which are mostly just proof of concept:

#!/usr/bin/groovy
import com.foo.bar.Constants
import com.foo.bar.Apps

def call(body) {

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

def setup() {
    node() {
        echo "Check workspace..."
        dir("${env.WORKSPACE}") {
            sh 'pwd'
        }
        echo "Running the setup stage..."
        echo "Testing Constants"
        git_url = Constants.GITLAB_WEB_URL
        echo "Gitlab: ${git_url}"
        echo "Directly - Gitlab: ${Constants.GITLAB_WEB_URL}"
    }
}
def getSource(String branch) {
    echo "Checking out source from branch: ${branch}"
}
def buildComponents() {
    echo "Building an application"
    myApp = new App("/some/source/dir", "MyNewApp")

    myApp.build()
}
def buildAndroidApps() {
    echo "Building Android Apps..."
}
def runUnitTests() {
    echo "Running unit tests..."
}
def deliverProducts() {
    echo "Delivering/staging products"
}
def sendNotification() {
    echo "Notification of build result...Done."
}

return this

The contents of the src/com/foo/bar/Apps.groovy:

package com.foo.bar

String source
String name

class App implements Serializable {
    def App(String source, String pro_file) {
        this.source = source
        this.name = name
}

    def build() {
        stage("Building App") {
            echo "Source: ${this.source}"
            echo "Name: ${this.name}"
        }
    }
}

The primary issue seems to be that I am unable to instantiate an object from the App() class. When I call myApp = new App("source/dir", "SomeName") I get the following error:

groovy.lang.GroovyRuntimeException: Could not find matching constructor for: com.foo.bar.App(java.lang.String, java.lang.String)

I can't figure out what I'm doing wrong and it's really frustrating. I have tried renaming the groovy file, tried running it without the class declaration as well as a bunch of other changes that were just desperate attempts to get it to work. I've tried Googling the error and even searched on SO, but the problem is always just different enough to not be applicable to my relatively simple use case.

Here is the content of my Jenkinsfile in case it is helpful in solving this issue.

@Library('shared-library@branch')
import com.foo.bar.*

job = new Pipeline()

job.setup();
job.getSource("${Branch}");
job.buildComponents();
job.buildAndroidApps();
job.runUnitTests();
job.deliverProducts();
job.sendNotification();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.